Gmail 样式列表视图 [英] Gmail style listview

查看:19
本文介绍了Gmail 样式列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个功能类似于 Gmail android 应用程序的列表视图.我的意思是,您可以通过单击左侧的图像来选择行,也可以通过单击行上的任何其他位置来查看电子邮件.我可以接近,但还不够.

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 和右侧的一些 TextView 组成.这是我的 Adapter 上 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 功能.不幸的是,我从未实施过.所以,不能给你一个详细的答案.但您可以从此处获得提示和其他答案.

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:自行实施.定义一个 array/list 或任何保留列表中所选元素索引的变通方法.然后用它来过滤 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;
}

祝你好运!

这篇关于Gmail 样式列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