实现点击ListView某一item项的选中效果

1 设置ListView的监听,在监听中记录点击的位置,并刷新适配器,adapter.notifyDataSetChanged(),会调用适配器的getView()方法。

private int currentPosition;

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id)
            {
                //变色
                currentPosition = position;
                adapter.notifyDataSetChanged();
              
            }
        });

2 item项的布局文件






其中 leftmenu_item_selector和 leftmenu_item_textcolro_selector选择器 使用enable属性



    
    





    
    



3 在ListView适配器的getView()方法中实现ListView某一item项的选中效果

 @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder viewHolder;
            if (convertView == null)
            {
                viewHolder = new ViewHolder();
                convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
                viewHolder.tv_item_title = (TextView) convertView.findViewById(R.id.tv_item_title);
                convertView.setTag(viewHolder);

            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            NewsCenterBean.DataBean dataBean = data.get(position);
            viewHolder.tv_item_title.setText(dataBean.getTitle());

            if (curPosition == position)
            {
                viewHolder.tv_item_title.setEnabled(true);
            }
            else
            {
                viewHolder.tv_item_title.setEnabled(false);
            }
            return convertView;
        }

        class ViewHolder
        {
            TextView tv_item_title;
        }



你可能感兴趣的:(实现点击ListView某一item项的选中效果)