如何突出显示或检查所选列表项

时间:2017-01-02 12:22:21

标签: android listview gridview contextual-action-bar multichoiceitems

如何在Android中的多项选择模式模式中使用Java或XML代码突出显示(或显示复选框)列表行或网格项以与上下文操作栏一起使用?

我已经实现了上下文操作栏和ListView / GridView,我可以选择它们并在所选项目上运行功能,但除了长按一下列表行/项目的简要突出显示外没有视觉反馈它,当它被释放时消失。

我的第一个想法是设置适配器中行/项的背景颜色,但我似乎无法让它工作。我也尝试过这个问题的接受答案建议的解决方案:Android ListView Multi-Choice don't show highlight after chlicking但它对我的ListView和GridView的行为没有任何影响。

我最感兴趣的是根据材料设计指南和/或最常见的方式了解标准方法。提前感谢您提出任何建议或解决方案。

修改

我尝试了Redman的回答(事实上类似于它,因为我使用了Contextual Action Mode和一个多选择的监听器),但我没有得到任何结果。这就是我在听众中所做的:

public void onItemCheckedStateChanged(ActionMode actionMode, int i, long id, boolean checked) {
            if (checked) {
                selectedItems.add(listAdapter.getItem(i));
                ((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(true);
            }
            else {
                selectedItems.remove(listAdapter.getItem(i));
                ((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(false);
            }
        }

它运行没有错误,但它没有对复选框做任何事情,所以我不确定问题是什么。任何帮助都非常感谢。

5 个答案:

答案 0 :(得分:2)

Okk对于多项选择你需要Model,Adapter。所以你必须制作customlistview。我在下面给出了一个例子。你只需要将markAttendance_detail(TextView)替换为复选框,如果它是' P&#39 ;设置复选框检查否则取消选中。如果您遇到任何问题,请告诉我。

Adaper Class - >

public class AttendanceAdapter extends ArrayAdapter {

    List list = new ArrayList();

    static class DataHolder {
        TextView rollNo_detail,studentName_detail,markAttendance_detail;
    }

    public AttendanceAdapter(Context context, int resource) {
        super(context, resource);
    }
    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }
    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        final DataHolder dataHolder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.attendance_list, parent, false);
            dataHolder = new DataHolder();

            dataHolder.rollNo_detail=(TextView)row.findViewById(R.id.student_rollno);
            dataHolder.studentName_detail=(TextView)row.findViewById(R.id.student_name);
            dataHolder.markAttendance_detail=(TextView)row.findViewById(R.id.mark_attendance);
            dataHolder.markAttendance_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AttendanceModel model=(AttendanceModel) dataHolder.markAttendance_detail.getTag();
                    String att=model.getAttendance().toString();
                    if (model.isSelected()==false){
                        model.setSelected(true);
                        dataHolder.markAttendance_detail.setText("P");
                    }else if (model.isSelected()==true){
                        model.setSelected(false);
                        dataHolder.markAttendance_detail.setText("A");
                    }
                }

            });
            row.setTag(dataHolder);
            dataHolder.markAttendance_detail.setTag(list.get(position));

        }
        else {
            dataHolder = (DataHolder) row.getTag();
            ((DataHolder) row.getTag()).markAttendance_detail.setTag(list.get(position));

        }
        AttendanceModel model = (AttendanceModel) this.getItem(position);

        dataHolder.rollNo_detail.setText(model.getRoll_no());
        dataHolder.studentName_detail.setText(model.getStudent_name());
        if (model.isSelected()==true){
           dataHolder.markAttendance_detail.setText("P");
       }else if (model.isSelected()==false){
            dataHolder.markAttendance_detail.setText("A");
        }
        return row;
    }

模型应该是这样的 - >

public class AttendanceModel {
    private String roll_no;
    private String student_name;
    private String attendance;
    private boolean selected;
    public AttendanceModel(String roll_no,String student_name,String attendance){
        this.roll_no=roll_no;
        this.selected=true;
        this.student_name=student_name;
        this.attendance=attendance;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getRoll_no() {
        return roll_no;
    }

    public void setRoll_no(String roll_no) {
        this.roll_no = roll_no;
    }

    public String getStudent_name() {
        return student_name;
    }

    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }

    public String getAttendance() {
        return attendance;
    }

    public void setAttendance(String attendance) {
        this.attendance = attendance;
    }
}

活动应该是这样的 - >

public class Attendance_Page extends AppCompatActivity {
    ListView listView;
   AttendanceModel model;
    AttendanceAdapter attendanceAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atendance_page);
        listView= (ListView) findViewById(R.id.attendancepage_list_view);
    attendanceAdapter=new AttendanceAdapter(getApplicationContext(),R.layout.attendance_list);
       listView.setAdapter(attendanceAdapter);
        for (int i=1;i<=10;i++){
            model=new AttendanceModel("1","Aryan","P");
            attendanceAdapter.add(model);
        }
    }
}

从我的观点来看,如果不让我知道,这可能是解决方案。

答案 1 :(得分:1)

“我最感兴趣的是根据材料设计指南和/或最常见的方式了解标准方法。”

我不确定我的答案是材料设计还是常见但我已经完成了基于GridView的Google“照片”应用中的选择处理。我测试了它,并知道它将包括突出显示的边框和“复选框”。

**免责声明**

我考虑使用选择器,但如果您的视图滚动,则似乎存在问题(请参阅this)。此外,我使用了自定义复选框(实际上只有两个不同的drawable),部分原因是this。此外,这个答案基本上只处理纵向模式的手机。需要添加更多来处理不同的配置(屏幕大小,方向等)

