参考
RecyclerView技术栈
Android RecyclerView 使用完全解析 体验艺术般的控件
[Android]使用RecyclerView替代ListView(一)
一、
RecyclerAdapter.java:
package com.xys.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class RecyclerAdapter
extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<String> mData;
public RecyclerAdapter(List<String> data) {
mData = data;
}
public OnItemClickListener itemClickListener;
public void setOnItemClickListener(
OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
textView.setOnClickListener(this);
}
// 通过接口回调来实现RecyclerView的点击事件
@Override
public void onClick(View v) {
if (itemClickListener != null) {
itemClickListener.onItemClick(v, getPosition());
}
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// 将布局转化为View并传递给RecyclerView封装好的ViewHolder
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.rc_item, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
// 建立起ViewHolder中视图与数据的关联
viewHolder.textView.setText(mData.get(i) + i);
}
@Override
public int getItemCount() {
return mData.size();
}
}
layout/rc_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:text="111"
android:textSize="40sp"
android:gravity="center"
android:layout_width="match_parent"
android:background="#bebebe"
android:layout_margin="3dp"
android:layout_height="match_parent">
</TextView>
RecyclerTest.java
package com.xys.myapplication;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class RecyclerTest extends Activity {
private RecyclerView mRcList;
private RecyclerAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Spinner mSpinner;
private List<String> mData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler);
mRcList = (RecyclerView) findViewById(R.id.rc_list);
mLayoutManager = new LinearLayoutManager(this);
mRcList.setLayoutManager(mLayoutManager);
mRcList.setHasFixedSize(true);
// 设置显示动画
mRcList.setItemAnimator(new DefaultItemAnimator());
mSpinner = (Spinner) findViewById(R.id.spinner);
mSpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view,
int position,
long id) {
if (position == 0) {
mRcList.setLayoutManager(
// 设置为线性布局
new LinearLayoutManager(
RecyclerTest.this));
} else if (position == 1) {
mRcList.setLayoutManager(
// 设置为表格布局
new GridLayoutManager(
RecyclerTest.this, 3));
} else if (position == 2) {
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 增加测试数据
mData.add("Recycler");
mData.add("Recycler");
mData.add("Recycler");
mAdapter = new RecyclerAdapter(mData);
mRcList.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(
new RecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(final View view, int position) {
// 设置点击动画
view.animate()
.translationZ(15F).setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.animate()
.translationZ(1f)
.setDuration(500).start();
}
}).start();
}
});
}
public void addRecycler(View view) {
mData.add("Recycler");
int position = mData.size();
if (position > 0) {
mAdapter.notifyDataSetChanged();
}
}
public void delRecycler(View view) {
int position = mData.size();
if (position > 0) {
mData.remove(position - 1);
mAdapter.notifyDataSetChanged();
}
}
}