In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about how to use the list of RecyclerView imitation applications to implement grid layout. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Detailed code
XML layout file
Using RecyclerView controls in a layout
Activity_main.xml
RecyclerView subitem list layout
Recyclerview_item.xml
JAVA code
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 List mApplyList; @ 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", "Settings", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("CALCULATOR", "Calculator", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("WEATHER", "Weather", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("CALENDAR", "Calendar", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("PHOTO", "Photo album", R.mipmap.ic_launcher)) MApplyList.add (new ApplyBean ("TIME", "clock", R.mipmap.ic_launcher); mApplyList.add (new ApplyBean ("FM", "Radio", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("CAMERA", "camera", R.mipmap.ic_launcher)); mApplyList.add (new ApplyBean ("PLAY", "player", R.mipmap.ic_launcher)); mGridAdapter = new GridAdapter (mApplyList) / / the second parameter 4 here represents the number of columns in the grid mRecyclerView.setLayoutManager (new GridLayoutManager (this, 3)); mRecyclerView.setAdapter (mGridAdapter);}}
RecyclerView Adapter 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 * date time: 2019-9-1 17:45 * content description: * version: * package name: * / 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 ()) .propagate (R.layout.submenu_item_recyclerview, null) Final Holder holder = new Holder (view); / / A pair of loaded subkey registration listening events 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 } / * define callback interface for RecyclerView option click event * / public interface OnItemClickListener {/ * @ param view currently clicked View * @ param applyId clicked View application id * / void onItemClick (View view, String applyId);} class Holder extends RecyclerView.ViewHolder {private TextView mApplyNameTv; private ImageView mApplyImageIv; private View fruitView; / / represents the view public Holder (View itemView) {super (itemView) of our custom control; fruitView = itemView MApplyNameTv = itemView.findViewById (R.id.tv_apply_name); mApplyImageIv = itemView.findViewById (R.id.iv_apply_image);}}
Apply list entity class ApplyBean
Package com.matt.recyclerview; import android.os.Parcel;import android.os.Parcelable; / * * @ author matt.liaojianpeng * date time: 2019-9-1 16:31 * content description: * version: * package name: * / 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 ();}}
Explain in detail
Set up the RecyclerView adapter
/ / the second parameter 3 here represents the number of columns in the grid mRecyclerView.setLayoutManager (new GridLayoutManager (this, 3)); mRecyclerView.setAdapter (mGridAdapter)
Some parameters that can be set are described as follows:
SetLayoutManager sets RecyclerView layout style
GridLayoutManager: grid layout
LinearLayoutManager: linear layout
Adapter GridAdapter inherits RecyclerView.Adapter
Initialization
Override the constructor, passing in a list of child data
Private List mList; private RecyclerView recyclerView; public GridAdapter (List list) {this.mList = list;}
The inner class Holder is used to bind data types
Class Holder extends RecyclerView.ViewHolder {private TextView mApplyNameTv; private ImageView mApplyImageIv; private View fruitView; / / represents the view of our custom control public Holder (View itemView) {super (itemView); fruitView = itemView; mApplyNameTv = itemView.findViewById (R.id.tv_apply_name); mApplyImageIv = itemView.findViewById (R.id.iv_apply_image);}}
The onCreateViewHolder () method, which is responsible for hosting the layout of each child.
The onBindViewHolder () method, which is responsible for binding data to each child holder.
The getItemCount () method returns the number of subitems in the list.
The setOnItemClickListener () method, which sets the subitem list listener.
The OnItemClickListener () interface, which defines the callback interface for the RecyclerView option click event.
The method of child listening implementation is as follows
MGridAdapter.setOnItemClickListener (new GridAdapter.OnItemClickListener () {@ Override public void onItemClick (View view, String applyId) {if (applyId.equals ("SETTINGS")) {Toast.makeText (getApplicationContext (), "setup", Toast.LENGTH_SHORT). Show ();} else if (applyId.equals ("CALCULATOR")) {Toast.makeText (getApplicationContext (), "Calculator", Toast.LENGTH_SHORT). Show ();})
Thank you for reading! This is the end of this article on "how to use RecyclerView imitation application list to achieve grid layout". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.