RecyclerView的简单使用方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView的简单使用方法相关的知识,希望对你有一定的参考价值。
1、在build中引入compile ‘com.android.support:recyclerview-v7:25.3.1‘
2、在activity中布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycylview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
item.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="20dp"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_gravity="center"
android:gravity="center"
android:text="Hello"
android:textSize="20dp"
android:padding="20dp"
android:textColor="#fff"/>
</LinearLayout>
3.在MainActivity中代码如下:
private RecyclerView mRecyclerView;4.适配器代码:
private List<String> datas = new ArrayList();
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycylview);
mAdapter = new MyAdapter(datas);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));//可以设置列表模式
mRecyclerView.addItemDecoration(new MyItemDacoration(this));//加载列表样式
initDatas();
}
private void initDatas(){
datas.add("New York");
datas.add("Boston");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
datas.add("New York");
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {5、新建dimen.xml文件
private List<String> mDatas ;
private MyAdapter mAdapter;
private LayoutInflater inflater;
public MyAdapter(List<String> datas){
mDatas =datas;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {//绑定数据
holder.textView.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
}
}
}<dimen name="divider">10dp</dimen> />
6.新建Java文件设置列表样式:
public class MyItemDacoration extends RecyclerView.ItemDecoration {
private int divider;
public MyItemDacoration(Context context) {
//即你要设置的分割线的宽度 --这里设为10dp
divider = context.getResources().getDimensionPixelSize(R.dimen.divider);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
// outRect.left = divider; //相当于 设置 left padding
// outRect.top = divider; //相当于 设置 top padding
// outRect.right = divider; //相当于 设置 right padding
outRect.bottom = divider; //相当于 设置 bottom padding
}
以上是关于RecyclerView的简单使用方法的主要内容,如果未能解决你的问题,请参考以下文章
我们如何在 android 的 RecyclerView 片段中使用 bottomSheet?
如何从 Firebase 获取数据到 Recyclerview 中的片段?