Gmail style listview

后端 未结 6 1795
情歌与酒
情歌与酒 2020-12-01 22:56

I want to create a listview that is similar in functionality to the Gmail android app. By that I mean that you can select rows by clicking an image on the left or view an em

6条回答
  •  误落风尘
    2020-12-01 23:29

    Option 1: Use listView's inbuilt choiceMode feature. Unfortunately, I've never implemented. So, can't give you a detailed answer. But you can take a hint from here and other answers.

    Option 2: Implement it on your own. Define an array/list or any work-around that keeps indexes of selected element of your list. And then use it to filter backgrounds in getView(). Here is a working example:

    public class TestAdapter extends BaseAdapter {
    
    List data;
    boolean is_element_selected[];
    
    public TestAdapter(List data) {
        this.data = data;
        is_element_selected = new boolean[data.size()];
    }
    
    public void toggleSelection(int index) {
        is_element_selected[index] = !is_element_selected[index];
        notifyDataSetChanged();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Initialize your view and stuff
    
        if (is_element_selected[position])
            convertView.setBackgroundColor(context.getResources().getColor(R.color.blue_item_selector));
        else
            convertView.setBackgroundColor(Color.TRANSPARENT);
    
         imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toggleSelection(position);
                }
            });
    
          row.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //get to detailed view page
                }
            });
    
        return convertView;
    }
    

    Good luck!

提交回复
热议问题