Gmail的样式列表视图视图、样式、列表、Gmail

2023-09-12 22:32:48 作者:阳光男孩

我想创建一个列表视图是在功能上与Gmail的Andr​​oid应用程序相似。我的意思是,你可以通过点击左边的图像选择行或通过单击行上其他任何地方查看电子邮件。我可以接近,但它不是相当有。

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 email by clicking anywhere else on the row. I can come close, but it's not quite there.

我的自定义行由左侧的ImageView的,右边部分TextViews的。这里的getView在我的适配器的要点。

My custom row consists of an ImageView on the left and some TextViews on the right. Here's the gist of the getView on my Adapter.

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListView().setItemChecked(position, !getListView().isItemChecked(position));
            }
        });

        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
            }
        });
     }

这非常接近!现在缺少的是该行点击监听,该行的高亮显示。

This comes very close! What's missing is the highlighting of the row on the row click listener.

推荐答案

选项1 :使用ListView的内置 choiceMode 功能。不幸的是,我从来没有实现。所以,不能给你一个详细的解答。但是,你可以从一个提示here和其他的答案。

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.

选项2 :实现它自己。定义一个阵列 / 列表或任何解决方法,让您的列表中选择元素的索引。然后用它来在getView过滤背景()。以下是一个例子:

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<String> data;
boolean is_element_selected[];

public TestAdapter(List<String> 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;
}

祝你好运!