效果图:
2.左边列表对应的布局文件(item_type_goods.xml)
4.右边列表对应的布局文件(item_select_goods.xml)
8.附上图片
package com.example.recycleview; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class TypeAdapter extends BaseAdapter { private List<String> goodsList; private Context context; private String typeW = ""; public TypeAdapter(List<String> goodsList,Context context){ this.goodsList = goodsList; this.context = context; } public void select(String typeW){ this.typeW = typeW; notifyDataSetChanged(); } @Override public int getCount() { return goodsList.size(); } @Override public Object getItem(int i) { return goodsList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder = null; if(view == null){ viewHolder = new ViewHolder(); view = LayoutInflater.from(context).inflate(R.layout.item_type_goods,null); viewHolder.tvName = view.findViewById(R.id.tvName); viewHolder.viewColor = view.findViewById(R.id.viewColor); view.setTag(viewHolder); }else { viewHolder = (ViewHolder)view.getTag(); } viewHolder.tvName.setText(goodsList.get(i) + ""); if(typeW.equals(goodsList.get(i))){ //点亮 viewHolder.tvName.setTextColor(Color.parseColor("#FF0033")); viewHolder.viewColor.setBackgroundColor(Color.parseColor("#FF0033")); }else{ viewHolder.tvName.setTextColor(Color.parseColor("#666666")); viewHolder.viewColor.setBackgroundColor(Color.parseColor("#FFFFFF")); } return view; } class ViewHolder{ TextView tvName; //商品名称 View viewColor; //选中颜色 } }
左边列表对应的布局文件(item_type_goods.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:background="#fff" android:layout_marginTop="1dp" android:layout_width="match_parent" android:layout_height="wrap_content" > <View android:id="@+id/viewColor" android:background="#fff" android:layout_width="4dp" android:layout_height="match_parent"/> <TextView android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:gravity="center" android:id="@+id/tvName" android:textSize="15sp" android:text="类型" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout>
package com.example.recycleview; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class SelectAdapter extends BaseAdapter { private List<MyContent> goodsList; private Context context; public SelectAdapter(List<MyContent> goodsList,Context context){ this.goodsList = goodsList; this.context = context; } @Override public int getCount() { return goodsList.size(); } @Override public Object getItem(int i) { return goodsList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder = null; if(view == null){ viewHolder = new ViewHolder(); view = LayoutInflater.from(context).inflate(R.layout.item_select_goods,null); viewHolder.tvName = view.findViewById(R.id.tvName); viewHolder.tvType = view.findViewById(R.id.tvType); viewHolder.tvMoney = view.findViewById(R.id.tvMoney); view.setTag(viewHolder); }else{ viewHolder = (ViewHolder)view.getTag(); } viewHolder.tvName.setText(String.valueOf(goodsList.get(i).name)); viewHolder.tvType.setText(String.valueOf(goodsList.get(i).type)); viewHolder.tvMoney.setText(String.valueOf(goodsList.get(i).money)); //当前条目类型 String type = goodsList.get(i).type; if(i == 0){ viewHolder.tvType.setVisibility(View.VISIBLE); }else{ //上一个条目类型 String nextType = goodsList.get(i - 1).type; if(type.equals(nextType)){ viewHolder.tvType.setVisibility(View.GONE); }else{ viewHolder.tvType.setVisibility(View.VISIBLE); } } return view; } //适配器中的GirdView缓存类 class ViewHolder{ //商品名称 TextView tvName,tvType,tvMoney; } }
右边列表对应的布局文件(item_select_goods.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#fff" android:layout_marginTop="1dp" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:id="@+id/tvType" android:textSize="14sp" android:text="类型" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FF0033" android:gravity="center_vertical|center_horizontal" android:paddingLeft="20dp" /> <LinearLayout android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="80dp" android:layout_height="60dp" android:scaleType="fitXY" android:background="#0000" android:src="@mipmap/b1"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_marginLeft="10dp" android:id="@+id/tvName" android:textSize="15sp" android:text="商品名" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_alignParentBottom="true" android:id="@+id/tvMoney_text" android:textSize="8sp" android:text="¥" android:textColor="#FF0033" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginBottom="5dp" android:id="@+id/tvMoney" android:layout_toRightOf="@+id/tvMoney_text" android:layout_alignParentBottom="true" android:textSize="15sp" android:text="20" android:textColor="#FF0033" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout> </LinearLayout>
package com.example.recycleview; public class MyContent { public String name; public String type; public int money; }
package com.example.recycleview; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private ListView lvTest; private ListView lvType; List<String> listType; List<MyContent> list; // 类型适配器 TypeAdapter typeAdapter; // 内容适配器 SelectAdapter selectAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); myOnclick(); } private void initView() { lvType = findViewById(R.id.lvType); lvTest = findViewById(R.id.lvTest); //绑定类型 listType = getDataType(); typeAdapter = new TypeAdapter(listType,MainActivity.this); lvType.setAdapter(typeAdapter); //绑定内容 list = getData(); selectAdapter = new SelectAdapter(list,MainActivity.this); lvTest.setAdapter(selectAdapter); } private void myOnclick() { //类型listView的条目点击事件,定位右边列表 lvType.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //左边类型 String typeT=listType.get(position); for(int i = 0; i < list.size(); i++) { //右边类型 String type=list.get(i).type; if(typeT.equals(type)) { //把定位listView的位置 lvTest.setSelection(i); return; } } } }); //右边listView的滑动事件,点亮左边的选中 lvTest.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //三个参数 //firstVisibleItem:当前能看见的第一个item的ID(从0开始) //visibleItemCount:当前可见的item总数 //totalItemCount:列表中适配器总数量,也就是整个ListView中item总数 String type = list.get(firstVisibleItem).type; typeAdapter.select(type); } }); } private List<MyContent> getData(){ List<MyContent>list = new ArrayList<>(); for(int i = 0; i < 89; i++) { MyContent myContent = new MyContent(); myContent.name="凉拌苦瓜"; myContent.money = i; if(i<=10) { myContent.type="星期一"; } else if(i>10&&i<=20) { myContent.type="星期二"; } else if(i>20&&i<=30) { myContent.type="星期三"; } else if(i>30&&i<=40) { myContent.type="星期四"; } else if(i>40&&i<=50) { myContent.type="星期五"; } else if(i>50&&i<=60) { myContent.type="星期六"; } else if(i>60&&i<=90) { myContent.type="星期日"; } list.add(myContent); } return list; } private List<String> getDataType(){ List<String>list = new ArrayList<>(); list.add("星期一"); list.add("星期二"); list.add("星期三"); list.add("星期四"); list.add("星期五"); list.add("星期六"); list.add("星期日"); return list; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="horizontal"> <ListView android:id="@+id/lvType" android:layout_width="80dp" android:layout_height="match_parent"> </ListView> <LinearLayout android:paddingRight="5dp" android:paddingLeft="5dp" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lvTest" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout> </LinearLayout>