RecycleView介绍:
RecycleView是为了替换listView和gridView被创造出来的,他比listview有了更高的解耦,所以使用recycleView可以变得非常灵活,使用recycleView可以自定义分割线等其他属性
首先介绍recycleView的方法,这里是实现一个简单recycle的api调用:
RecyclerView.setAdapter(Adapter)
RecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
这里比ListView多了一个setLayoutManager,这个函数是用来设置Layout的类型的,在这里,系统默认提供了三种管理器LinearLayoutManager,GridLayoutManager,还有StaggeredGridLayoutManager(瀑布流),从这里就可以看出RecycleView的高度解耦和灵活性,还有一点,如果使用recycler就需要引用support v7 recycler支持jar包
代码实现:
实现代码:
package com.lc.store.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.lc.store.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jiangbo on 2016/4/2.
* Description
*/
public class CategoryFragment extends Fragment {
private TextView tv;
private RecyclerView rv;
List<String> list;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_category, container, false);
rv = (RecyclerView) inflate.findViewById(R.id.recycler);
tv = (TextView) inflate.findViewById(R.id.string);
initList();
initRecycleView();
return inflate;
}
private void initRecycleView() {
MAdapter mAdapter = new MAdapter();
rv.setAdapter(mAdapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
public void initList() {
list = new ArrayList<>();
for (int i = 'a'; i <= 'z'; i++) {
list.add((char) i + "");
}
}
class MAdapter extends RecyclerView.Adapter<MAdapter.ViewHolder> {
//创建Viewholder
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//注意,必须使用这个填充器
View inflater = LayoutInflater.from(getContext()).inflate(R.layout.string_item, parent, false);
//如果是这个填充器,那么item里面的math_parent会失效的,其他的填充器没有试过
//View inflater=LayoutInflater.from(getContext()).inflate(R.layout.string_item,null);
return new ViewHolder(inflater);
}
//绑定数据
public void onBindViewHolder(ViewHolder holder, int position) {
holder.alph.setText(list.get(position));
}
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView alph;
public ViewHolder(View itemView) {
super(itemView);
alph = (TextView) itemView.findViewById(R.id.string);
}
}
}
}
主布局文件fragment_category:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Item布局文件string_item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/brown"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/string"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/rosybrown"
android:gravity="center"
android:text="1" />
</FrameLayout>
所有的文件就这三个,和listview差不多
代码分析:
这里是用一个简单的fragment作为容器进行填充的,需要注意的是填充器的使用,不能传入空的root容器,这样会使itemlayout里面的mathParent失效
其实可以看出,大部分代码都是在Adapter上面,接下来就对这个adapter进行分析一下
class MAdapter extends RecyclerView.Adapter<MAdapter.ViewHolder>{
class ViewHolder extends RecyclerView.ViewHolder{
TextView alph;
public ViewHolder(View itemView) {
super(itemView);
alph= (TextView) itemView.findViewById(R.id.string);
}
不过这里他也需要继承RecyclerView中的ViewHolder类,然后实现它的抽象构造方法,我们只需要实现控件的绑定就对了,可以看得出来,这里recyclerview提供了一个itemview,可以直观的看出这个list中的一个条目
然后要实现自己的adapter还需要实现这几个方法:
public void onBindViewHolder(ViewHolder holder, int position) {
public int getItemCount() {
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
第一个方法就是绑定数据到viewholder中,第二个方法和以前实现adapter的方法一样,这里就不说了,第三个方法是用来创建viewholder对象的,代码也很简单易懂
布局分析:
布局文件也很简单,和listview差不多,也不用过多的分析
下面就是效果图:
这样弄出来的布局没分割线,而且在recyclerview中也没有分割线这个属性,所以需要自定义分割线
取巧分割线设置
这里自定义分割线太复杂了,基本都是绘图,我就使用了一个取巧一点的办法:
给recyclerView设置一个item的布局设置一个backgroud,然后再给textview设置一个间距,这样间距空出来的地方显示的就是layout的背景颜色
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<span style="font-size:18px;color:#ff0000;">android:background="@color/gold"</span>
android:layout_height="wrap_content" >
<TextView
android:id="@+id/string"
android:layout_width="match_parent"
android:layout_height="50dp"<span style="color:#ff0000;">
<span style="font-size:18px;">android:background="@color/rosybrown"</span></span>
android:gravity="center"
android:text="1"
android:layout_margin="5dp"/>
</FrameLayout>
其他
RecyclerView.addItemDecoration(new CustomerDecoration());
这里的customerDecoration需要自己创建,具体的实现方法也就不讲了
下面是常用的几个方法:
增加删除和增加item的动画效果
RecyclerView.setItemAnimator(new DefaultItemAnimator());
还有就是数据更新:
他比普通的Adapter多实现了几个方法,也可以很直观的看出方法的功能
这里比较常用的就是
notifyItemInserted和notifyItemMoved
这两个,使用这两个方法进行局部更新就可以不用更新整个dataset那样浪费资源了
Recyclerview基本使用就这样