这里有很多,但就像我说的,我已经测试过了。整个项目已发布到GitHub

1,在MainActivity onCreate()

mList = new ArrayList<>();

// fill your list here

mAdapter = new GridAdapter(getApplicationContext(), R.layout.grid_item, mList);
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d(TAG, "item clicked = " + i);
        boolean isSelected = mList.get(i).isSelected();
        mList.get(i).setSelected(!isSelected);

        mAdapter.notifyDataSetChanged();
    }
});

接下来,在你的适配器中覆盖getView()(我正在使用ArrayAdapter)

@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {

    RelativeLayout itemLayout;

    GridItem gridItem = (GridItem)getItem(position);

    // use existing Views when we can
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemLayout = (RelativeLayout) inflater.inflate(R.layout.grid_item, null);
    } else {
        itemLayout = (RelativeLayout) convertView;
    }

    // get your bitmap or list item etc. here
    BitmapDrawable bitmap = gridItem.getBitmap();
    ImageView imageView = (ImageView)itemLayout.findViewById(R.id.image_view);
    imageView.setBackground(bitmap);

    if(gridItem.isSelected()) {
        final int PADDING = 16;

        Log.d(TAG, "position " + position + " is selected");
        itemLayout.findViewById(R.id.image_frame).setPadding(PADDING, PADDING, PADDING, PADDING);
        itemLayout.findViewById(R.id.custom_check_box)
                .setBackgroundResource(R.drawable.custom_check_box_selected);
    } else {
        Log.d(TAG, "postion " + position + " is NOT selected");
        itemLayout.findViewById(R.id.image_frame).setPadding(0,0,0,0);
        itemLayout.findViewById(R.id.custom_check_box)
                .setBackgroundResource(R.drawable.custom_check_box_unselected);
    }
    return itemLayout;
}

这是GridItem类的核心,省略了getter&amp; setter方法。

public class GridItem {

    private BitmapDrawable bitmap = null;
    private boolean isSelected = false;

    public GridItem(BitmapDrawable bitmap) {
        this.bitmap = bitmap;
    }
}

这就是Java。现在为一些xml

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    >
    <FrameLayout
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:id="@+id/image_frame"
        >
        <!-- view for the main image -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:background="@android:color/white"
            android:id="@+id/image_view"
            />
    </FrameLayout>

    <!-- view for the 'checkbox' in upper left corner -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:src="@drawable/custom_check_box_unselected"
        android:id="@+id/custom_check_box"
        />
</RelativeLayout>

这将在content_main.xml或activity_main.xml等中进行。

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="20dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:id="@+id/grid_view"
    >
</GridView>

现在有2个可绘制文件夹的文件。

custom_check_box_unselected.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="20dp"
    android:width="20dp"

    android:viewportWidth="400"
    android:viewportHeight="400">

    <!-- the outside box -->
    <!-- top line & top left corner -->
    <path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35 "
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- left line & bottom left corner -->
    <path android:pathData="M 27 64 v271 c0 20 15 35 35 35 "
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- bottom line & bottom right corner -->
    <path android:pathData="M 60 370 h275 c20 0 35 -15 35 -35"
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- right line & top right corner -->
    <path android:pathData="M 370 336 v -271 c0 -20 -15 -35 -35  -35"
        android:strokeColor="#000000" android:strokeWidth="20" />
</vector>

custom_check_box_selected.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="20dp"
    android:width="20dp"

    android:viewportWidth="400"
    android:viewportHeight="400">

    <!-- the outside box -->
    <path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35
            v271 c0 20 15 35 35 35
            h275 c20 0 35 -15 35 -35
            v -271 c0 -20 -15 -35 -35  -35 "
        android:fillColor="#FDD835"  android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- the check mark -->
    <path android:pathData="M 140 320 l -100 -100 25 -30
            l 75 75 l 190 -190
            l 25 30 l -190 190"
        android:fillColor="#000000"  android:strokeColor="#000000" android:strokeWidth="2" />
</vector>

答案 2 :(得分:0)

您可以通过以下方式在列表视图中显示带有多选的复选框

在list child xml中添加一个复选框,并在xml

中将其设置为clickable false
 android:clickable="false"

并为您的列表OnItemSelectedListner保留此

 ArrayList<Integer> listPositionsSaveArray= new ArrayList<Integer>(); 

  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                    CheckBox presentCheckBox=(CheckBox)view.findViewById(R.id.checkbox__list);

                    if(presentCheckBox.isChecked()){
                          presentCheckBox.setChecked(false);
                          listPositionsSaveArray.removeAll(Arrays.asList(position+1));
                    }
                    else{
                    presentCheckBox.setChecked(true);
                    listPositionsSaveArray.add(position+1);
                    }


            }
        });

列表视图的所有选定位置都将在listPositionsSaveArray现在

答案 3 :(得分:0)

根据我的理解,当用户选中该复选框时,您想要更改listview行颜色..

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        for (int j = 0; j < adapterView.getChildCount(); j++)
            adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

        // change the background color of the selected element
        view.setBackgroundColor(Color.LTGRAY);
});

您可以根据自己的要求使用逻辑来实现这种变化的颜色

答案 4 :(得分:-1)

这非常简单。在布局文件中,请在ListView

中使用此属性
android:choiceMode="singleChoice"
android:listSelector="#666666"

如果您想以编程方式执行此操作,请尝试以下操作:

listView.setSelector(Drawable selector)

listView.setSelector(int resourceId)

listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

如果您想要多次选择,请使用AbsListView.CHOICE_MODE_MULTIPLE