标签:recyclerview 监听
RecyclerView是ListView的衍生而来的,它比ListView效率更高,使用起来也更加方便。
本文将会讲解用RecyclerView如何实现:垂直列表效果、横向列表效果、网格列表效果、瀑布流(横向与纵向),以及Item的点击监听(因为RecyclerView没有item的点击监听功能,这里是我自创的,感觉还算好用)。效果还算强大吧?哈哈,下面看源码
(1)主布局
<?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="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
(2)Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:textSize="60dp"
android:padding="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
(3)MainActivity代码
MainActivity AppCompatActivity { RecyclerView String[] = { }(Bundle savedInstanceState) { .onCreate(savedInstanceState)setContentView(R.layout.)= (RecyclerView) findViewById(R.id.).setHasFixedSize()StaggeredGridLayoutManager staggeredGridLayoutManager =StaggeredGridLayoutManager(StaggeredGridLayoutManager.).setLayoutManager(staggeredGridLayoutManager).setAdapter(MyAdapter())
(4)适配器Adapter
MyAdapter RecyclerView.Adapter<MyAdapter.ViewHolder>{ String[] MyAdapter(String[] mdatas){ =mdatas} ViewHolder (ViewGroup parentviewType) { View view=LayoutInflater.(parent.getContext()).inflate(R.layout.parent)ViewHolder viewHolder=ViewHolder(viewparent.getContext())viewHolder} (ViewHolder holderposition) { holder..setText([position])} () { .} ViewHolder RecyclerView.ViewHolder View.OnClickListener { TextView Context ViewHolder(View itemViewContext context) { (itemView)= (TextView) itemView.findViewById(R.id.).=contextitemView.setOnClickListener()} (View v) { Toast.(.getText().toString()Toast.).show()} } }
运行效果:
本文出自 “移动平台开发” 博客,请务必保留此出处http://liuxudong1001.blog.51cto.com/10877072/1715723
标签:recyclerview 监听
原文地址:http://liuxudong1001.blog.51cto.com/10877072/1715723