[英]ListView with CheckBox Scrolling Issue
我有android程序,但我的列表視圖無法正常工作。 如果列表視圖有更多項目滾動當我要點擊最后一個復選框滾動它檢查另一個項目。 請找我的代碼sample.please幫我解決這個問題。
這是我的getView for adapter類的代碼
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.unsettled_invoice_layout, null);
holder = new ViewHolder();
holder.desc = (TextView) vi.findViewById(R.id.usinvoicedesc);
holder.chkbox = (CheckBox) vi.findViewById(R.id.selected);
holder.chkbox.setOnClickListener(new OnItemClickListener(position));
vi.setTag(holder);
vi.setTag(R.id.selected, holder.chkbox);
} else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0){
holder.desc.setText("No Data");
}else{
tempValues=null;
tempValues = (Invoice)data.get(position);
if(mode == 1){
int myColor = Color.rgb(0, 128, 0);
if(tempValues.getStatus() == 2){
myColor = Color.rgb(0, 128, 0); // Green
holder.chkbox.setChecked(false);
}else if(tempValues.getStatus() == 3){
myColor = Color.rgb(128, 0, 0); // Red
holder.chkbox.setChecked(true);
}
holder.desc.setBackgroundColor(myColor);
}
holder.desc.setText(tempValues.toString());
vi.setOnClickListener(new OnItemClickListener( position));
}
return vi;
}
這是我用於android的layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingTop="0dip"
android:textColor="#000000" >
<LinearLayout
android:id="@+id/usinvoicelayout"
android:layout_width="fill_parent"
android:layout_height="95dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="2dip"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:paddingTop="0dip" >
<TextView
android:id="@+id/usinvoicedesc"
android:layout_width="520dp"
android:layout_height="90dp"
android:layout_gravity="start"
android:gravity="center_vertical"
android:textColor="#FFFF00"
android:textSize="18sp" />
<CheckBox
android:id="@+id/selected"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_marginStart="15dip"
android:button="@drawable/checkbox_background"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
為此,您需要維護一個hashmap。 當您單擊復選框放置位置在hashmap和getview中之前設置復選框選中檢查位置在hashmap中。 如果存在,則選中復選框。
請檢查以下鏈接:它是滾動時選中的另一個項目復選框的特定解決方案:
http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html
在上面的listview中的鏈接示例適配器類getView()方法添加下面的代碼:
viewHolder.checkbox.setTag(position); // This line is important.
對於復選框,請單擊以下代碼添加:
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.