How to highlight ListView-Items

后端 未结 4 1721
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 15:30

i got the following problem.

i have a ListView with custom rows consisting of an imageview and a textview. the textview\'s xml code is



        
4条回答
  •  清歌不尽
    2021-02-07 16:17

    In your Activity:

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long itemid) {
                itemOnclickList(position, view);
            }
        });
    
        public void itemOnclickList(int position, View view) {      
        if (listview.isItemChecked(position)) {                           
           view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));            
        } else {    
        view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));           
        }
        adapter.notifyDataSetChanged();
    
    }
    

    In your Adapter:

      public View getView(int position, View view, ViewGroup parent) {
        View convertView = inflater.inflate(R.layout.listdocument_item, null);      
    
            ListView lvDocument = (ListView) parent;
            if (lvDocument.isItemChecked(position)) {
                convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));               
            } else {
                convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));               
            }
    
        return convertView;
    }
    

    Good luck!

提交回复
热议问题