Several commonly used Adapter


people ugly words, directly 撸 code:

public class MainMenuAdapter extends RecyclerView.Adapter <MainMenuAdapter.MainMenuViewholder>{
    Public Context context;
    //Menu collection
    Public List<Menu> menuList;

    //With reference structure
    Public MainMenuAdapter(Context context, List<Menu> menuList) {
        This.context = context;
        this.menuList = menuList;

    / / Return to the view view by loading the layout
    Public MainMenuViewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        Return new MainMenuViewholder(LayoutInflater.from(context).inflate(R.layout.item_main_menu,null));

    / / Get data from the collection to set the content in the layout control
    Public void onBindViewHolder(@NonNull MainMenuViewholder mainMenuViewholder, int i) {
        Menu menu=menuList.get(i);

    Public int getItemCount() {
        / / If the menuList is not empty, return the collection size, otherwise return 0
        Return null!=menuList?menuList.size():0;

    / / Customize a Viewholder inherits RecyclerView.ViewHolder, initialize the control
    Class MainMenuViewholder extends RecyclerView.ViewHolder{
        Public ImageView mImageView;
        Public TextView mTextView;

       Public MainMenuViewholder(@NonNull View itemView) {
How to use 

@java code:

MainMenuAdapter mainMenuAdapter=new MainMenuAdapter(getContext(), DataUtil.getMainMenu(icons,names));
/ / Set the cloth style of recyclerView_main_menu (horizontal, vertical, grid)
/ / The default is the horizontal layout (that is, the default parameter is 1)
recyclerView_main_menu.setLayoutManager(new GridLayoutManager(getContext(),4,1,false));



public class MainMenuListViewAdapter extends BaseAdapter{

    Private Context context;
    Private List<MyListerView> listerViewList;

    //With reference structure
    Public MainMenuListViewAdapter(Context context, List<MyListerView> listerViewList) {
        This.context = context;
        this.listerViewList = listerViewList;

    Public int getCount() {
        Return null!=listerViewList?listerViewList.size():0;

    Public MyListerView getItem(int i) {
        Return listerViewList.get(i);

    Public long getItemId(int i) {
        Return i;

    Public View getView(int i, View view, ViewGroup viewGroup) {
        View=View.inflate(context, R.layout.listview_item_main_menu,null);
        MyListerView myListerView=getItem(i);

        / / Initialize the control
        ImageView image_listView_main_menu=view.findViewById(;
        TextView title_listView_main_menu=view.findViewById(;
        TextView detial_listView_main_menu=view.findViewById(;

        / / Set the control content

        / / Return to the view
        Return view;
How to use the 

@java code:

//Create a list adapter
MainMenuListViewAdapter mainMenuListViewAdapter=new MainMenuListViewAdapter(getContext(), DataUtil.getMainMenuListView(list_icons,list_titles,list_detials));
/ / Set the adapter for the list

can dynamically load the data in string:

string.xml file

<string-array name="main_menu">
        <item>air ticket </item>
        <item> staying at the hotel</item>
        <item>Go to travel</item>
        <item> Surrounding Tour</item>
        <item> buy tickets</item>
        <item> train ticket </item>
        <item>car ticket </item>
        <item>Lead Miles</item>

javaGet data in

//Get custom string data from string