RecyclerView 的优点
RecyclerView 不但可以通过设置 LayoutManager 来快速实现 ListView、GridView和瀑布流等效果,而且可以设置横向和纵向显示,并且添加动画效果也很常简单。
实现步骤
- 添加包的引用
implementation 'com.android.support:recyclerview-v7:27.0.2'
// 我用了 CardView 所以也加上
implementation 'com.android.support:cardview-v7:27.0.2'
- 在 XML 里添加 RecyclerView
我的 activity_main.xml 里把屏幕分成上下两部分,上半部分欲做竖向滑动,下半部分欲做横向滑动。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="han.kunpeng.recyclerviewdemo.MainActivity">
<android.support.constraint.Guideline
android:id="@+id/guide_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<!-- A vertical RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_vertical"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@id/guide_line"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- A horizontal RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_horizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guide_line" />
</android.support.constraint.ConstraintLayout>
- 在 Activity 中进行设置
private void findView() {
mRecyclerViewVertical = (RecyclerView) findViewById(R.id.recycler_view_vertical);
mRecyclerViewHorizontal = (RecyclerView) findViewById(R.id.recycler_view_horizontal);
}
private void initRecyclerView() {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerViewVertical.setHasFixedSize(true);
mRecyclerViewHorizontal.setHasFixedSize(true);
// use a linear layout manager - VERTICAL
mLinearLayoutManagerVertical = new LinearLayoutManager(this);
mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerViewLayoutManagerVertical = mLinearLayoutManagerVertical;
mRecyclerViewVertical.setLayoutManager(mRecyclerViewLayoutManagerVertical);
// use a linear layout manager - HORIZONTAL
mLinearLayoutManagerHorizontal = new LinearLayoutManager(this);
mLinearLayoutManagerHorizontal.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerViewLayoutManagerHorizontal = mLinearLayoutManagerHorizontal;
mRecyclerViewHorizontal.setLayoutManager(mRecyclerViewLayoutManagerHorizontal);
// specify an adapter (see also next example)
mRecyclerViewVertical.setAdapter(new BasicRecyclerViewAdapter(mDataset, VIEW_TYPE_ORIENTATION_VERTICAL));
mRecyclerViewHorizontal.setAdapter(new BasicRecyclerViewAdapter(mDataset, VIEW_TYPE_ORIENTATION_HORIZONTAL));
}
- 重点是实现 Adapter
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class BasicViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private TextView textView;
public BasicViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view_info);
}
}
// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
// create a new view
if (VIEW_TYPE_ORIENTATION_VERTICAL == mOrientation) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vertical, parent, false);
} else if (VIEW_TYPE_ORIENTATION_HORIZONTAL == mOrientation) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal, parent, false);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
}
// set the view's size, margins, paddings and layout parameters
//...
return new BasicViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
if (holder instanceof BasicViewHolder) {
((BasicViewHolder) holder).textView.setText(mDataset.get(position));
} else {
Log.d("HHHH", "onBindViewHolder - holder instanceof");
}
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
完整代码
延伸阅读
RecyclerView | Android Developers
Recycler View | Android Developers
Creating Lists and Cards | Android Developers