带有Checkbox的Android ListView,所有可点击的

cod*_*711 53 checkbox android listview focus

可能重复:
Android:将数据从数据库绑定到ListView中的CheckBox?

我想使用具有以下布局的项目的ListView

------------------------- 
 [CB]    TV            TV
-------------------------
Run Code Online (Sandbox Code Playgroud)

CB是一个复选框,电视是Textview.

现在我读到了一个你无法在ListView中拥有可点击项目的地方.如果你有一些,那么你将无法点击ListItems.但是,如果我查看GoogleMail应用,这是可能的.我可以使用复选框标记多个消息(然后为它们选择一个操作)或者我可以单击ListItem(或使用dpad滚动)以进入另一个屏幕.有人有代码/示例如何可能吗?

Pac*_*Sky 101

在XML布局中设置CheckBoxas focusable="false".否则它将从列表视图中窃取点击事件.

当然,如果你这样做,你需要手动处理标记CheckBox为已选中/未选中如果单击列表项而不是CheckBox,但你可能还是想要它.

  • 设置focusable ="false"复选框允许我单击列表视图项,但它不会阻止我单击复选框本身.为此,我还需要为复选框设置clickable ="false". (30认同)

Eri*_*edt 19

将listview适配器设置为"simple_list_item_multiple_choice"

ArrayAdapter<String> adapter;

List<String> values; // put values in this

//Put in listview
adapter = new ArrayAdapter<UserProfile>(
this,
android.R.layout.simple_list_item_multiple_choice, 
values);
setListAdapter(adapter);    
Run Code Online (Sandbox Code Playgroud)


max*_*ver 9

holder.checkbox.setTag(row_id);
Run Code Online (Sandbox Code Playgroud)

holder.checkbox.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckBox c = (CheckBox) v;

                    int row_id = (Integer) v.getTag();

                    checkboxes.put(row_id, c.isChecked());


                }
        });
Run Code Online (Sandbox Code Playgroud)


Bor*_*rja 7

这段代码适用于我的proyect,我可以选择listview项目和复选框

<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:text="" >
    </CheckBox>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Uda*_*ran 5

下面的代码将帮助您:

public class DeckListAdapter extends BaseAdapter{

      private LayoutInflater mInflater;
        ArrayList<String> teams=new ArrayList<String>();
        ArrayList<Integer> teamcolor=new ArrayList<Integer>();


        public DeckListAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

            teams.add("Upload");
            teams.add("Download");
            teams.add("Device Browser");
            teams.add("FTP Browser");
            teams.add("Options");

            teamcolor.add(Color.WHITE);
            teamcolor.add(Color.LTGRAY);
            teamcolor.add(Color.WHITE);
            teamcolor.add(Color.LTGRAY);
            teamcolor.add(Color.WHITE);


        }



        public int getCount() {
            return teams.size();
        }


        public Object getItem(int position) {
            return position;
        }


        public long getItemId(int position) {
            return position;
        }

       @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;


            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.decklist, null);

                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.deckarrow);
                holder.text = (TextView) convertView.findViewById(R.id.textname);

             .......here you can use holder.text.setonclicklistner(new View.onclick.

                        for each textview


                System.out.println(holder.text.getText().toString());

                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }



             holder.text.setText(teams.get(position));

             if(position<teamcolor.size())
             holder.text.setBackgroundColor(teamcolor.get(position));

             holder.icon.setImageResource(R.drawable.arraocha);







            return convertView;
        }

        class ViewHolder {
            ImageView icon;
            TextView text;



        }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.