Android RecyclerView实现listview效果
RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用。
据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我们并不陌生,例如:ListView、GridView。
那么有了ListView、GridView为什么还需要RecyclerView这样的控件呢?整体上看RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。
- 你想要控制其显示的方式,请通过布局管理器LayoutManager
- 你想要控制Item间的间隔(可绘制),请通过ItemDecoration
- 你想要控制Item增删的动画,请通过ItemAnimator
- 你想要控制点击、长按事件,请自己写(擦,这点尼玛。)
基本使用
鉴于我们对于ListView的使用特别的熟悉,对比下RecyclerView的使用代码:
<code class="language-java hljs has-numbering">mRecyclerView = findView(R.id.id_recyclerview); <span class="hljs-comment">//设置布局管理器</span> mRecyclerView.setLayoutManager(layout); <span class="hljs-comment">//设置adapter</span> mRecyclerView.setAdapter(adapter) <span class="hljs-comment">//设置Item增加、移除动画</span> mRecyclerView.setItemAnimator(<span class="hljs-keyword">new</span> DefaultItemAnimator()); <span class="hljs-comment">//添加分割线</span> mRecyclerView.addItemDecoration(<span class="hljs-keyword">new</span> DividerItemDecoration( getActivity(), DividerItemDecoration.HORIZONTAL_LIST));</code><ul style="display: block;" class="pre-numbering"></ul>
<code class="language-java hljs has-numbering">mRecyclerView = findView(R.id.id_recyclerview); <span class="hljs-comment">//设置布局管理器</span> mRecyclerView.setLayoutManager(layout); <span class="hljs-comment">//设置adapter</span> mRecyclerView.setAdapter(adapter) <span class="hljs-comment">//设置Item增加、移除动画</span> mRecyclerView.setItemAnimator(<span class="hljs-keyword">new</span> DefaultItemAnimator()); <span class="hljs-comment">//添加分割线</span> mRecyclerView.addItemDecoration(<span class="hljs-keyword">new</span> DividerItemDecoration( getActivity(), DividerItemDecoration.HORIZONTAL_LIST));</code>
ok,相比较于ListView的代码,ListView可能只需要去设置一个adapter就能正常使用了。而RecyclerView基本需要上面一系列的步骤,那么为什么会添加这么多的步骤呢?
那么就必须解释下RecyclerView的这个名字了,从它类名上看,RecyclerView代表的意义是,我只管Recycler View,也就是说RecyclerView只管回收与复用View,其他的你可以自己去设置。可以看出其高度的解耦,给予你充分的定制自由(所以你才可以轻松的通过这个控件实现ListView,GirdView,瀑布流等效果)。
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class BinderSampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_fragment);
FragmentActivityExt
.setContentFragment(this, R.id.container, BinderSampleFragment.newInstance());
}
}
package jp.satorufujiwara.binder.sample;
import android.os.Bundle;
import android.support.annotation.Nullable;
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 butterknife.ButterKnife;
import butterknife.InjectView;
import jp.satorufujiwara.binder.Section;
import jp.satorufujiwara.binder.recycler.RecyclerBinderAdapter;
public class BinderSampleFragment extends Fragment {
public static BinderSampleFragment newInstance() {
return new BinderSampleFragment();
}
@InjectView(R.id.recyclerView)
RecyclerView recyclerView;
private final RecyclerBinderAdapter<BinderSampleSection, BinderSampleViewType> adapter
= new RecyclerBinderAdapter<>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_binder_sample, container, false);
ButterKnife.inject(this, v);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
adapter.add(BinderSampleSection.SECTION_1, new DataBinder1(getActivity(), "in Section1"));
adapter.add(BinderSampleSection.SECTION_1, new DataBinder2(getActivity(), "in Section1"));
adapter.add(BinderSampleSection.SECTION_1, new DataBinder3(getActivity(), "in Section1"));
adapter.add(BinderSampleSection.SECTION_3, new DataBinder2(getActivity(), "in Section3"));
adapter.add(BinderSampleSection.SECTION_3, new DataBinder1(getActivity(), "in Section3"));
adapter.add(BinderSampleSection.SECTION_2, new DataBinder3(getActivity(), "in Section2"));
adapter.add(BinderSampleSection.SECTION_2, new DataBinder2(getActivity(), "in Section2"));
adapter.add(BinderSampleSection.SECTION_2, new DataBinder1(getActivity(), "in Section2"));
}
enum BinderSampleSection implements Section {
SECTION_1,
SECTION_2,
SECTION_3
}
}
源码下载:http://itlanbao.com/code/20150812/10050/100316.html