手记

ListView 点击选中行文字颜色、背景变化

创建ListView的适配器ListViewAdapter继承BaseAdapter;在ListViewAdapter添加一个方法

public void setSelectPosition(int position){    this.mPosition = position;}
mPosition 在ListView的监听事件中传入。
lvLists.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        listsAdapter.setSelectPosition(position);        listsAdapter.notifyDataSetChanged();}
 @Override    public View getView(final int position, View convertView, ViewGroup parent) {        if (mPosition == position){
            convertView.setBackgroundColor(Color.WHITE);            viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.myColor));        }else {
            convertView.setBackgroundColor(context.getResources().getColor(R.color.myGrey));            viewHolder.tvName.setTextColor(Color.BLACK);        }        return convertView;    }选中背景及文字颜色就会发生改变啦

原文链接:http://www.apkbus.com/blog-721408-75907.html

0人推荐
随时随地看视频
慕课网APP