ListView点击Item改变文字颜色和背景颜色

github地址(完整Demo)

https://github.com/zhouxu88/ListViewItemClick

效果图

ListView点击Item改变文字颜色和背景颜色_第1张图片
ListViewitem点击改变背景.gif

实现逻辑

ListView的适配器用的是BaseAdapter,当点击Item的时候会触发setOnItemClickListener()方法,同时也会执行adapter中的getView()方法,而且是setOnItemClickListener------->getView,
注意:数据源有多少个,每次点击事件,getView会被执行多少次,
于是只需要2步就可完成逻辑

在Adapter中
1、 private int mSelect; //选中项

2、设置被选中项的文字颜色和背景颜色

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if (mSelect == position) {
            Log.i(TAG, "第:   " + position + " 项被点击了");
            holder.titleTv.setEnabled(true);
            //选中项背景
            holder.titleLayout.setBackgroundColor(Color.RED);
        } else {
            holder.titleTv.setEnabled(false);
            //其他项背景
            holder.titleLayout.setBackgroundColor(Color.YELLOW);
        }
}

完整代码

ublic class MyAdapter extends BaseAdapter {

    private static final String TAG = "TitleFragment";
    private List mList;
    private LayoutInflater inflater;
    private int mSelect;   //选中项

    public MyAdapter(Context context, List mList) {
        this.mList = mList;
        inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_title_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置相关数据
        String title = mList.get(position);
        holder.titleTv.setText(title);
        Log.i(TAG, "getView:     " + position + "-------->被执行了");
        if (mSelect == position) {
            Log.i(TAG, "第:   " + position + " 项被点击了");
            holder.titleTv.setEnabled(true);
            //选中项背景
            holder.titleLayout.setBackgroundColor(Color.RED);
        } else {
            holder.titleTv.setEnabled(false);
            //其他项背景
            holder.titleLayout.setBackgroundColor(Color.YELLOW);
        }
        return convertView;
    }

    //刷新方法
    public void changeSelected(int positon) {
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

    private class ViewHolder {
        private LinearLayout titleLayout;
        private TextView titleTv;

        private ViewHolder(View view) {
            titleLayout = (LinearLayout) view.findViewById(R.id.title_layout);
            titleTv = (TextView) view.findViewById(R.id.title_tv);
        }
    }
}

Fragment

 titleLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Log.i(TAG, "第:   " + position + " 项被点击了");
                mAdapter.changeSelected(position);
                EventBus.getDefault().post("第" + position + "项详情");
            }
        });

你可能感兴趣的:(ListView点击Item改变文字颜色和背景颜色)