简介:
RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,但是直接把viewholder的实现封装起来,用户只要实现自己的viewholder就可以了,该组件会自动帮你回收复用每一个item。它不但变得更精简,也变得更加容易使用,而且更容易组合设计出自己需要的滑动布局。要使用
RecyclerView,请参考 ,其实你也可以只下载一个jar包,添加到自己的libs里就能使用它了Recycler.jar这个包。
推荐使用理由:
RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.
简单说,它是ListView的进化,为了当你需要动态展示一组数据的时候就会需要它。
当然,如果只是动态展示数据,listview也可以做到,用它替代listview的原因有几个:
-简介中提到的它封装了viewholder的回收复用。-RecyclerView使用布局管理器管理子view的位置(目前尚只提供了LinearLayoutManager),也就是说你再不用拘泥于ListView的线性展示方式,如果之后提供其他custom
- LayoutManager的支持,你能够使用复杂的布局来展示一个动态组件。
-自带了ItemAnimation,可以设置加载和移除时的动画,方便做出各种动态浏览的效果。
使用案例一:普通的 listview 展示类型
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:scrollbars="vertical" />
</LinearLayout>
③bean 对象(ps:这里只是举例,可根据具体对象来实现)
package com.demo.bean;
import java.io.Serializable;
public class Recycler implements Serializable {
private static final long serialVersionUID = 1L;
private int imageId;
private String title;
private String time;
public Recycler(int imageId, String title, String time) {
super();
this.imageId = imageId;
this.title = title;
this.time = time;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
package com.demo.adapter;
import java.util.List;
import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.demo.bean.Recycler;
import com.demo.ui.R;
/**
* 适配器
*
* @author zengtao 2015年7月17日 下午3:27:17
*
*/
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<Recycler> list;
public RecycleAdapter(List<Recycler> list) {
this.list = list;
}
@Override
public int getItemCount() {
return list.isEmpty() ? 0 : list.size();
}
@SuppressLint("InflateParams")
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, null);
ViewHolder vh = new ViewHolder(view);
vh.recycler_item_image = (ImageView) view.findViewById(R.id.recycler_item_image);
vh.recycler_item_name = (TextView) view.findViewById(R.id.recycler_item_name);
vh.recycler_item_time = (TextView) view.findViewById(R.id.recycler_item_time);
return vh;
}
/**
* 设置值
*/
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
final Recycler rc = list.get(i);
viewHolder.recycler_item_image.setImageResource(rc.getImageId());
viewHolder.recycler_item_name.setText(rc.getTitle());
viewHolder.recycler_item_time.setText(rc.getTime());
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView recycler_item_image;
private TextView recycler_item_name, recycler_item_time;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
package com.demo.ui;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.demo.adapter.RecycleAdapter;
import com.demo.bean.Recycler;
/**
* RecyclerView 演示
*
* @author zengtao 2015年7月27日 下午4:06:40
*
*/
public class MainActivity extends Activity {
/**
* RecyclerView 所需
*/
private RecyclerView recyclerView;
private RecycleAdapter adapter;
private LinearLayoutManager manager;
private List<Recycler> list;
/**
* 图片id
*/
private int[] imageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initView();
initData();
}
/**
* 初始化控件
*/
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
/**
* 初始化数据
*/
private void initData() {
// 1.获取图片的数据
imageId = new int[] { R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5, R.drawable.image_6, R.drawable.image_7 };
list = new ArrayList<Recycler>();
for (int i = 0; i < imageId.length; i++) {
list.add(new Recycler(imageId[i], "羊信息", "2015-7-29 15:3" + i));
}
// 2.设置布局管理器
manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
// 3.设置适配器
adapter = new RecycleAdapter(list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
使用案例二:横向滚动代替gridview实现滚动效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/recycler_item_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_vertical"
android:src="@drawable/image_1" />
<TextView
android:id="@+id/recycler_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="羊羊羊"
android:textColor="#ff00ff"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
package com.demo.adapter;
import java.util.List;
import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.demo.bean.Recycler;
import com.demo.ui.R;
/**
* 适配器
*
* @author zengtao 2015年7月17日 下午3:27:17
*
*/
public class RecycleAdapterH extends RecyclerView.Adapter<RecycleAdapterH.ViewHolder> {
private List<Recycler> list;
public RecycleAdapterH(List<Recycler> list) {
this.list = list;
}
@Override
public int getItemCount() {
return list.isEmpty() ? 0 : list.size();
}
@SuppressLint("InflateParams")
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_h, null);
ViewHolder vh = new ViewHolder(view);
vh.recycler_item_image = (ImageView) view.findViewById(R.id.recycler_item_image);
vh.recycler_item_name = (TextView) view.findViewById(R.id.recycler_item_name);
return vh;
}
/**
* 设置值
*/
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
final Recycler rc = list.get(i);
viewHolder.recycler_item_image.setImageResource(rc.getImageId());
viewHolder.recycler_item_name.setText(rc.getTitle());
viewHolder.recycler_item_image.setImageResource(list.get(i).getImageId());
// 如果设置了回调,则设置点击事件
if (mOnItemClickLitener != null) {
viewHolder.recycler_item_image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickLitener.onItemClick(viewHolder.recycler_item_image, i);
}
});
}
}
/**
* ItemClick的回调接口
*/
public interface OnItemClickLitener {
void onItemClick(View view, int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView recycler_item_image;
private TextView recycler_item_name;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
package com.demo.ui;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.demo.adapter.RecycleAdapterH;
import com.demo.adapter.RecycleAdapterH.OnItemClickLitener;
import com.demo.bean.Recycler;
/**
* RecyclerView 演示
*
* @author zengtao 2015年7月27日 下午4:06:40
*
*/
public class MainActivity extends Activity {
/**
* RecyclerView 所需
*/
private RecyclerView recyclerView;
private RecycleAdapterH adapter;
private LinearLayoutManager manager;
private List<Recycler> list;
/**
* 图片id
*/
private int[] imageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initView();
initData();
}
/**
* 初始化控件
*/
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
}
/**
* 初始化数据
*/
private void initData() {
// 1.获取图片的数据
imageId = new int[] { R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5, R.drawable.image_6, R.drawable.image_7 };
list = new ArrayList<Recycler>();
for (int i = 0; i < imageId.length; i++) {
list.add(new Recycler(imageId[i], "羊信息"));
}
// 2.设置布局管理器
manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(manager);
// 3.设置适配器
adapter = new RecycleAdapterH(list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
adapter.setOnItemClickLitener(new OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
}
});
}
}