xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" />
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" />4.添加在RecyclerView里面显示的列表项目文件:list_item.xml:
xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/button" android:layout_width="match_parent" android:layout_height="120dp" tools:text="Sound name"/>5.添加用于保存用户信息的类User.java:
package com.example.wanglunhui.recycleview; /* *存储用户信息的类 */ public class User { private String mAddress; private String mAge; private String mSex; private String mName; public String getAddress() { return mAddress; } public void setAddress(String address) { mAddress = address; } public String getAge() { return mAge; } public void setAge(String age) { mAge = age; } public String getSex() { return mSex; } public void setSex(String sex) { mSex = sex; } public String getName() { return mName; } public void setName(String name) { mName = name; } }6.主文件:MainActivity.java:
public class MainActivity extends AppCompatActivity {//AppCompatActivity是Fragment子类,所以继承AppCompatActivity也是继承Frgment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//设置布局问Fragment布局,R.id.fragmentContainer在R.layout.activity_main中,所以先设置布局为它 FragmentManager manager = getFragmentManager(); Fragment fragment = manager.findFragmentById(R.id.fragmentContainer);//Fragment布局的id,用于显示托管的Activity if (fragment == null) { fragment = new MainFragment(); manager.beginTransaction() .add(R.id.fragmentContainer, fragment)//Fragment布局的id .commit(); } }
}
7.托管活动的文件MainFragment.java:
public class MainFragment extends Fragment { private ListmUserList;//存储用户信息 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUserList=new ArrayList<>(); for (int i = 0; i < 30; i++) {//随机添加30个用户信息 User user = new User(); user.setAddress("Address" + i); user.setAge("Age" + i); user.setName("Name" + i); user.setSex("Sex" + i); mUserList.add(user); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //显示的布局就为RecyclerView View view = inflater.inflate(R.layout.fragment_recyclerview, container, false); RecyclerView recyclerView = (RecyclerView)view//获取RecyclerView实例 .findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));//你想要控制其显示的方式,请通过布局管理器LayoutManager,这路设置成网格显示 //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里设置成线性的显示 recyclerView.setAdapter(new UserAdapter()); return view; } private class UserHolder extends RecyclerView.ViewHolder {//用于托管每一个用于显示的单个项目,注意它继承自RecyclerView.ViewHolder private Button mButton; private User mUser;//每一个项目有一个按钮和对应的用户信息,所以有这两个属性 public UserHolder(LayoutInflater inflater, ViewGroup parent) { super(inflater.inflate(R.layout.list_item, parent, false));//每一个项目在显示的时候都只是显示R.layout.list_item里面的按钮但实际上每一个按钮既有按钮,又有用户信息 mButton = (Button)itemView.findViewById(R.id.button);//Button这个按钮实例 mButton.setOnClickListener(new View.OnClickListener() {//设置布局管理器也是在Holder这里面设置的 @Override public void onClick(View view) { //因为Adapter的BindViewHolder将每一个User都绑定了,所以这里可以直接使用 Toast.makeText(getActivity(), "姓名:"+mUser.getName()+"年龄:"+mUser.getAge()+"性别:"+mUser.getSex()+"住址"+mUser.getAddress(), Toast.LENGTH_SHORT).show(); } }); } } private class UserAdapter extends RecyclerView.Adapter {//适配器,注意继承自RecyclerView.Adapter且内容为UserHolder @Override public UserHolder onCreateViewHolder(ViewGroup viewGroup, int i) { LayoutInflater inflater = LayoutInflater.from(getActivity()); return new UserHolder(inflater, viewGroup); } @Override public void onBindViewHolder(UserHolder soundHolder, int i) {//为每一个项目绑定对应的信息,对应每一个项目,参数UserHolder即为用于显示的每一个项目 User user = mUserList.get(i); soundHolder.mUser=user; soundHolder.mButton.setText(user.getName()); } @Override public int getItemCount() {//在RecycleView中显示的个数 return mUserList.size(); } } }