2018-01-06
原CSDN博客已弃用,文章会逐渐迁移过来。
转帖请注明本文出自weimeig的博客(https://blog.csdn.net/weimeig/article/details/80244059),请尊重他人的辛勤劳动成果,谢谢
一、添加依赖
dependencies {
compile 'com.android.support:recyclerview-v7:23.1.1'
}
点击右上角Sync Now,此时如果不报红,直接跳过下面步骤,可以开始编写RecyclerView控件了。
但,如果点击了右上角的Sync Now,却还是出现报红不用怕
修改后
点击右上角Sync Now
二、定义本地数据源Menu类,为RecyclerView提供数据源。
Menu.java
package com.administrator.taolvyou.entity;
/**
* 数据源
* Created by Administrator on 2018/5/8.
*/
public class Menu {
public int icon;
public String menuName;
public Menu(int icon, String menuName) {
this.icon = icon;
this.menuName = menuName;
}
}
三、定义DataUtil工具类,将数据源转换成集合形式存储并返回该集合对象。
DataUtil.java
package com.administrator.taolvyou.util;
import com.administrator.taolvyou.entity.Menu;
import java.util.ArrayList;
import java.util.List;
/**
* 数据源
* Created by Administrator on 2018/5/8.
*/
public class DataUtil {
/**
* 主菜单
* @param icons
* @param names
* @return
*/
public static List<Menu> getMainMenus(int icons[],String names[]){//导包导入我们刚才建立的实体类的包
List<Menu> menus = new ArrayList<>();
for(int i=0;i<icons.length;i++){
Menu menu = new Menu(icons[i],names[i]);
menus.add(menu);
}
return menus;
}
}
四、在strings文件中定义数组数据
<resources>
<string name="app_name">TaoLvYou</string>
<string-array name="main_menu">
<item>飞机票</item>
<item>住酒店</item>
<item>去旅游</item>
<item>周边游</item>
<item>买门票</item>
<item>火车票</item>
<item>汽车票</item>
<item>领里程</item>
</string-array>
</resources>
五、在fragment_main中引入该RecyelerView控件
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
六、为数据与视图之间定义适配器MainMenuAdapter
MainMenuAdapter.java
package com.administrator.taolvyou.adapter;
import android.content.Context;
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.administrator.taolvyou.R;
import com.administrator.taolvyou.entity.Menu;
import java.util.List;
/**
* Created by Administrator on 2018/5/8.
*/
public class MainMenuAdapter extends RecyclerView.Adapter<MainMenuAdapter.MainMenuViewHolder> {
protected Context context;
protected List<Menu> menus;
class MainMenuViewHolder extends RecyclerView.ViewHolder{
public ImageView mImgMenuIcon;
public TextView mTxtMenuName;
public MainMenuViewHolder(View itemView) {
super(itemView);
mImgMenuIcon=(ImageView) itemView.findViewById(R.id.img_menu_icon);
mTxtMenuName=(TextView) itemView.findViewById(R.id.txt_menu_name);
}
}
public MainMenuAdapter(Context context, List<Menu> menus){
this.context=context;
this.menus=menus;
}
@Override
public MainMenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MainMenuViewHolder(LayoutInflater.from(context).inflate(R.layout.item_main_menu,null));//将布局渲染成一个视图的形式
}
@Override
public void onBindViewHolder(MainMenuViewHolder holder, int position) {
Menu menu=menus.get(position);//返还每一个菜单的对象
holder.mImgMenuIcon.setImageResource(menu.icon);
holder.mTxtMenuName.setText(menu.menuName);
}
@Override
public int getItemCount() {
return null!=menus?menus.size():0;
}
}
七、在主界面视图中使用该布局
MainFragment.java
package com.administrator.taolvyou.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.administrator.taolvyou.R;
import com.administrator.taolvyou.adapter.MainMenuAdapter;
import com.administrator.taolvyou.util.DataUtil;
/**
* 主界面视图
*/
public class MainFragment extends Fragment{
protected RecyclerView mRecyclerViewMenu;
//菜单图标
protected int [] menuIons={R.mipmap.menu_airport,R.mipmap.menu_car
,R.mipmap.menu_course,R.mipmap.menu_hatol,
R.mipmap.menu_nearby, R.mipmap.menu_ticket,
R.mipmap.menu_train,R.mipmap.menu_trav};
String[] menus;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
menus=this.getActivity().getResources().getStringArray(R.array.main_menu);//从string文件中,将数组数据拿出来
mRecyclerViewMenu = (RecyclerView) getView().findViewById(R.id.recycleview_main_menu);
//设置RecyclerView布局的样式
mRecyclerViewMenu.setLayoutManager(new GridLayoutManager(getActivity(),4));//第一个参数是context对象,第二个参数是列数
//另有线性布局写法可另行百度
//菜单
MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(getActivity(), DataUtil.getMainMenus(menuIons,menus));
mRecyclerViewMenu.setAdapter(mainMenuAdapter);
}
}
运行效果:
项目Demo下载: