最近项目中有一个需求是RecyclerView中的Item中嵌套RecyclerView,在网上没找到什么资料,然后就自己尝试了一下。
首先看一下效果图
录制GIF现在不大方便,就直接上两个图了。
关于RecyclerView,都已经很熟悉了不熟悉的去百度一下,这里就不多介绍了。
先说一下需求,最外层的是一个RecyclerView是竖向的,然后外层RecyclerView里面Item中的RecyclerView是GridManager。
我们先看一下具体的代码:
最外层的布局代码:
public class MultipleActivity extends AppCompatActivity {
private static final String TAG = "MultipleActivity";
RecyclerView mRecyclerView;
TopCategoryBean mCategoryBean = new TopCategoryBean();
MultipleAdapter mMultipleAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple);
mRecyclerView = (RecyclerView) findViewById(R.id.rvMultipleItem);
mMultipleAdapter = new MultipleAdapter(this, mCategoryBean);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://api.zhuishushenqi.com/cats/lv2/statistics");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(60000);
urlConnection.setConnectTimeout(10000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
Gson gson = new Gson();
final TopCategoryBean categoryBean = gson.fromJson(sb.toString(), TopCategoryBean.class);
Log.i(TAG,"-------->"+categoryBean);
runOnUiThread(new Runnable() {
@Override
public void run() {
mMultipleAdapter.setCategoryBean(categoryBean);
mRecyclerView.setAdapter(mMultipleAdapter);
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
最外层的布局文件就是一个RecyclerView控件,然后在Java代码中必须要设置LinearLayoutManager,然后就没什么很难的实现了。
接着是适配器的代码:
public class MultipleAdapter extends RecyclerView.Adapter {
private static final String TAG = "MultipleAdapter";
private Context mContext;
private LayoutInflater mInflater;
private TopCategoryBean mCategoryBean;
public MultipleAdapter(Context context, TopCategoryBean categoryBean) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mCategoryBean = categoryBean;
}
public void setCategoryBean(TopCategoryBean categoryBean) {
mCategoryBean = categoryBean;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_multiple_normal, parent, false);
return new NormalItemViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
NormalItemViewHolder viewHolder = (NormalItemViewHolder) holder;
RecyclerView rvCategory = viewHolder.mRvCategory;
rvCategory.setHasFixedSize(true);
rvCategory.setLayoutManager(new GridLayoutManager(mContext, 3));
List male = mCategoryBean.getMale();
List female = mCategoryBean.getFemale();
CategoryAdapter adapter = new CategoryAdapter(male);
if (position == 0) {
viewHolder.mTextView.setText("男生");
adapter.setCategoryBeans(male);
} else if (position == 1) {
viewHolder.mTextView.setText("女生");
adapter.setCategoryBeans(female);
} else if (position == 2) {
viewHolder.mTextView.setText("耽美");
adapter.setCategoryBeans(mCategoryBean.getPicture());
} else if (position == 3) {
viewHolder.mTextView.setText("出版");
adapter.setCategoryBeans(mCategoryBean.getPress());
}
rvCategory.setAdapter(adapter);
}
@Override
public int getItemCount() {
return 4;
}
private class CategoryAdapter extends RecyclerView.Adapter {
List mCategoryBeans;
public CategoryAdapter(List maleBeans) {
mCategoryBeans = maleBeans;
}
public void setCategoryBeans(List categoryBeans) {
mCategoryBeans = categoryBeans;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_category, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemViewHolder viewHolder = (ItemViewHolder) holder;
final TopCategoryBean.MaleBean maleBean = mCategoryBeans.get(position);
viewHolder.mTvBookCount.setText(maleBean.getBookCount() + "本");
viewHolder.mTvCategoryName.setText(maleBean.getName());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount() {
return mCategoryBeans.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView mTvCategoryName;
TextView mTvBookCount;
CardView llItem;
public ItemViewHolder(View itemView) {
super(itemView);
mTvCategoryName = itemView.findViewById(R.id.tvCategoryName);
mTvBookCount = itemView.findViewById(R.id.tvBookCount);
llItem = itemView.findViewById(R.id.llItem);
}
}
}
private class NormalItemViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
RecyclerView mRvCategory;
public NormalItemViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tvName);
mRvCategory = itemView.findViewById(R.id.rvCategory);
}
}
}
适配器这里我没做封装,直接用最原始的写法,需要注意的一点是:我这里最外层的Item数只有4个,我就直接写死了4个,具体的项目中而已根据需求来实现。
外层Item的布局代码:
这个布局里面用了一个recyclerView控件。我们需要在最外层的适配器中给这个RecyclerView添加一个适配器。
private class CategoryAdapter extends RecyclerView.Adapter {
List mCategoryBeans;
public CategoryAdapter(List maleBeans) {
mCategoryBeans = maleBeans;
}
public void setCategoryBeans(List categoryBeans) {
mCategoryBeans = categoryBeans;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_category, parent, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemViewHolder viewHolder = (ItemViewHolder) holder;
final TopCategoryBean.MaleBean maleBean = mCategoryBeans.get(position);
viewHolder.mTvBookCount.setText(maleBean.getBookCount() + "本");
viewHolder.mTvCategoryName.setText(maleBean.getName());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount() {
return mCategoryBeans.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView mTvCategoryName;
TextView mTvBookCount;
CardView llItem;
public ItemViewHolder(View itemView) {
super(itemView);
mTvCategoryName = itemView.findViewById(R.id.tvCategoryName);
mTvBookCount = itemView.findViewById(R.id.tvBookCount);
llItem = itemView.findViewById(R.id.llItem);
}
}
}
这是里面适配器的代码,使用的时候跟别的地方使用一样,需要注意的是布局管理器设置成GridLayoutManager,因为需求里面是GridLayout的,这样就能实现RecyclerView里面嵌套RecyclerView。
这里写的不是很好,具体需要了解的可以去看一下代码:传送门