效果如下图所示:
当在开发中无论是Activity中还是Fragment中基本上都会使用到这个功能:使用这个功能要注意以下几点:
1:当项目中需要很多个不同的RecyclerView来实现的时候就先定义一个中的RecyclerView来盛放子RecyclerView
2:定义一个总的Adapter来加载不同的RecyclerView
3:注意子RecyclerView的viewType类型一定不能相同要不然就只加载一个
4:一定要使用LayoutManager来管理RecyclerView,这是RecyclerView的优于ListView的地方,可扩展性强
5:等理解深入的时候要学会解决滑动冲突,因为我们很有可能需要上下滑动和水平滑动。
现在就来说说这种界面的加载方法:
package com.choicelean.superwinner.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.choicelean.superwinner.R;
import com.choicelean.superwinner.web.result.babytest.BabyTestGoods;
import com.choicelean.superwinner.web.result.babytest.BabyTestGoodsList;
import com.choicelean.superwinner.web.result.babytest.BabyTestProject;
import com.choicelean.superwinner.web.result.babytest.BabyTestProjectList;
import java.util.ArrayList;
import java.util.List;
public class BabyTestActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView babyTestBack;
private TextView babyTestAdvice;
private RecyclerView recommendProjectGoods;
//推荐项目
private BabyTestProject babyTestProject;
private BabyTestProjectList babyTestProjectList;
private List<BabyTestProjectList> babyTestProjectLists;
//推荐商品
private BabyTestGoods babyTestGoods;
private BabyTestGoodsList babyTestGoodsList;
private List<BabyTestGoodsList> babyTestGoodsLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_baby_test);
initView();
initData();
}
public void initView() {
babyTestBack = (ImageView) findViewById(R.id.baby_test_back);
babyTestBack.setOnClickListener(this);
babyTestAdvice = (TextView) findViewById(R.id.baby_test_advice);
recommendProjectGoods = (RecyclerView) findViewById(R.id.recommend_project_goods);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recommendProjectGoods.setLayoutManager(layoutManager);
}
public void initData() {
babyTestAdvice.setText("\u3000" + "指导建议:孩子在这次游戏中表现的较为活泼,我们建议家长可以鼓励宝宝多做运动,并加强以下项目锻炼以及使用相关产品。");
babyTestProjectLists = new ArrayList<>();
babyTestProjectList = new BabyTestProjectList(R.drawable.recommendedproject_image_01, "八爪鱼");
for (int i = 0; i <= 3; i++) {
babyTestProjectLists.add(babyTestProjectList);
}
babyTestProject = new BabyTestProject("推荐项目", babyTestProjectLists);
//推荐商品
babyTestGoodsLists=new ArrayList<>();
babyTestGoodsList=new BabyTestGoodsList(R.drawable.recommendcommodities_image_02,"健康绵柔轻薄粒粒",79.9);
for(int i=0;i<=5;i++){
babyTestGoodsLists.add(babyTestGoodsList);
}
babyTestGoods=new BabyTestGoods("推荐商品",babyTestGoodsLists);
BabyTestAdapter adapter = new BabyTestAdapter();
recommendProjectGoods.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.baby_test_back:
finish();
break;
default:
break;
}
}
/*
====================================================
====================================================
================== BabyTestRecycler是我们重点讲解的内容 ================
====================================================
====================================================
*/
private class BabyTestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//根据不同的ViewHolder来加载不同的视图
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//根据的是viewType来判断的
//这里的区分viewType是根据要下面要加载的布局来判断的是否是同一种viewType的
if (viewType == 0) {
return new BabyTestProjectVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.baby_test_recommend_project, parent, false));
} else {
return new BabyTestGoodsVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.baby_test_recommend_goods, parent, false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof BabyTestProjectVH) {
((BabyTestProjectVH) holder).setData(babyTestProject);
}else if(holder instanceof BabyTestGoodsVH) {
((BabyTestGoodsVH)holder).setData(babyTestGoods);
}
}
@Override
public int getItemCount() {
return 2; //写3就只能显示前3个类型
}
@Override
public int getItemViewType(int position) {
return position < 2 ? position : 2;
}
}
//下面是具体的两个子项目
/**
* 宝贝测评推荐项目
*/
private class BabyTestProjectVH extends RecyclerView.ViewHolder {
private TextView recommendProject;
private RecyclerView recommendProjectRecycler;
public BabyTestProjectVH(View itemView) {
super(itemView);
recommendProject = (TextView) itemView.findViewById(R.id.recommend_project);
recommendProjectRecycler = (RecyclerView) itemView.findViewById(R.id.recommend_project_recycler);
}
private void setData(BabyTestProject babyTestProject) {
recommendProject.setText(babyTestProject.getName());
LinearLayoutManager layoutManager = new LinearLayoutManager(BabyTestActivity.this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recommendProjectRecycler.setLayoutManager(layoutManager);
//adapter的初始化
BabyTestProjectAdapter adapter = new BabyTestProjectAdapter(babyTestProjectLists);
recommendProjectRecycler.setAdapter(adapter);
}
}
private class BabyTestProjectViewHolder extends RecyclerView.ViewHolder {
private ImageView itemProjectImage;
private TextView itemProjectName;
public BabyTestProjectViewHolder(View itemView) {
super(itemView);
itemProjectImage = (ImageView) itemView.findViewById(R.id.item_project_image);
itemProjectName = (TextView) itemView.findViewById(R.id.item_project_name);
}
}
private class BabyTestProjectAdapter extends RecyclerView.Adapter<BabyTestProjectViewHolder> {
private List<BabyTestProjectList> mBabyTestProject;
public BabyTestProjectAdapter(List<BabyTestProjectList> babyTestProjects) {
mBabyTestProject = babyTestProjects;
}
@Override
public BabyTestProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.baby_test_recommend_project_item, parent, false);
BabyTestProjectViewHolder holder = new BabyTestProjectViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(BabyTestProjectViewHolder holder, int position) {
BabyTestProjectList babyTestProjectList = mBabyTestProject.get(position);
holder.itemProjectImage.setImageResource(babyTestProjectList.getResourceImage());
holder.itemProjectName.setText(babyTestProjectList.getProjectName());
}
@Override
public int getItemCount() {
return mBabyTestProject.size();
}
}
/**
* 推荐商品
*/
private class BabyTestGoodsVH extends RecyclerView.ViewHolder {
private TextView recommendGoods;
private RecyclerView recommendGoodsRecycler;
public BabyTestGoodsVH(View itemView) {
super(itemView);
recommendGoods = (TextView) itemView.findViewById(R.id.recommend_goods);
recommendGoodsRecycler = (RecyclerView) itemView.findViewById(R.id.recommend_goods_recycler);
}
public void setData(BabyTestGoods babyTestGoods) {
recommendGoods.setText(babyTestGoods.getName());
LinearLayoutManager layoutManager = new LinearLayoutManager(BabyTestActivity.this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recommendGoodsRecycler.setLayoutManager(layoutManager);
BabyTestRecommendGoodsAdapter adapter = new BabyTestRecommendGoodsAdapter(babyTestGoodsLists);
recommendGoodsRecycler.setAdapter(adapter);
}
}
private class BabyTestRecommendGoodsAdapter extends RecyclerView.Adapter<BabyTestRecommendGoodsAdapter.ViewHolder> {
private List<BabyTestGoodsList> mBabyTestGoodsListList;
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView goodsImage;
private TextView goodsName;
private TextView goodsPrice;
public ViewHolder(View itemView) {
super(itemView);
goodsImage = (ImageView) itemView.findViewById(R.id.goods_image);
goodsName = (TextView) itemView.findViewById(R.id.goods_name);
goodsPrice = (TextView) itemView.findViewById(R.id.goods_price);
}
}
public BabyTestRecommendGoodsAdapter(List<BabyTestGoodsList> babyTestGoodsLists) {
mBabyTestGoodsListList = babyTestGoodsLists;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.baby_test_recommend_goods_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
BabyTestGoodsList babyTestGoodsList = mBabyTestGoodsListList.get(position);
holder.goodsImage.setImageResource(babyTestGoodsList.getResourceImage());
holder.goodsName.setText(babyTestGoodsList.getGoodName());
holder.goodsPrice.setText("¥" + babyTestGoodsList.getGoodPrice());
}
@Override
public int getItemCount() {
return mBabyTestGoodsListList.size();
}
}
}