Android仿美团左右联动购物列表
左右联动购物列表,不难。
一、思路:
两个RecycleView
二、效果图:
三、关键代码:
public class MainActivity extends AppCompatActivity {private RecyclerView rl_left;private RecyclerView rl_right;private LinkedHashMap<Integer, String> mHeaderList = new LinkedHashMap<>();private ArrayList<FoodBean> rightFoods = new ArrayList<>();private ArrayList<LeftBean> leftTitles = new ArrayList<>();private RightAdapter rightAdapter;private LeftAdapter leftAdapter;private LinearLayoutManager mLayoutManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();initView();}private void initView(){rl_left = findViewById(R.id.rl_left);rl_right = findViewById(R.id.rl_right);rl_right.setLayoutManager(mLayoutManager = new LinearLayoutManager(this));rl_right.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));FloatingBarItemDecoration floatingBarItemDecoration = new FloatingBarItemDecoration(this, mHeaderList);rl_right.addItemDecoration(floatingBarItemDecoration);rightAdapter = new RightAdapter(LayoutInflater.from(this),rightFoods);rl_right.setAdapter(rightAdapter);floatingBarItemDecoration.setOnChangeTitleListener(new FloatingBarItemDecoration.OnChangeTitleListener() {@Overridepublic void onChangePosition(int position) {if (!TextUtils.isEmpty(rightFoods.get(position).getItemTitle())){//判断不同才更新左边选中标题,避免左边因为一直在刷新导致没法响应点击事件if (leftAdapter != null && !rightFoods.get(position).getItemTitle().equals(leftAdapter.getCurrentTitle())){leftAdapter.setCurrentPosition(position);}}}});rl_left.setLayoutManager(new LinearLayoutManager(this));leftTitles.get(0).setSelect(true); //初始化第一个设为trueleftAdapter = new LeftAdapter(leftTitles);rl_left.setAdapter(leftAdapter);leftAdapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() {@Overridepublic void onItemClicked(int rightPosition) {if (mLayoutManager != null){mLayoutManager.scrollToPositionWithOffset(rightPosition, 0);}}});}private void initData(){rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡1","汉堡"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡2"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡3"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡4"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡5"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡6"));rightFoods.add(new FoodBean(R.mipmap.shiwu1,"汉堡7"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡1","炸鸡"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡2"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡3"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡4"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡5"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡6"));rightFoods.add(new FoodBean(R.mipmap.shiwu2,"炸鸡7"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条1","薯条"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条2"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条3"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条4"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条5"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条6"));rightFoods.add(new FoodBean(R.mipmap.shiwu3,"薯条7"));for (int i = 0;i<rightFoods.size();i++){if (!TextUtils.isEmpty(rightFoods.get(i).getItemTitle())){mHeaderList.put(i,rightFoods.get(i).getItemTitle());leftTitles.add(new LeftBean(i,rightFoods.get(i).getItemTitle()));}}}
}
四、项目demo源码结构图:
有问题或者需要完整源码demo的私信我,我每天都看私信的