RecycleView的使用
RecyclerView是Google在API 21下support.V7包里的控件,用来替代ListView。
官网对RecycleView的描述为:A flexible view for providing a limited window into a large data set。
一、使用RecycleView的前提条件
想使用RecycleView,一定要在build.gradle中引入compile 'com.android.support:recyclerview-v7:24.0.0'依赖。
二、使用RecycleView的优缺点
优点:
- RecycleView强制封装ViewHolder
- 相当轻松的设置布局管理器以控制Item的布局方式,横向、竖向以及瀑布流方式
- 可设置Item操作的动画,删除或者添加等
- 通过ItemDecoration,控制Item间的间隔,可自己绘制
缺点:
- 需要自己实现OnItemClickListener点击事件(这么实用的需求,Google竟然让我们自己实现...)
三、RecycleView的具体实现
先来看下效果图:
image1image2
image3
使用方法如下:
- 在xml布局里面直接添加RecycleView控件(跟ListView差不多),在布局中我添加了一个Spinner按钮来选择不同的Item展示模式,两个Button按钮用来添加或者删除Item。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.furui.testrecycleview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"></Spinner>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/spinner"
android:text="add" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/b1"
android:text="delete" />
</RelativeLayout>
- Activity界面分析
- 1 先找到RecycleView控件,然后设置一个默认的Item显示模式,并将自己封装的Adapter和RecycleView绑定
mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(datas);
mRecyclerView.setAdapter(myRecycleViewAdapter);
- 2 设置item的点击事件,并设置点击Item时的动画效果
//设置item的点击事件
myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@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();
}
});
- 3 根据Spinner的选择,Item转换展示效果
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0){
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}else if (position == 1){
mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
}else if (position == 2)
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
}
下面是完整的Activity的代码:
package com.furui.testrecycleview;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private RecyclerView mRecyclerView;
private MyRecycleViewAdapter myRecycleViewAdapter;
private List<String> datas = new ArrayList<>();
private LinearLayoutManager mLinearLayoutManager;
private Spinner mSpinner;
private static final String[] m={"竖着的RecycleView","GridView类型的RecycleView","瀑布流"};
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
Button add = (Button) findViewById(R.id.b1);
Button delete = (Button) findViewById(R.id.b2);
add.setOnClickListener(this);
delete.setOnClickListener(this);
mSpinner = (Spinner) findViewById(R.id.spinner);
//将可选内容与ArrayAdapter连接起来
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 添加到spinner中
mSpinner.setAdapter(adapter);
//设置默认值
mSpinner.setVisibility(View.VISIBLE);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
// do not change the size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0){
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}else if (position == 1){
mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
}else if (position == 2)
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
for (int i = 0; i < 3 ; i++) {
datas.add("TestMyItem");
}
myRecycleViewAdapter = new MyRecycleViewAdapter(datas);
mRecyclerView.setAdapter(myRecycleViewAdapter);
//设置item的点击事件
myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@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 add(View view){
datas.add("TestMyItem");
int position = datas.size();
if (position > 0){
myRecycleViewAdapter.notifyDataSetChanged();
}
}
public void delete(View view){
int position = datas.size();
if (position > 0){
datas.remove(position - 1);
myRecycleViewAdapter.notifyDataSetChanged();
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.b1 :
add(view);
break;
case R.id.b2 :
delete(view);
break;
}
}
}
- RecycleView的Adapter和ListView的Adapter还是有点区别,来看看RecycleView的Adapter
package com.furui.testrecycleview;
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;
/**
* Created by Administrator on 2016/11/2.
*/
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {
private List<String> mData;
public MyRecycleViewAdapter(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);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mData.get(position)+ position);
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView textView ;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
textView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (itemClickListener != null){
itemClickListener.onItemClick(view,getPosition());
}
}
}
}
网友评论