如何在网格视图项目中进行多选功能?

时间:2013-10-28 11:31:35

标签: android

我已经制作了网格视图,我必须在其中进行多项选择。但我不想要长按功能。我只想在单击时可以选择多个项目。网格视图正在片段下。

这是我的片段类:

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类:

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","","","","","","",""};
 }

任何建议将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:4)

这是您需要做的粗略解决方案:

1)维护一个列表,其中包含当前所选项目的位置。

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

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

    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)你必须更新视图,以显示是否选择了项目,我将添加代码(+注释)来执行该操作。

3)最后,列表将包含所有已选择的项目。

以下代码显示了上述代码的放置位置。

代码:

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","","","","","","",""};
 }

<强>更新: 要更新视图,您应该阅读有关如何以编程方式更改视图属性的信息。例如,如果要更改背景颜色:

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