使用RecyclerView需要在gradle里面添加依赖。
compile 'com.android.support:recyclerview-v7:24.2.1'
下面直接给代码,代码有详细注释
布局文件
.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent" >
.support.v7.widget.RecyclerView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null"
android:scrollbars="vertical" />
.support.v4.widget.SwipeRefreshLayout>
item布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_text"/>
LinearLayout>
Activity
package wnl.myndk;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.TypedValue;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import wnl.myndk.adapter.MyRecyclerViewAdapter;
public class SwipeActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshWidget;
private RecyclerView mRecyclerView;
private GridLayoutManager gridLayoutManager;
private StaggeredGridLayoutManager staggeredGridLayoutManager;
private LinearLayoutManager linearLayoutManager;
private MyRecyclerViewAdapter adapter;
private String[] dataArrary = {"Android", "JavaWeb", "NDK", "NodeJs", "Android", "JavaWeb", "NDK", "NodeJs"
, "Android", "JavaWeb", "NDK", "NodeJs", "Android", "JavaWeb", "NDK", "NodeJs", "Android", "JavaWeb", "NDK", "NodeJs"};
private List datas = new ArrayList(Arrays.asList(dataArrary));
private static final int COMPLETE_REFRESHING = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.arg1) {
case COMPLETE_REFRESHING:
Toast.makeText(SwipeActivity.this, "刷新成功", Toast.LENGTH_SHORT).show();
mSwipeRefreshWidget.setRefreshing(false);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
mRecyclerView = (RecyclerView) findViewById(android.R.id.list);
mSwipeRefreshWidget.setColorSchemeColors(getResources().getColor(R.color.colorAccent),
getResources().getColor(R.color.colorPrimary),
getResources().getColor(R.color.white));
mSwipeRefreshWidget.setOnRefreshListener(this);
// 这句话是为了,第一次进入页面的时候显示加载进度条
mSwipeRefreshWidget.setProgressViewOffset(false, 0, (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
.getDisplayMetrics()));
/**
* 设置RecyclerView
*/
/*
* 设置mRecyclerView的布局管理器,用来控制item的布局
*/
linearLayoutManager = new LinearLayoutManager(this);
// linearLayoutManager.setOrientation(0); 横向
linearLayoutManager.setOrientation(1); //竖向
gridLayoutManager = new GridLayoutManager(this, 3);//网格布局
staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 0);
mRecyclerView.setLayoutManager(linearLayoutManager);
// mRecyclerView.setLayoutManager(gridLayoutManager);
// mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
adapter = new MyRecyclerViewAdapter(datas);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
@Override
public void onItemClick(View view, String data) {
Toast.makeText(SwipeActivity.this, data + "", Toast.LENGTH_SHORT).show();
// 删除item
adapter.removeItem(data);
// 添加item
adapter.addItem(data+"h",0);
}
});
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//Toast.makeText(SwipeActivity.this, "onScrollStateChanged", Toast.LENGTH_SHORT).show();
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//Toast.makeText(SwipeActivity.this, "onScrolled", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
Message msg = Message.obtain();
msg.arg1 = COMPLETE_REFRESHING;
handler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
RecyclerView的适配器
package wnl.myndk.adapter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import wnl.myndk.R;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter implements View.OnClickListener {
private List datas;
public MyRecyclerViewAdapter(List datas) {
this.datas = datas;
}
//创建新View,被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_viewholder, parent, false);
ViewHolder vh = new ViewHolder(view);
//注册点击事件
view.setOnClickListener(this);
return vh;
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//获取ViewHolder在onCreateViewHolder绑定的View
View view = holder.itemView;
TextView textView = (TextView) view.findViewById(R.id.item_text);
textView.setText(datas.get(position));
//将数据保存在itemView的Tag中,以便点击时进行获取
holder.itemView.setTag(datas.get(position));
Log.e("position==", position + "");
}
@Override
public int getItemCount() {
return datas.size();
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
/**
* item 的点击事件接口
*/
public static interface OnRecyclerViewItemClickListener {
void onItemClick(View view, String data);
}
//定义完接口,添加接口和设置Adapter接口的方法
private OnRecyclerViewItemClickListener mOnItemClickListener = null;
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public void onClick(View v) {
//注意这里使用getTag方法获取数据
mOnItemClickListener.onItemClick(v, (String) v.getTag());
}
/**
* 添加item
*/
public void addItem(String data, int position) {
datas.add(position, data);
notifyItemInserted(position);
}
/**
* 删除item
*/
public void removeItem(String data) {
int index = datas.indexOf(data);
datas.remove(index);
notifyItemRemoved(index);
}
}