本文主要是介绍listView隔行换色多选,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参照网上相关文章写了下listview隔行换色与多选
1.item_fg_seclib.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="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:gravity="center_vertical"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
>
<CheckBox
android:id="@+id/itemCbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/blue"
android:text=""
/>
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/pro_name"
android:textColor="@color/blue"/>
</LinearLayout>
<TextView
android:id="@+id/itemSn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/pro_Bid"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/blue"/>
<TextView
android:id="@+id/itemStatus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/status"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="@dimen/h8"
android:textColor="@color/blue"/>
</LinearLayout>
</LinearLayout>
2.SecUpLoadAdapter:
public class SecUpLoadAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductItemInfo> list;
private static HashMap<Integer, Boolean> isSelected;
class Viewholder{
CheckBox itemCbox;
TextView itemName;
TextView itemSn;
TextView itemStatus;
}
public SecUpLoadAdapter(Context context,ArrayList<ProductItemInfo> list){
this.context=context;
this.list=list;
isSelected=new HashMap<Integer, Boolean>();
initData();
}
private void initData() {
for (int i = 0; i < list.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public ProductItemInfo getItem(int arg0) {
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
Viewholder holder=null;
ProductItemInfo productItemInfo=list.get(position);
if(convertView==null){
convertView=View.inflate(context, R.layout.item_fg_seclib,null);
holder=new Viewholder();
holder.itemCbox=(CheckBox)convertView.findViewById(R.id.itemCbox);
holder.itemSn=(TextView)convertView.findViewById(R.id.itemSn);
holder.itemStatus=(TextView)convertView.findViewById(R.id.itemStatus);
convertView.setTag(holder);
}else{
holder=(Viewholder) convertView.getTag();
}
holder.itemCbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
getIsSelected().put(position,arg0.isChecked());
notifyDataSetChanged();
}
});
if(position%2==1){
convertView.setBackgroundColor(context.getResources().getColor(R.color.gray_verylight));
}else{
convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
}
holder.itemCbox.setChecked(getIsSelected().get(position));
String status=null;
if(productItemInfo.getStatus()==false){
status = context.getResources().getString(R.string.un_upload);
}else{
status=context.getResources().getString(R.string.upload);
}
holder.itemStatus.setText(status);
return convertView;
}
public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
SecUpLoadAdapter.isSelected = isSelected;
}
/**
* 设置取消或全选
* @param flag
*/
public void setOptionAll(boolean flag){
for (int i = 0; i < isSelected.size(); i++) {
isSelected.put(i, flag);
}
notifyDataSetChanged();
}
}
3.Activity里调用
cbCheckAll=(CheckBox)rootView.findViewById(R.id.secLibCbox);
ArrayList<ProductItemInfo> data=new ArrayList<ProductItemInfo>();
for (int i = 0; i < 1000; i++) {
ProductItemInfo info=new ProductItemInfo();
info.setId(i);
info.setName("商品"+i);
info.setSelected(false);
info.setSn("1000"+i);
if(i%2==1){
info.setStatus(false);
}else{
info.setStatus(true);
}
data.add(info);
}
adapter = new SecUpLoadAdapter(ac, data);
ArrayAdapter<String> adapter2=new ArrayAdapter<String>(ac, android.R.layout.simple_list_item_1,data2);
unUploadLv.setAdapter(adapter);
cbCheckAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
adapter.setOptionAll(arg0.isChecked());
}
});
这篇关于listView隔行换色多选的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!