最开始接触RecyclerView的时候,百度各种教程,感觉都很复杂,不容易看懂,或许是讲的比较深,这里不谈RecyclerView的好处,只谈最基本的使用,让他能跑起来再说。
效果图
首先在Gradle中引入依赖:compile 'com.android.support:design:28.0.0'
上java目录截图和xml截图
xml:
fragment_02.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#56abe4"
android:fontFamily="cursive"
android:gravity="center"
android:text="RecyclerView"
android:textColor="#FFF"
android:textSize="34sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"></android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/backTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="50dp"
android:text="回到顶部"
android:textColor="#931f26"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
recyclerview_fg2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:src="@drawable/girl" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="20dp" />
<TextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
java
LinkMan(实体类)
package com.example.vulcan.newstudiodemo.fragment.recyclerview;
public class LinkMan {
/**
* 姓名
*/
private String name;
/**
* 电话号码
*/
private String phone;
public LinkMan(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Fragment2:
package com.example.vulcan.newstudiodemo.fragment.recyclerview;
import android.os.Bundle;
import android.support.annotation.NonNull;
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 android.widget.TextView;
import com.example.vulcan.newstudiodemo.R;
import java.util.ArrayList;
import java.util.List;
public class Fragment2 extends Fragment {
private TextView backTop;//回到顶部按钮
private RecyclerView recyclerView;
AdapterRecycler adapterRecycler;//定义适配器
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_02, null);
initView(view);
//region 数据模拟
List<LinkMan> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add(new LinkMan("莉莉" + i, "123456789"));
}
//endregion
adapterRecycler = new AdapterRecycler(getActivity(), list);
// recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//设置布局为线性布局
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));//设置布局为网格布局,第二个参数3代表每行三个
recyclerView.setAdapter(adapterRecycler);//适配
/**
* 回到顶部监听
*/
backTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recyclerView.scrollToPosition(0);
}
});
return view;
}
/**
* 控件初始化
* @param view
*/
private void initView(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
backTop = (TextView) view.findViewById(R.id.backTop);
}
}
AdapterRecycler:
package com.example.vulcan.newstudiodemo.fragment.recyclerview;
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.TextView;
import com.example.vulcan.newstudiodemo.R;
import java.util.List;
public class AdapterRecycler extends RecyclerView.Adapter<AdapterRecycler.VH> {
Context context;
List<LinkMan> list;
/**
* 构造方法,获得上下文和数据集合
*
* @param context
* @param list
*/
public AdapterRecycler(Context context, List<LinkMan> list) {
this.context = context;
this.list = list;
}
/**
* 创建ViewHolder,解析XML
*
* @param viewGroup
* @param i
* @return
*/
public VH onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_fg2, null);
return new VH(view);
}
/**
* 操作列表项
*
* @param vh
* @param i
*/
@Override
public void onBindViewHolder(VH vh, int i) {
vh.name.setText(list.get(i).getName());
vh.phone.setText(list.get(i).getPhone());
}
/**
* 获得列表项的数量
*
* @return
*/
@Override
public int getItemCount() {
return list.size();
}
/**
* 控件初始化
*/
public static class VH extends RecyclerView.ViewHolder {
TextView name;
TextView phone;
public VH(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
phone = itemView.findViewById(R.id.phone);
}
}
}
代码中注释的比较清晰了,如果有ListView的使用基础,应该能很容易看懂这个。
如果有什么看不懂的,可以在下方评论,我会尽量回答
注:有什么地方有问题的,希望各位不吝赐教,多谢!