赞
踩
由于项目需求需要点击左侧的listview中的item,右侧随着改变,例如下图是我实现的效果:
今天记录一下,以便以后翻阅.左侧为listvie列表,右侧也为点击左侧listview中的item随着改变。现在主要记录一下左侧listview中点击item背景以及文字颜色的改变。主要有几点:
1、在MainActivity中设置listview的setOnItemClickListener监听 ,以下代码中,主要是调用productNameLisAdapter的两个方法以及对适配器及时更新数据
- productNameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- setReclassifyData(position);//设置listview点击时候右侧二级分类的数据
- productNameLisAdapter.setCurrentItem(position);
- productNameLisAdapter.setClick(true);
- productNameLisAdapter.notifyDataSetChanged();
- }
- });
2、设置ProductNameLisAdapter
- private int mCurrentItem=0;//选中项的下标
- private boolean isClick = false;//判断是否选中
- //判断初始进来选中的下标==当前的下标 如果i=0时设置背景、文字颜色、以及左侧的竖直tab
- if (mCurrentItem == i) {
- viewHolder.textViewProjectName.setTextColor(context.getResources().getColor(R.color.colorOrangeRed));
- viewHolder.textViewLine.setBackgroundColor(context.getResources().getColor(R.color.colorOrangeRed));
- viewHolder.linearLayoutProductNameList.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
- } else {
- viewHolder.textViewProjectName.setTextColor(context.getResources().getColor(R.color.colorBlack));
- viewHolder.textViewLine.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
- viewHolder.linearLayoutProductNameList.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
- }
- public void setCurrentItem(int currentItem) {
- this.mCurrentItem = currentItem;
- }
-
- public void setClick(boolean click) {
- this.isClick = click;
- }
网上有很多代码会设置判断 !isClick但是我代码中设置之后初始进来第一条item的状态是选中正确的,但是在点击item的时候其他状态并没有改变,所以我没有设置,参考的时候看自己的情况
以下为适配器的代码:
- public class ProductNameLisAdapter extends BaseAdapter {
- Context context;
- private List<ClassificationEntity.DataBean.GoodsCategoryListBean> list;
- private int mCurrentItem=0;
- private boolean isClick = false;
-
-
- public ProductNameLisAdapter(Context context, List<ClassificationEntity.DataBean.GoodsCategoryListBean> list) {
- this.context = context;
- this.list = list;
- }
-
- @Override
- public int getCount() {
- return list.size();
- }
-
- @Override
- public Object getItem(int position) {
- return list.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int i, View convertView, ViewGroup viewGroup) {
- ViewHolder viewHolder;
- if (convertView == null) {
- viewHolder = new ViewHolder();
- convertView = View.inflate(context, R.layout.item_project_name_list, null);
- viewHolder.textViewProjectName = convertView.findViewById(R.id.textViewProjectName);
- viewHolder.textViewLine = convertView.findViewById(R.id.textViewLine);
- viewHolder.linearLayoutProductNameList = convertView.findViewById(R.id.linearLayoutProductNameList);
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
-
- viewHolder.textViewProjectName.setText(list.get(i).getShort_name());
- if (mCurrentItem == i) {
- viewHolder.textViewProjectName.setTextColor(context.getResources().getColor(R.color.colorOrangeRed));
- viewHolder.textViewLine.setBackgroundColor(context.getResources().getColor(R.color.colorOrangeRed));
- viewHolder.linearLayoutProductNameList.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
- } else {
- viewHolder.textViewProjectName.setTextColor(context.getResources().getColor(R.color.colorBlack));
- viewHolder.textViewLine.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
- viewHolder.linearLayoutProductNameList.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
- }
- return convertView;
- }
-
- public class ViewHolder {
- public TextView textViewProjectName, textViewLine;
- LinearLayout linearLayoutProductNameList;
- }
-
- public void setCurrentItem(int currentItem) {
- this.mCurrentItem = currentItem;
- }
-
- public void setClick(boolean click) {
- this.isClick = click;
- }
- }
在配上item_project_name_list 的布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/linearLayoutProductNameList"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/textViewLine"
- android:layout_width="2dp"
- android:layout_height="match_parent"
- android:background="@color/colorOrangeRed" />
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/textViewProjectName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:padding="10dp"
- android:text="@string/textview_phone_card"
- android:textColor="@color/colorOrangeRed" />
-
- <TextView style="@style/MyDivider" />
- </LinearLayout>
-
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。