在项目中遇到了一个功能,就是选中listview中的任意一个item,其他的是未被选中状态。或者是多个item被同时勾选中。根据被选中的item计算金额的总和。如下图所示:
(https://img-blog.youkuaiyun.com/20151215110800371)
下面给出源码:
activity的布局immediaterepayment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f7f7f7"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#dadada" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/all"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:textSize="19sp"
android:textColor="#666666"
android:text="总计:6333元" />
<TextView
android:id="@+id/huankuan"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:background="#3a8dee"
android:gravity="center"
android:text="立即还款"
android:textSize="19sp"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
适配器的布局文件immediaterepaymentadapter.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="5dp"
android:src="@drawable/gou_no" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp" >
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="333元"
android:textColor="#ed4a18"
android:textSize="19sp" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:gravity="right"
android:text="最迟还款:12月12日"
android:textColor="#999999"
android:textSize="15sp" />
</RelativeLayout>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="left"
android:text="开店分期付款第2期还款"
android:textColor="#666666"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity的Java代码ImmediateRepayment:
public class ImmediateRepayment extends Activity implements OnClickListener {
private ImmediateRepaymentAdapter adapter;
private ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
private ListView listView;
private TextView all, huankuan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.immediaterepayment);
loginMap = Internet.getLoginData(this);
init();
}
private void init() {
findId();
setText();
setAdapter();
addData();
setListener();
}
private void setText() {
Spanned spanned = Html.fromHtml("总计:"
+ "<font color=#ed4a18 >0元</font>");
all.setText(spanned);
}
private void addData() {
for (int i = 0; i < 5; i++) {
HashMap<String, Object> map = new HashMap<>();
map.put("money", 444);
map.put("date", "最迟还款:12月12日");
map.put("text", "开店分期付款第2期还款");
map.put("view", "0");
arrayList.add(map);
}
}
private void setAdapter() {
adapter = new ImmediateRepaymentAdapter(ImmediateRepayment.this,
arrayList);
listView.setAdapter(adapter);
}
private void setListener() {
back.setOnClickListener(this);
huankuan.setOnClickListener(this);
listView.setOnItemClickListener(listener);
}
private void findId() {
back = (ImageView) findViewById(R.id.back);
listView = (ListView) findViewById(R.id.listview);
all = (TextView) findViewById(R.id.all);
huankuan = (TextView) findViewById(R.id.huankuan);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 立即还款
case R.id.huankuan:
break;
}
}
OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
for (int i = 0; i < arrayList.size(); i++) {
if (position == i) {
arrayList.get(position).put("view", "1");
} else {
arrayList.get(i).put("view", "0");
}
}
int countMoney = 0;
for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.get(i).get("view").toString().equals("1")) {
countMoney += Integer.parseInt(arrayList.get(i)
.get("money").toString());
}
}
adapter.notifyDataSetChanged();
Spanned spanned = Html.fromHtml("总计:" + "<font color=#ed4a18 >"
+ countMoney + "元</font>");
all.setText(spanned);
}
};
}
适配器的Java代码ImmediateRepaymentAdapter:
public class ImmediateRepaymentAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String, Object>> arrayList;
public ImmediateRepaymentAdapter(Context context,
ArrayList<HashMap<String, Object>> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return getItem(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(
R.layout.immediaterepaymentadapter, null);
TextView tv_money = (TextView) convertView.findViewById(R.id.money);
TextView tv_time = (TextView) convertView.findViewById(R.id.time);
TextView tv_content = (TextView) convertView
.findViewById(R.id.content);
ImageView img = (ImageView) convertView.findViewById(R.id.image);
tv_money.setText(arrayList.get(position).get("money").toString()+"元");
tv_time.setText(arrayList.get(position).get("date").toString());
tv_content.setText(arrayList.get(position).get("text").toString());
if (arrayList.get(position).get("view").toString().equals("0"))
{
img.setBackgroundResource(R.drawable.gou_no);
}else
{
img.setBackgroundResource(R.drawable.gou);
}
return convertView;
}
}
这样子就可以实现单个item被选中了。如果要实现多个item同时被选中,只需要将onItemClick中第一个for循环换成如下代码即可:
if (arrayList.get(position).get("view").toString().equals("0")) {
arrayList.get(position).put("view", "1");
} else {
arrayList.get(position).put("view", "0");
}
以上就是实现的全部代码了,个人技术有限,不喜勿喷哈!如有写的不对的地方,欢迎大家多给建议。