Android 管理listview上的复选框可见性

Android 管理listview上的复选框可见性,android,listview,checkbox,Android,Listview,Checkbox,我有listview和list_项布局,其中包含imageview、textview和复选框。我的adapater包含sectionheader。当我点击listview项目时,使复选框可见,当我再次点击该项目时,使复选框不可见 ListView lv = (ListView) findViewById(R.id.friendsList); lv.setFastScrollEnabled(true); fbAdapter = new FbFrien

我有listview和list_项布局,其中包含imageview、textview和复选框。我的adapater包含sectionheader。当我点击listview项目时,使复选框可见,当我再次点击该项目时,使复选框不可见

        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.

                 }
             });
ListView lv=(ListView)findViewById(R.id.friendsList);
lv.setFastScrollEnabled(真);
fbAdapter=新的FbFriendsAdapter(此为0);
低压设置适配器(fbAdapter);
lv.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//TODO自动生成的方法存根
fbAdapter.getSelectedFriend();
字符串s=(字符串)((文本视图)视图
.findViewById(R.id.friendName)).getText();
复选框cb=(复选框)view.findViewById(R.id.iv_检查);
cb.setVisibility(View.VISIBLE);
cb.setChecked(真);
if(cb.isChecked()){
Toast.makeText(FbFriendsListActivity.this,“选定”+s,
吐司。长度(短)。show();
}
否则{
//当我尝试时不工作。设置Visibility不在复选框中。
}
});
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>

在if-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);

它应该可以正常工作。

我认为对于复选框可见的项目,应该有一个
哈希映射
数组列表
,或者
SparseBooleanArray
。例如:

HashMap<Integer, Boolean> checkedItems = new HashMap<Integer, Boolean>();
HashMap checkedItems=newhashmap();
当用户单击项目时:

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();
         });
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
HashMap checkedItems=新HashMap();
如果(!checkedItems.containsKey(位置))
checkedItems.put(位置,false);
checkedItems.put(位置,!checkedItems.get(位置));
mAdapter.notifyDataSetChanged();
});
在适配器中,根据此
HashMap
显示
复选框


如果你这样做,它将是在保证。Android重画滚动列表项目。因此,即使你在你的方式正确,复选框将在使用滚动中消失。

我使用下面的代码片段获得它

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


                }