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;private List<String> datas = new ArrayList();private MyAdapter mAdapter; @Overrideprotected 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"); }4.适配器代码:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 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); } }}5、新建dimen.xml文件
<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 }
时间: 2024-11-04 14:44:46