使用RecyclerView怎么實現(xiàn)一個網(wǎng)格布局?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元蚌山做網(wǎng)站,已為上家服務(wù),為蚌山各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
activity_main.xml
RecyclerView子項列表布局
recyclerview_item.xml
JAVA代碼
MainActivity
package com.matt.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private GridAdapter mGridAdapter; private ListmApplyList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recyclerview); mApplyList = new ArrayList<>(); mApplyList.add(new ApplyBean("SETTINGS","設(shè)置", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("CALCULATOR","計算器", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("WEATHER","天氣", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("CALENDAR","日歷", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("PHOTO" ,"相冊", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("TIME" ,"時鐘", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("FM" ,"收音機", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("CAMERA" ,"相機", R.mipmap.ic_launcher)); mApplyList.add(new ApplyBean("PLAY","播放器", R.mipmap.ic_launcher)); mGridAdapter = new GridAdapter(mApplyList); //這里的第二個參數(shù)4代表的是網(wǎng)格的列數(shù) mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); mRecyclerView.setAdapter(mGridAdapter); } }
RecyclerView適配器GridAdapter
package com.fenda.homepage.Adapter; 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.fenda.homepage.R; import com.fenda.homepage.bean.ApplyBean; import java.util.List; /** * @author:Matt.Liao * 日期時間: 2019/9/1 17:45 * 內(nèi)容描述: * 版本: * 包名: */ public class GridAdapter extends RecyclerView.Adapter{ private List mList; private RecyclerView recyclerView; public GridAdapter(List list){ this.mList = list; } @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.submenu_item_recyclerview, null); final Holder holder = new Holder(view); //對加載的子項注冊監(jiān)聽事件 holder.fruitView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position = holder.getAdapterPosition(); String applyId = mList.get(position).getApplyId(); onItemClickListener.onItemClick(view ,applyId); } }); return holder; } @Override public void onBindViewHolder(Holder holder, int position) { holder.mApplyNameTv.setText(mList.get(position).getApplyName()); holder.mApplyImageIv.setImageResource(mList.get(position).getApplyImage()); } @Override public int getItemCount() { return mList == null ? 0 : mList.size(); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; } /** * 定義RecyclerView選項單擊事件的回調(diào)接口 */ public interface OnItemClickListener{ /** * @param view 當前單擊的View * @param applyId 單擊的View的應(yīng)用id */ void onItemClick(View view, String applyId); } class Holder extends RecyclerView.ViewHolder { private TextView mApplyNameTv; private ImageView mApplyImageIv; private View fruitView; //表示我們自定義的控件的視圖 public Holder(View itemView) { super(itemView); fruitView = itemView; mApplyNameTv = itemView.findViewById(R.id.tv_apply_name); mApplyImageIv = itemView.findViewById(R.id.iv_apply_image); } } }
應(yīng)用列表實體類ApplyBean
package com.matt.recyclerview; import android.os.Parcel; import android.os.Parcelable; /** * @author matt.liaojianpeng * 日期時間: 2019/9/1 16:31 * 內(nèi)容描述: * 版本: * 包名: */ public class ApplyBean{ private String applyId; private String applyName; private int applyImage; public String getApplyId() { return applyId; } public void setApplyId(String applyId) { this.applyId = applyId; } public ApplyBean(String applyName, int applyImage){ this.applyName = applyName; this.applyImage = applyImage; } public ApplyBean(String applyId , String applyName, int applyImage){ this.applyId = applyId; this.applyName = applyName; this.applyImage = applyImage; } public ApplyBean() { } public String getApplyName() { return applyName; } public void setApplyName(String applyName) { this.applyName = applyName; } public int getApplyImage() { return applyImage; } public void setApplyImage(int applyImage) { this.applyImage = applyImage; } public ApplyBean(Parcel source) { applyId = source.readString(); applyName = source.readString(); applyImage = source.readInt(); } }
詳細講解
設(shè)置RecyclerView適配器
//這里的第二個參數(shù)3代表的是網(wǎng)格的列數(shù) mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3)); mRecyclerView.setAdapter(mGridAdapter);
可以設(shè)置的一些參數(shù),說明如下:
setLayoutManager設(shè)置RecyclerView布局樣式
GridLayoutManager:網(wǎng)格布局
LinearLayoutManager:線性布局
適配器GridAdapter繼承RecyclerView.Adapter
初始化
重寫構(gòu)造方法,傳入子項數(shù)據(jù)列表
private ListmList; private RecyclerView recyclerView; public GridAdapter(List list){ this.mList = list; }
內(nèi)部類Holder用于綁定數(shù)據(jù)類型
class Holder extends RecyclerView.ViewHolder { private TextView mApplyNameTv; private ImageView mApplyImageIv; private View fruitView; //表示我們自定義的控件的視圖 public Holder(View itemView) { super(itemView); fruitView = itemView; mApplyNameTv = itemView.findViewById(R.id.tv_apply_name); mApplyImageIv = itemView.findViewById(R.id.iv_apply_image); } }
onCreateViewHolder()方法,負責承載每個子項的布局。
onBindViewHolder()方法,負責將每個子項holder綁定數(shù)據(jù)。
getItemCount()方法,返回子項列表數(shù)目。
setOnItemClickListener()方法,設(shè)置子項列表監(jiān)聽。
OnItemClickListener()接口,定義RecyclerView選項單擊事件的回調(diào)接口。
子項監(jiān)聽實現(xiàn)的方法,如下
mGridAdapter.setOnItemClickListener(new GridAdapter.OnItemClickListener() { @Override public void onItemClick(View view, String applyId) { if(applyId.equals("SETTINGS")){ Toast.makeText(getApplicationContext(),"設(shè)置",Toast.LENGTH_SHORT).show(); } else if (applyId.equals("CALCULATOR")){ Toast.makeText(getApplicationContext(),"計算器",Toast.LENGTH_SHORT).show(); } } });
關(guān)于使用RecyclerView怎么實現(xiàn)一個網(wǎng)格布局問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。