本文介绍了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?

Saying that, do you actually need 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多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:13