最開始接觸RecyclerView的時候,百度各種教程,感覺都很復雜,不容易看懂,或許是講的比較深,這里不談RecyclerView的好處,只談最基本的使用,讓他能跑起來再說。
效果圖

recyclerView.gif
首先在Gradle中引入依賴:compile 'com.android.support:design:28.0.0'
上java目錄截圖和xml截圖

項目目錄.png
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);//適配
/**
* 回到頂部監(jiān)聽
*/
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;
}
/**
* 創(chuàng)建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的使用基礎,應該能很容易看懂這個。
如果有什么看不懂的,可以在下方評論,我會盡量回答
注:有什么地方有問題的,希望各位不吝賜教,多謝!