2017-02-16 103 views
1

我有Android程序,但我的列表视图无法正常工作。 如果列表视图有更多的项目与滚动,当我要点击最后一个复选框与滚动检查另一个项目。请找到我的代码sample.please帮我解决这个问题。ListView与复选框滚动问题

这我的getView代码适配器类

@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; 
    } 

这是layout.xml我用它为Android

<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> 
+2

我认为你的问题有很多问题和解决方案。尝试搜索 –

回答

0

为此,你需要保持一个HashMap。当你点击复选框在hashmap中放置位置并在getview之前设置复选框时在hashmap中检查位置。如果存在,然后选中复选框。

0

请查看以下链接: 这是你的另一个项目的复选框上滚动检查的问题的具体解决方案:()在你的列表视图适配器类getView

http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html

在上面的链接示例方法添加以下代码:

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. 
       } 
      }); 

如果能帮助您解决问题,请接受答案。

+0

尚未解决我的问题 –