由于我ListView中的尝试就没按照《第一行代码》中的样例来,所以以下内容我都是找的几篇博客看的
与百分比布局类似,都要先添加依赖库
implementation 'com.android.support:recyclerview-v7:28.0.0'
RecyclerView的四大组成:
名 | 用途 |
---|---|
Layout Manager | Item的布局管理器 |
Adapter | 适配器,为Item提供数据 |
Item Decoration | Item之间的Divider(分割线) |
Item Animator | 添加、删除Item动画 |
从它类名上看,RecyclerView代表的意义是,我只管Recycler View,也就是说RecyclerView只管回收与复用View,其他的你可以自己去设置。可以看出其高度的**解耦**,给予你充分的定制自由(所以你才可以轻松的通过这个控件实现ListView,GirdView,瀑布流等效果)
步骤:
①先提前创建好设想好的Item布局文件并进行调整排版
②创建活动和与活动相关的布局文件,引入RecyclerView
③在活动中编写设置好适配器、布局管理器,有需要的话设置分割线,Item动画,将以上进行绑定连接
Activity:
package com.example.activitytest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 java.util.ArrayList;
import java.util.List;
public class SixActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> mDatas;
private Recycler_Adapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_six);
mDatas = new ArrayList<String>();
for (int i = 'A'; i <= 'Z'; i++)
{
mDatas.add("" + (char) i);
}
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new Recycler_Adapter());
}
class Recycler_Adapter
extends RecyclerView.Adapter<Recycler_Adapter.MyViewHolder>
{
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)
{
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
SixActivity.this).inflate(R.layout.words_item, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
holder.tv.setText(mDatas.get(position));
}
@Override
public int getItemCount()
{
return mDatas.size();
}
class MyViewHolder extends MyAdapter.ViewHolder
{
TextView tv;
public MyViewHolder(View view)
{
super(view);
tv = (TextView) view.findViewById(R.id.words_name);
}
}
}
}
与Activity相关的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SixActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:dividerHeight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
绑定的Item布局文件
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/words_name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25dp"/>
</RelativeLayout>
效果:
插入分割线(在Activity中添加)
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
大神的博客中有详细的讲了原理,然后他在原博客中说明添加的代码跟我这个有点出入
效果:
自定义分割线:
①在drawable中添加分割线的自定义xml文件(我这里是divider.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#ff00ff"
android:endColor="#ff0000ff"
android:startColor="#ffff0000"
android:type="linear"/>
<size
android:height="4dp"/>
</shape>
②在values下的style.xml中修改添加代码(就是让函数调用①中的自定义文件)
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:listDivider">@drawable/divider</item> //需要添加的
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
③效果:
备注:由于更改的是系统listDivider所调用的文件,所以ListView中的也会随之更改:
默认是纵向可滑动,如果想改成横向滑动,只需在设置LayoutManager之前调用一下函数即可:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
效果:
(2)GridLayoutManager
在设置布局管理器时用以下代码即可:
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
//3表示三列
mRecyclerView.setLayoutManager(gridLayoutManager);
效果:
(3)StaggeredGridLayoutManager
StaggeredGridLayoutManager staggeredGridLayoutManager = new
StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
//VERTICAL表示3列(纵向滑动),HORIZONTAL则为3行(横向滑动)
mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
效果与上面相同,也可以自定义设置每格大小(详见参考博客)