Android gridview keep item selected

后端 未结 6 1441
失恋的感觉
失恋的感觉 2020-11-29 06:18

I have a GridView with multiple items, but the items must be kept selected once the the onClickListener is called.How can i achive this?

I\'v already tried v.s

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 06:42

    The concept that you want to achieve is possible, but not like the way you are working now.

    The best and easiest solution would be to keep track of the states of the clicked items and give them the correct layout inside the adapter. I have set up a little example:

    Activity

    public class StackOverFlowActivity extends Activity {
        GridView gridView;
        MyCustomAdapter myAdapter;
        ArrayList myObjects;
    
        static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
                "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z" };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            myObjects = new ArrayList();
            for (String s : numbers) {
                myObjects.add(new GridObject(s, 0));
            }
    
            gridView = (GridView) findViewById(R.id.gridView1);
    
            myAdapter = new MyCustomAdapter(this);
    
            gridView.setAdapter(myAdapter);
            gridView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView arg0, View v, int position, long arg3) {
                    myObjects.get(position).setState(1);
                    myAdapter.notifyDataSetChanged();
                }
            });
        }
    
        static class ViewHolder {
            TextView text;
        }
    
        private class MyCustomAdapter extends BaseAdapter  {
    
            private LayoutInflater mInflater;
    
            public MyCustomAdapter(Context context) {
                mInflater = LayoutInflater.from(context);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                GridObject object = myObjects.get(position);
                ViewHolder holder;
    
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
                    holder = new ViewHolder();
                    holder.text = (TextView) convertView.findViewById(R.id.text);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
    
                holder.text.setText(object.getName());
    
                if (object.getState() == 1) {
                    holder.text.setBackgroundColor(Color.GREEN);
                } else {
                    holder.text.setBackgroundColor(Color.BLUE);
                }
                return convertView;
            }
    
            @Override
            public int getCount() {
                return myObjects.size();
            }
    
            @Override
            public Object getItem(int position) {
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
        }
    }
    

    GridObject

    public class GridObject {
    
        private String name;
        private int state;
    
        public GridObject(String name, int state) {
            super();
            this.name = name;
            this.state = state;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getState() {
            return state;
        }
    
        public void setState(int state) {
            this.state = state;
        }   
    }
    

    Main.xml

    
    
    
        
        
    
    
    

    list_item_icon_text

    
    
    
        
    
    
    

提交回复
热议问题