RecycleView的使用
RecyclerView是Google在API 21下support.V7包里的控件,用來(lái)替代ListView竖慧。
官網(wǎng)對(duì)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的優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
- RecycleView強(qiáng)制封裝ViewHolder
- 相當(dāng)輕松的設(shè)置布局管理器以控制Item的布局方式顽决,橫向窍仰、豎向以及瀑布流方式
- 可設(shè)置Item操作的動(dòng)畫汉规,刪除或者添加等
- 通過ItemDecoration,控制Item間的間隔辈赋,可自己繪制
缺點(diǎn):
- 需要自己實(shí)現(xiàn)OnItemClickListener點(diǎn)擊事件(這么實(shí)用的需求鲫忍,Google竟然讓我們自己實(shí)現(xiàn)...)
三、RecycleView的具體實(shí)現(xiàn)
先來(lái)看下效果圖:
使用方法如下:
- 在xml布局里面直接添加RecycleView控件(跟ListView差不多)钥屈,在布局中我添加了一個(gè)Spinner按鈕來(lái)選擇不同的Item展示模式悟民,兩個(gè)Button按鈕用來(lái)添加或者刪除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控件篷就,然后設(shè)置一個(gè)默認(rèn)的Item顯示模式射亏,并將自己封裝的Adapter和RecycleView綁定
mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(datas);
mRecyclerView.setAdapter(myRecycleViewAdapter);
- 2 設(shè)置item的點(diǎn)擊事件,并設(shè)置點(diǎn)擊Item時(shí)的動(dòng)畫效果
//設(shè)置item的點(diǎn)擊事件
myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onItemClick(final View view, int position) {
//設(shè)置點(diǎn)擊動(dòng)畫
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 根據(jù)Spinner的選擇竭业,Item轉(zhuǎn)換展示效果
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);
//將可選內(nèi)容與ArrayAdapter連接起來(lái)
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
//設(shè)置下拉列表的風(fēng)格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//將adapter 添加到spinner中
mSpinner.setAdapter(adapter);
//設(shè)置默認(rèn)值
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);
//設(shè)置item的點(diǎn)擊事件
myRecycleViewAdapter.setOnItemClickListener(new MyRecycleViewAdapter.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onItemClick(final View view, int position) {
//設(shè)置點(diǎn)擊動(dòng)畫
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還是有點(diǎn)區(qū)別智润,來(lái)看看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());
}
}
}
}