2

我已经制作了网格视图,我必须在其中选择多个项目。但我不想要长按功能。我只希望单击即可选择多个项目。网格视图位于片段下。

这是我的片段类:

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

这是我的适配器类:

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

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

4

1 回答 1

5

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

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
于 2013-10-28T11:55:13.363 回答