RecyclerView展示固定資料、上拉載入更多、下拉重新整理、點選事件、長按點選事件、刪除條目、重新整理條目、新增條目、多條目載入
1、依賴: implementation 'com.android.support:recyclerview-v7:27.0.2'
2、activity_main、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.recyclerviewdemo2.MainActivity">
<Button
android:onClick="btnAddItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="往RecyclerView中新增一個條目"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
3、item、
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:background="@color/colorAccent"
android:text="哈哈哈"/>
</LinearLayout>
4、item2、
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:drawableLeft="@mipmap/ic_launcher"
android:background="@android:color/holo_blue_dark"
android:text="哈哈哈"/>
</LinearLayout>
5、Mainactivity.java
package com.bwie.recyclerviewdemo2;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
//假資料
private List<String> list = new ArrayList<>();
private MyAdapter adapter;
private LinearLayoutManager linearLayoutManager;
private int lastItemPostion;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0;i < 20;++i){
list.add("我是 "+i);
}
//找到swipeRefreshLayout控制元件
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
recyclerView = findViewById(R.id.recyclerView);
//設定佈局管理器
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
//新增分割線
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
adapter = new MyAdapter(this,list);
recyclerView.setAdapter(adapter);
//設定新增、刪除動畫
recyclerView.setItemAnimator(new DefaultItemAnimator());
//條目點選 、 長按
//Google
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show();
adapter.removeItem(position);
}
});
//recyclerView的條目長按事件
adapter.setOnItemLongClickListener(new MyAdapter.onItemLongClickListener() {
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, "我是長按事件" + list.get(position), Toast.LENGTH_SHORT).show();
}
});
//實現RcyclerView的上拉重新整理
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState){
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://靜止狀態
//載入更多
if(adapter.getItemCount() - 1 == lastItemPostion){
//滑動到了最底部
List<String> result = new ArrayList<>();
for (int i = 0;i < 10; i++){
result.add("我是新來的"+i);
}
//新增更多
adapter.addMore(result);
Toast.makeText(MainActivity.this, "載入更多", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//找到第一個條目
//int firstItemPostision = linearLayoutManager.findFirstVisibleItemPosition();
lastItemPostion = linearLayoutManager.findLastVisibleItemPosition();
//System.out.println("firstItemPostion : "+firstItemPostision+"\t lastItemPosition : "+lastItemPostion);
}
});
//設定下拉重新整理圓圈顏色變換
swipeRefreshLayout.setColorSchemeColors(Color.BLUE,Color.YELLOW,Color.RED);
//實現recycerView的下拉重新整理
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "下拉了", Toast.LENGTH_SHORT).show();
//隱藏
swipeRefreshLayout.setRefreshing(false);
//模擬網路載入資料
}
},5000);
}
});
}
public void btnAddItem(View view){
adapter.addItem("世界,你好!");
}
}
6、Myadapter、
package com.bwie.recyclerviewdemo2;
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.AdapterView;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<String> list;
//條目點選監聽
private OnItemClickListener onItemClickListener;
//長按事件
private onItemLongClickListener onItemLongClickListener;
//常量
private static final int TYPE_ONE = 0;
private static final int TYPE_TWO = 1;
public MyAdapter(Context context, List<String> list){
this.context = context;
this.list = list;
}
//新增一個條目
public void addItem(String str){
list.add(0,str);
//重新整理介面卡
notifyDataSetChanged();
//notifyItemInserted();新增的時候,想要的動畫,必須呼叫這個方法
}
//刪除一個條目
public void removeItem(int postion)
{
list.remove(postion);
//notifyDataSetChanged();
notifyItemRemoved(postion);
}
//載入更多
public void addMore(List<String> resultList){
for (String str : resultList) {
list.add(str);
}
//重新整理介面卡
notifyDataSetChanged();
}
//條目點選事件
interface OnItemClickListener{
void onItemClick(View view,int position);
}
//條目點選事件
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
//長按事件
interface onItemLongClickListener{
void onItemLongClick(View view,int position);
}
//recycleVIew長按事件
public void setOnItemLongClickListener(onItemLongClickListener onItemLongClickListener){
this.onItemLongClickListener = onItemLongClickListener;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = null;
if(viewType == TYPE_ONE) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
viewHolder = new ViewHolder(view);
}else{
View view = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
viewHolder = new ViewHolder(view);
}
return viewHolder;
}
@Override
public void onBindViewHolder(final MyAdapter.ViewHolder holder, int position) {
//將資料繫結到控制元件上
holder.tvContent.setText(list.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//條目的點選事件
int index = holder.getLayoutPosition();
onItemClickListener.onItemClick(view,index);
}
});
//實現recycleView的條目長按事件
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//條目的位置獲取,應該由這個比較保險
int index = holder.getLayoutPosition();
onItemLongClickListener.onItemLongClick(view,index);
return true;//讓其消費掉事件
}
});
}
//得到多條目載入的型別
@Override
public int getItemViewType(int position) {
if(position %2 == 0){
return TYPE_ONE;
}else{
return TYPE_TWO;
}
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvContent;
public ViewHolder(View itemView) {
super(itemView);
tvContent = itemView.findViewById(R.id.tvContent);
}
}
}