android recyclerview
来源:互联网 发布:成年后不喜欢读书 知乎 编辑:程序博客网 时间:2024/06/04 19:56
- recyclerview需要导入support-v7使用
- recyclerview优点是切换样式容易一个可以顶ListView GridView使用
- recyclerview缺点是没有点击 长按事件需要自己写
- 插拔式的体验,高度的解耦,异常的灵活
导入
compile 'com.android.support:recyclerview-v7:25.+'
MainActivity
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); recyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); /** * 这里是控制RecyclerView的样式 * LinearLayoutManager 就类似listview 上到下的排列 * GridLayoutManager 就是Gridview 4是列数 * StaggeredGridLayoutManager 也像Gridview 可以控制上到下的排列 也可以左到右的排列 */// recyclerView.setLayoutManager(new LinearLayoutManager(this));// recyclerView.setLayoutManager(new GridLayoutManager(this, 4));// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL)); recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL));// recyclerView.setAdapter(animationListAdapter = new AnimationListAdapter(this, strings)); recyclerView.setAdapter(mAdapter = new RecyclerAdapter(this, strings)); recyclerView.setItemAnimator(new DefaultItemAnimator()); SpacesItemDecoration decoration = new SpacesItemDecoration(16); recyclerView.addItemDecoration(decoration); } protected void initData() { strings = new ArrayList<String>(); for (int i = 'a'; i <= 'z'; i++) { strings.add("" + (char) i); } }
Adapter
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> { private static final String TAG = "RecyclerAdapter"; private Context context; private List<String> strings; public RecyclerAdapter(Context context, List<String> strings) { this.context = context; this.strings = strings; } @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { Holder holder = new Holder(LayoutInflater.from(context).inflate(R.layout.item, parent, false)); return holder; } @Override public void onBindViewHolder(Holder holder, int position) { holder.textView.setText(strings.get(position)); } @Override public int getItemCount() { return strings.size(); } class Holder extends RecyclerView.ViewHolder { TextView textView; public Holder(View view) { super(view); textView = (TextView) view.findViewById(R.id.textView); } } }
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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.team.we.recyclerview.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#ffff0000" android:dividerHeight="10dp" /></android.support.constraint.ConstraintLayout>
itemlayout
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@color/colorPrimary" android:layout_height="150dp"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center_horizontal|center_vertical" /></RelativeLayout>
持续更新中,下次更新点击事件和item刷新 增删 item交换操作
demo下载
https://github.com/CzmZomBie/recyclerview
阅读全文
0 0
- Android RecyclerView
- android RecyclerView
- android-RecyclerView
- Android RecyclerView
- Android RecyclerView
- Android--RecyclerView
- Android RecyclerView
- Android RecyclerView
- android RecyclerView
- Android RecyclerView
- Android RecyclerView
- android RecyclerView
- Android RecyclerView
- Android RecyclerView
- [Android]RecyclerView
- Android RecyclerView
- Android RecyclerView
- android recyclerview
- 以安全的名义,为了那些高考的情怀和努力——
- Linux内核之双向通信Socketpair
- Faster RCNN算法详解
- bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月
- mybatis报Could not find result map java.lang.String
- android recyclerview
- Flume原理
- BZOJ4815: [Cqoi2017]小Q的表格
- Unity3D 如何在退出运行模式后保存修改数据
- CSUACM小组的成绩排名(用qsort函数对double排序需谨慎)
- Android 同步和消息机制
- 心电图各波及波段的组成
- 内存碎片/内存空洞
- UESTC 数据结构专题训练 D,E,F