2014-10-10 19 views
0

我有listview和list_item佈局包含一個imageview,textview和複選框。我的adapater包含sectionheader。當我點擊listview項目使該複選框可見時,當我再次點擊該項目使複選框不可見。管理複選框在ListView上的可見性onItemClick

 ListView lv = (ListView) findViewById(R.id.friendsList); 
     lv.setFastScrollEnabled(true); 
     fbAdapter = new FbFriendsAdapter(this, 0); 
     lv.setAdapter(fbAdapter); 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 

       fbAdapter.getSelectedFriend(); 

       String s = (String) ((TextView) view 
       .findViewById(R.id.friendName)).getText(); 
       CheckBox cb = (CheckBox) view.findViewById(R.id.iv_check); 
       cb.setVisibility(View.VISIBLE); 

       cb.setChecked(true); 

       if (cb.isChecked()) { 

        Toast.makeText(FbFriendsListActivity.this, "selected " + s, 
       Toast.LENGTH_SHORT).show(); 
       } 
       else{ 
        //not working when i tried . setvisibilty gone of checkbox. 

       } 
      }); 

listitemlayout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <com.hashmash.utils.RoundedImageView 
      android:id="@+id/friendPhoto" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:src="@drawable/fb_profileimgsm" 
      android:scaleType="centerCrop" 
      app:corner_radius="80dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="30dp" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/friendName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:text="#Name" 
       android:textColor="@color/instablue" 
       android:textSize="14sp" /> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="right" > 

       <CheckBox 
        android:id="@+id/iv_check" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:focusable="false" 
        android:visibility="visible" /> 
      </RelativeLayout> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

回答

0

我已經得到它使用下面的代碼段。

if (view.findViewById(R.id.iv_check).getVisibility() == View.VISIBLE) { 
        view.findViewById(R.id.iv_check).setVisibility(View.INVISIBLE); 
       } else { 
        view.findViewById(R.id.iv_check).setVisibility(View.VISIBLE); 


       } 
0

你之前檢查複選框,如果else塊。

cb.setChecked(true); 

if (cb.isChecked()) { 

    Toast.makeText(FbFriendsListActivity.this, "selected " + s, 
       Toast.LENGTH_SHORT).show(); 
}   
else{ 
    cb.setVisibility(View.INVISIBLE); //or GONE if you don't need blank space occupied by checkbox 
} 

所以你的複選框cb總是被選中,否則永遠不會被執行。

只是刪除

cb.setChecked(true); 

,它應該工作的罰款。

0

我認爲你應該有一個HashMap<Integer, Boolean>ArrayListSparseBooleanArrayCheckBox可見的項目。例如:

HashMap<Integer, Boolean> checkedItems = new HashMap<Integer, Boolean>(); 

當項目用戶點擊:根據本HashMap

lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      HashMap<Integer, Boolean> checkedItems = new HashMap<Integer, Boolean>(); 
      if (!checkedItems.containsKey(position)) 
       checkedItems.put(position, false); 

      checkedItems.put(position, !checkedItems.get(position)); 

      mAdapter.notifyDataSetChanged(); 
     }); 

而在你的適配器顯示checkbox

如果你這樣做,它會在保證。滾動時Android重新繪製列表項。所以,即使你的方式正確,複選框也將不再使用滾動。