Several commonly used Adapter

1.RecyclerViewAdapter

people ugly words, directly 撸 code:

public class MainMenuAdapter extends RecyclerView.Adapter <MainMenuAdapter.MainMenuViewholder>{
    //Context
    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
    @NonNull
    @Override
    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
    @Override
    Public void onBindViewHolder(@NonNull MainMenuViewholder mainMenuViewholder, int i) {
        Menu menu=menuList.get(i);
        mainMenuViewholder.mImageView.setImageResource(menu.icon);
        mainMenuViewholder.mTextView.setText(menu.menuName);
    }

    @Override
    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) {
           Super(itemView);
           mImageView=itemView.findViewById(R.id.icon_main_menu);
           mTextView=itemView.findViewById(R.id.text_main_menu);
       }
   }
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));

recyclerView_main_menu.setAdapter(mainMenuAdapter);

2.ListViewAdapter

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;
    }

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

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

    @Override
    Public long getItemId(int i) {
        Return i;
    }

    @Override
    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(R.id.image_listView_main_menu);
        TextView title_listView_main_menu=view.findViewById(R.id.title_listView_main_menu);
        TextView detial_listView_main_menu=view.findViewById(R.id.detial_listView_main_menu);

        / / Set the control content
        image_listView_main_menu.setImageResource(myListerView.icon);
        title_listView_main_menu.setText(myListerView.title);
        detial_listView_main_menu.setText(myListerView.detial);

        / / 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
listView_main_menu.setAdapter(mainMenuListViewAdapter); 

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>
    </string-array>

javaGet data in

//Get custom string data from string
Menu_names=this.getActivity().getResources().getStringArray(R.array.main_menu);