如何在Android中单击CustomAdapter类外部的切换按钮来检查和取消选中getView()内的复选框?

时间:2015-03-19 15:46:58

标签: android android-listview baseadapter android-checkbox android-togglebutton

我有一个具有切换按钮和列表视图的布局。切换按钮位于listview.Listview包含文本和复选框之外。如何点击android中的切换按钮,检查并取消选中CustomAdapter类的getView()内的复选框。

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >



    <LinearLayout
        android:id="@+id/linlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

    <LinearLayout
        android:id="@+id/linlay1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@drawable/border_set" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linlay2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Linlay1"
        android:background="#ffffff"
        android:gravity="center_vertical" 
        >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Notifications"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />



        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <ToggleButton
                android:id="@+id/toggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:checked="true"
                android:text="ToggleButton" />

        </LinearLayout>

     </LinearLayout>


     <LinearLayout
        android:id="@+id/linlay3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Linlay2"
        android:background="#ffffff"
        android:orientation="vertical" >

         <TextView
             android:layout_width="fill_parent"
             android:layout_height="2dp"
             android:background="@drawable/border_set" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Please choose your area of intrest"
            android:textSize="16sp"
            android:textColor="#000000"/>



     </LinearLayout>


    <LinearLayout
        android:id="@+id/linlay4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/linlay3"
        android:orientation="vertical" 
        android:background="@drawable/border_set">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:listSelector="@android:color/transparent" />

    </LinearLayout>


    </LinearLayout>

</RelativeLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RLfirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:paddingBottom="3dp"
    android:paddingTop="3dp" >

    <RelativeLayout
        android:id="@+id/RLthird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="title"
            android:textColor="#0000ff"
            android:textSize="15sp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RLsecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

        <ImageView
            android:id="@+id/RightArrowimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:scaleType="fitXY"
            android:visibility="invisible"
            android:layout_alignParentRight="true"
            android:src="@drawable/red_arrow_small" />

        <CheckBox
        android:id="@+id/chk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:scaleX=".60"
        android:scaleY=".60" 
        android:layout_below="@id/RightArrowimg" 
        android:enabled="true"/>
    </RelativeLayout>
</RelativeLayout>

Test.class

    public class Test extends Activity{
    ListView listView1;
    ToggleButton toggleButton;
    Boolean flag=true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            listView1 = (ListView) findViewById(R.id.listView1);
            toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
            toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if (isChecked) {
                        flag=true;

                    } else {
                        flag=false;

                    }

                }
            });
            ArrayList<String> arr=new ArrayList<String>();
            arr.add("a");
            arr.add("b");

            MyAdapter madap = new MyAdapter(Test.this,arr);
            listView1.setAdapter(madap);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public class MyAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private Context cont;
        int layout_xml;
        ArrayList<String> arr;

        public MyAdapter(Context context, ArrayList<String> arr) {
            this.cont = context;
            this.arr = arr;
            mInflater = LayoutInflater.from(context);
        }
        public View getView(final int position, View convertView,ViewGroup parent) {
            try {

                if (convertView == null) {
                    layout_xml = R.layout.list_row;
                    convertView = mInflater.inflate(layout_xml, null);
                }

                TextView tvtitle = (TextView) convertView.findViewById(R.id.tv_title);
                ImageView RightArrowimg=(ImageView)convertView.findViewById(R.id.RightArrowimg);
                final CheckBox chk=(CheckBox)convertView.findViewById(R.id.chk);

                chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(flag){

                            chk.setEnabled(true);
                            chk.setClickable(true);

                        }else{

                            chk.setEnabled(false);
                            chk.setClickable(false);
                            chk.setChecked(false);

                        }

                    }
                });


            } catch (Exception e) {
                e.printStackTrace();
            }


            return convertView;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public int getCount() {

            return arr.size();

        }

        @Override
        public Object getItem(int position) {
            // return position;
            return null;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

没有任何代码,我只能猜测你在做什么......

如果你有某种ArrayList,其中包含ListView中每个复选框的状态,你可以将each的状态设置为false / true,然后使用ArrayAdapters notifyDataSetChanged()来更新列表视图数据。