AndroidRecyclerView中的Item嵌套RecyclerView使用实例
Posted wenzhang1102
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AndroidRecyclerView中的Item嵌套RecyclerView使用实例相关的知识,希望对你有一定的参考价值。
最近项目中有一个需求是RecyclerView中的Item中嵌套RecyclerView,在网上没找到什么资料,然后就自己尝试了一下。
首先看一下效果图
录制GIF现在不大方便,就直接上两个图了。
关于RecyclerView,都已经很熟悉了不熟悉的去百度一下,这里就不多介绍了。
先说一下需求,最外层的是一个RecyclerView是竖向的,然后外层RecyclerView里面Item中的RecyclerView是GridManager。
我们先看一下具体的代码:
最外层的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvMultipleItem"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
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<RecyclerView.ViewHolder>
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<TopCategoryBean.MaleBean> male = mCategoryBean.getMale();
List<TopCategoryBean.MaleBean> 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<RecyclerView.ViewHolder>
List<TopCategoryBean.MaleBean> mCategoryBeans;
public CategoryAdapter(List<TopCategoryBean.MaleBean> maleBeans)
mCategoryBeans = maleBeans;
public void setCategoryBeans(List<TopCategoryBean.MaleBean> 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的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:orientation="vertical"
android:padding="10dp"
app:cardCornerRadius="3dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="男生"
android:textSize="16sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="2dp"
android:layout_marginStart="10dp"
android:background="#FF0000"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:padding="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
这个布局里面用了一个recyclerView控件。我们需要在最外层的适配器中给这个RecyclerView添加一个适配器。
private class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
List<TopCategoryBean.MaleBean> mCategoryBeans;
public CategoryAdapter(List<TopCategoryBean.MaleBean> maleBeans)
mCategoryBeans = maleBeans;
public void setCategoryBeans(List<TopCategoryBean.MaleBean> 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。
这里写的不是很好,具体需要了解的可以去看一下代码:传送门
以上是关于AndroidRecyclerView中的Item嵌套RecyclerView使用实例的主要内容,如果未能解决你的问题,请参考以下文章
Android RecyclerView 监听Item短按和长按
Android RecyclerView单击长按事件标准实现:基于OnItemTouchListener + GestureDetector
Android RecyclerView之添加Item分割线