Activity布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="example.com.recyclerviewdemo.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Item的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="hello" />
</LinearLayout>
</RelativeLayout>
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<String> mDatas;
private Context context;
public RecyclerViewAdapter(Context context, List<String> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tv_item.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas == null ? 0 : mDatas.size();
}
public void addData(int position, String s) {
mDatas.add(position,s);
notifyItemInserted(position);
}
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}
public void addItem(List<String> newDatas) {
mDatas.addAll(0,newDatas);
notifyDataSetChanged();
}
public void addMoreData(List<String> newDatas) {
mDatas.addAll(newDatas);
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_item;
public ViewHolder(View itemView) {
super(itemView);
tv_item = (TextView) itemView.findViewById(R.id.tv_item);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView recyclerView;
private List<String> mDatas;
private RecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
recyclerViewAdapter = new RecyclerViewAdapter(this, mDatas);
recyclerView.setAdapter(recyclerViewAdapter);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recylerview_id);
}
private void initData() {
mDatas = new ArrayList<>();
for (int i = 'A'; i <= 'z'; i++) {
mDatas.add((char) i+"");
}
}
}
添加分割线
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class DividerItemDecoration extends RecyclerView.ItemDecoration{
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
MainActivity加上
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
设置item点击事件
在RecyclerViewAdapter 设置回调接口
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<String> mDatas;
private Context context;
...
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_item;
public ViewHolder(View itemView) {
super(itemView);
tv_item = (TextView) itemView.findViewById(R.id.tv_item);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListenter!=null){
onItemClickListenter.OnItemClick(v,getLayoutPosition(),mDatas.get(getLayoutPosition()));
}
}
});
}
}
public interface OnItemClickListenter{
void OnItemClick(View v,int position,String data);
}
private OnItemClickListenter onItemClickListenter;
public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter) {
this.onItemClickListenter = onItemClickListenter;
}
}
MainActivity调用
recyclerViewAdapter.setOnItemClickListenter(new RecyclerViewAdapter.OnItemClickListenter() {
@Override
public void OnItemClick(View v, int position, String data) {
Toast.makeText(MainActivity.this, "position==" + position + " Data--" + data, Toast.LENGTH_SHORT).show();
}
});
添加删除item项
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<String> mDatas;
private Context context;
...
public void addData(int position, String s) {
mDatas.add(position,s);
notifyItemInserted(position);
}
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}
...
}
MainActivity
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
recyclerViewAdapter.addData(0, "new Data");
//定位到第0个位置
recyclerView.scrollToPosition(0);
break;
case R.id.btn_remove:
recyclerViewAdapter.removeData(0);
break;
}
}
XRecyclerView的使用
https://github.com/jianghejie/XRecyclerView
public class MainActivity extends AppCompatActivity {
private XRecyclerView mRecyclerView;
private ArrayList<String> mDatas;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatas = new ArrayList<>();
for (int i = 'A'; i < 'z'; i++) {
mDatas.add((char) i + "");
}
mRecyclerView = (XRecyclerView) findViewById(R.id.recycleview);
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallPulse );
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new MyAdapter(mDatas);
mRecyclerView.setAdapter(mAdapter);
View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
mRecyclerView.addHeaderView(header);
mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
mDatas.clear();
for (int i = 0; i < 15; i++) {
mDatas.add("item" + i + "after times of refresh");
}
mAdapter.notifyDataSetChanged();
mRecyclerView.refreshComplete();
}
}, 1000); //refresh data here
}
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable() {
public void run() {
for (int i = 0; i < 4; i++) {
mDatas.add("item" + (1 + mDatas.size()));
}
mRecyclerView.loadMoreComplete();
mAdapter.notifyDataSetChanged();
}
}, 1000);
}
});
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position, String s) {
Toast.makeText(MainActivity.this,position+" "+s,Toast.LENGTH_SHORT).show();
}
});
}
}