记得以前刚学完android的时候去面试,总会被问到listview怎么优化啊!当时就说一下复用啊等等。大家都知道,我们平时写listview或gridview的时候,都会用到ViewHolder来复用item的布局优化性能。recyvleview 已经出现很久啦,android5.0以后google推出了recyvleview 它将viewholder官方画内部实现更好的性能优化,
更牛的是通过设置其LayoutManager只需一句话就可以将它变成gridview,变成瀑布流。
通过设置其ItemDecoration 可以随意定义每个item之间的分割线。
通过设置其ItemAnimator,还可以设置其增加删除时候的动画。很强大的一个控件。
简单应用:
<?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:fitsSystemWindows="true"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
package com.chs.myrecycleview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.chs.myrecycleview.adapter.MyAdapter;
import com.chs.myrecycleview.adapter.RecycleViewDivider;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<String> mData;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
mData = new ArrayList<>();
for (int i = 0; i < 100; i++) {
mData.add("item---" + i);
}
}
private void initView() {
myAdapter = new MyAdapter(this,mData);
recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置其布局管理是listview gridview 还是瀑布流
recyclerView.setAdapter(myAdapter);
recyclerView.addItemDecoration(new RecycleViewDivider(this));
}
}
adapter:
package com.chs.myrecycleview.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.chs.myrecycleview.R;
import java.util.List;
/**
* 作者:chs on 2016/2/2 14:43
* 邮箱:657083984@qq.com
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<String> mData;
public MyAdapter(Context mContext,List<String> list) {
this.mContext = mContext;
this.mData = list;
}
//创建view
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
//绑定完成之后赋值
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_text.setText(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{ //自定义viewholder 初始化控件
TextView tv_text;
public MyViewHolder(View itemView) {
super(itemView);
tv_text = (TextView) itemView.findViewById(R.id.tv_text);
}
}
}
item:
<?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="wrap_content">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20sp"
android:background="@color/gray"
android:text="hahah"
android:gravity="center"
/>
</LinearLayout>
这样一个最基本的的recycleview 就实现了 效果图:
但是我的item中的布局明明设置的是match_parent但是怎么显示的是图上那样呢 match_parent失效了?
后来就在代码中动态设置了下:
在adapter的下面方法中
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
这样就OK啦 后来又发现一个更好的方法:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
效果:
现在感觉就好多啦,但是我们还发现每个item之间没有分割线,如果你去给Recycleview去设置分割线,会发现没有像listview中的android:divider 和android:dividerHeight属性,那怎么给它设置分割线呢 且听下回分解