Android GridView 多选 [英] Android GridView Multiple Selection

查看:24
本文介绍了Android GridView 多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现并激活了 GridView

I have a GridView implemented and activated the

mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);

模式.现在,当我长按一个项目时,我可以从我的网格中选择多个项目.我想通过正常的短点击来实现这种行为.这可能吗?

mode. Now I have the possibility to select multiple items from my grid when I perform a long click on one item. I want to achieve this behavior on a normal, short click. Is this possible?

推荐答案

首先,我建议您考虑一下这个用户场景是否是您一直在寻找的.默认情况下,在 Android UX 中选择您长按的内容,这是用户习惯的模式.所以,也许你应该重新考虑整个流程.

First, I'd suggest to think if this user scenario is what you have been looking for. By default, in Android UX to select something you do long press and it's a pattern users used to. So, maybe you should rethink the whole flow.

话说,你真的需要GridView.CHOICE_MODE_MULTIPLE_MODAL吗?

您可以在适配器级别处理它,只需存储选定的位置并在 onClick 处理程序中更新此列表:

You can handle it on the Adapter level, by just storing selected positions and update this list in onClick handler:

static final String[] numbers = new String[] {
        "A", "B", "C", "D", "E",....
        "U", "V", "W", "X", "Y", "Z"};

.....

gridView = (GridView) findViewById(R.id.gridView1);
final CustomAdapter adapter = new CustomAdapter(numbers);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
        int selectedIndex = adapter.selectedPositions.indexOf(position);
        if (selectedIndex > -1) {
            adapter.selectedPositions.remove(selectedIndex);
            ((CustomView)v).display(false);
        } else {
            adapter.selectedPositions.add(position);
            ((CustomView)v).display(true);
        }
    }
});

自定义 BaseAdapter 以显示自定义视图:

Custom BaseAdapter to display Custom views:

public class CustomAdapter extends BaseAdapter {
    private String[] strings;
    List<Integer> selectedPositions = new ArrayList<>();

    CustomAdapter(String [] strings) {
        this.strings = strings;
    }

    @Override
    public int getCount() {
        return strings.length;
    }

    @Override
    public Object getItem(int position) {
        return strings[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CustomView customView = (convertView == null) ? 
                                    new CustomView(MainActivity.this) : (CustomView) convertView;
        customView.display(strings[position], selectedPositions.contains(position));
        return customView;
    }
}

自定义视图(在我的情况下 - 带有 TextView 的单元格).xml:

Custom View (in my case - cell with TextView). Xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/textView"
        android:textColor="#FFF"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
</merge>

代码:

class CustomView extends FrameLayout {

    TextView textView;

    public CustomView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.custom_view, this);
        textView = (TextView)getRootView().findViewById(R.id.textView);
    }

    public void display(String text, boolean isSelected) {
        textView.setText(text);
        display(isSelected);
    }

    public void display(boolean isSelected) {
        textView.setBackgroundColor(isSelected? Color.RED : Color.LTGRAY);
    }
}

这篇关于Android GridView 多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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