如何做自来水网格视图项目多选功能? [英] How to do multiselect functionality on grid view items on tap?

查看:149
本文介绍了如何做自来水网格视图项目多选功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我要做items.But的多重选择我不想长按functionality.I只是想上一个水龙头的多个项目可以selected.Grid的观点是在片段网格视图。

I have made grid view in which I have to do multiple selection of items.But I dont want long tap functionality.I simply want that on single tap the multiple items can be selected.Grid view is under fragment.

这是我的片段类:

public class FragmentOrder extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //View view = inflater.inflate(R.layout.g, null);
        View view = inflater.inflate(R.layout.gridview,null);
        final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
        listView.setAdapter(new OrderGridViewAdapter(MainActivity.this));
        //int setSelected = 0;
        listView.setSelected(true);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub

            }
        });
        return view;
    }
}    

这是我的Adapter类:

This is my Adapter class:

public class OrderGridViewAdapter extends BaseAdapter{
    private Context MContext;

    public OrderGridViewAdapter(Context C){
        MContext = C;

    }


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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View myView;


        LayoutInflater inflater = (LayoutInflater)MContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        myView = inflater.inflate(R.layout.grid_items_ontap, null);


        // Add The Image!!!           
        ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
        iv.setImageResource(mThumbIds[position]);


        // Add The Text!!!
        TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
        tv.setText(names[position] );




        return myView;
    }


    private Integer[] mThumbIds = {
            R.drawable.car, R.drawable.car,
            R.drawable.car, R.drawable.car,
            R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
            R.drawable.car, R.drawable.car,
            R.drawable.car,R.drawable.car,R.drawable.car
    };

    private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
 }

任何建议将AP preciated。
谢谢你。

Any suggestion will be appreciated. Thanks.

推荐答案

这是你需要做一个粗略的解决方案:

This is a rough solution of what you need to do:

1)维护,这将包含当前选定项目的位置的列表。

1) Maintain a list which will contain the positions of currently selected items.

private ArrayList<Integer> mSelected = new ArrayList<Integer>();

当你点击的项目(选择项),将其添加到列表中。当你的项目再次单击(取消选择的项目),从列表中删除。

When you click on item (select item), add it to the list. When you click on item again (deselect item), remove from the list.

    public void onItemSelect(AdapterView<?> parent, View v, int pos, long id) {
        Integer position = new Integer(pos);
        if(mSelected.contains(position)) {
            mSelected.remove(position); // remove item from list
            // update view (v) state here
            // eg: remove highlight
        }
        else {
            mSelected.add(position); // add item to list
            // update view (v) state here
            // eg: add highlight
        } 
    }

2)你必须更新视图,如果选择与否项显示,我会加code(+评论)在哪里做。

2) You have to update the view, to show if item is selected or not, I will add code (+comments) on where to do that.

3)在结束时,列表将包含已选择的所有项目。

3) In the end, the list will contain all items which have been selected.

下面是code这表明你在哪里放置上面code。

Here is the code which shows you where to place the above code.

code:

public class FragmentOrder extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //View view = inflater.inflate(R.layout.g, null);
        View view = inflater.inflate(R.layout.gridview,null);
        final GridView listView = (GridView) view.findViewById(R.id.mainGrid);
        final OrderGridViewAdapter adapter = new OrderGridViewAdapter(MainActivity.this)
        listView.setAdapter(adapter);
        //int setSelected = 0;
        listView.setSelected(true);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                adapter.onItemSelect(arg0, arg1, arg2, arg3);
            }
        });
        return view;
    }
}

适配器:

public class OrderGridViewAdapter extends BaseAdapter{
    private Context MContext;
    private ArrayList<Integer> mSelected = new ArrayList<Integer>();

    public OrderGridViewAdapter(Context C){
        MContext = C;
    }


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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public void onItemSelect(AdapterView<?> parent, View v, int pos, long id) {
        Integer position = new Integer(pos);
        if(mSelected.contains(position)) {
            mSelected.remove(position);
            // update view (v) state here
            // eg: remove highlight
        }
        else {
            mSelected.add(position);
            // update view (v) state here
            // eg: add highlight
        } 
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View myView;


        LayoutInflater inflater = (LayoutInflater)MContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
        myView = inflater.inflate(R.layout.grid_items_ontap, null);


        // Add The Image!!!           
        ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
        iv.setImageResource(mThumbIds[position]);


        // Add The Text!!!
        TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
        tv.setText(names[position] );

        // Set view highlight here, based on if it is selected or not.. 
        if(mSelected.contains(position)) { 
            // update view (v) state here
            // eg: add highlight
        }
        else {
            // update view (v) state here
            // eg: remove highlight
        }


        return myView;
    }


    private Integer[] mThumbIds = {
            R.drawable.car, R.drawable.car,
            R.drawable.car, R.drawable.car,
            R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
            R.drawable.car, R.drawable.car,
            R.drawable.car,R.drawable.car,R.drawable.car
    };

    private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
 }

更新
要更新视图,你应该阅读有关如何以编程方式更改视图的属性。例如,如果你想改变背景颜色:

Update: To update the view, you should read about how to change properties of the view programatically. For example, if you want to change the background color:

v.setBackgroundColor(Color.parseColor("#000000")); // change to black

这篇关于如何做自来水网格视图项目多选功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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