RecyclerView是新推出的View,ListView和GridView逐渐被抛弃,RecyclerView可以替代ListView和GridView。
使用时时需要倒包com.android.support.recyclerview.v7
用法和listView类似,需要adapter,viewHolder,但是viewHolder中的内容不需要自己写,只需要继承RecyclerView.ViewHolder。
可以实现横向/纵向布局的listview、GridView、瀑布流。
下面给出一个示例,其中还包括动画
package com.example.immediate.myrecyclerview;
import android.animation.ObjectAnimator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> mData;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mData = new ArrayList<>();
for (int i = 0;i<10;i++){
mData.add("第"+i+"项");
}
myAdapter = new MyAdapter();
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setAdapter(myAdapter);
}
/**
* 动画类
*/
class MyAnimator extends DefaultItemAnimator{
@Override
public void endAnimation(RecyclerView.ViewHolder item) {
super.endAnimation(item);
}
@Override
public boolean animateRemove(RecyclerView.ViewHolder holder) {
ObjectAnimator animator = ObjectAnimator.ofFloat(holder.itemView,"caleX",1,0).setDuration(2000);
animator.start();
return super.animateRemove(holder);
}
}
class MyAdapter extends RecyclerView.Adapter<MyHolder>{
@Override
public MyHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
View view = null;
switch (type){
case 0x23:
view = getLayoutInflater().inflate(R.layout.item_button,null);
break;
case 0x24:
view = getLayoutInflater().inflate(R.layout.item_imageview,null);
break;
}
MyHolder vh = new MyHolder(view);
return vh;
}
@Override
public void onBindViewHolder(MyHolder myHolder, final int position) {
if (position%2==0){
Button button = (Button) myHolder.itemView.findViewById(R.id.button);
button.setText(mData.get(position));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mData.remove(position);
notifyItemRemoved(position);
}
});
}else {
myHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mData.remove(position);
notifyItemRemoved(position);
}
});
}
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public int getItemViewType(int position) {
if (position%2==0){
return 0x23;
}else {
return 0x24;
}
// return super.getItemViewType(position);
}
}
class MyHolder extends RecyclerView.ViewHolder{
public MyHolder(View itemView) {
super(itemView);
}
}
}
布局文件
<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" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageview"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>