Android中RecyclerView实现横向滑动代码
程序员文章站
2024-03-04 19:39:24
recyclerview 是android l版本中新添加的一个用来取代listview的sdk,它的灵活性与可替代性比listview更好。本文给大家介绍an...
recyclerview 是android l版本中新添加的一个用来取代listview的sdk,它的灵活性与可替代性比listview更好。本文给大家介绍android中recyclerview实现横向滑动代码,一起看看吧。
android.support.v7.widget.recyclerview
功能:recyclerview横向滑动
控件:<android.support.v7.widget.recyclerview />
java类:recyclerview、galleryadapter、list<integer>
1、主界面布局
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.info.activity.recycleractivity"> <android.support.v7.widget.recyclerview android:id="@+id/id_recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:layout_centervertical="true" android:background="#ff0000" android:scrollbars="none" ></android.support.v7.widget.recyclerview> </relativelayout>
2、子界面布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="120dp" android:layout_height="120dp"> <imageview android:id="@+id/id_index_gallery_item_image" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margin="5dp" android:scaletype="centercrop" /> <textview android:id="@+id/id_index_gallery_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/id_index_gallery_item_image" android:layout_centerhorizontal="true" android:layout_marginbottom="5dp" android:layout_margintop="5dp" android:textcolor="#000000" android:text="some info" android:textsize="12dp" /> </relativelayout>
3、java代码
package com.info.activity; import android.app.activity; import android.content.context; import android.support.v7.app.appcompatactivity; import android.os.bundle; 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.imageview; import android.widget.textview; import java.util.arraylist; import java.util.arrays; import java.util.list; public class recycleractivity extends activity { private recyclerview mrecyclerview; private galleryadapter madapter; private list<integer> mdatas; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recycler); initdatas(); //得到控件 mrecyclerview = (recyclerview) findviewbyid(r.id.id_recyclerview_horizontal); //设置布局管理器 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this); linearlayoutmanager.setorientation(linearlayoutmanager.horizontal); mrecyclerview.setlayoutmanager(linearlayoutmanager); //设置适配器 madapter = new galleryadapter(this, mdatas); mrecyclerview.setadapter(madapter); } private void initdatas() { mdatas = new arraylist<>(arrays.aslist(r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher)); } public class galleryadapter extends recyclerview.adapter<galleryadapter.viewholder> { private layoutinflater minflater; private list<integer> mdatas; public galleryadapter(context context, list<integer> datats) { minflater = layoutinflater.from(context); mdatas = datats; } public class viewholder extends recyclerview.viewholder { public viewholder(view arg0) { super(arg0); } imageview mimg; textview mtxt; } @override public int getitemcount() { return mdatas.size(); } /** * 创建viewholder */ @override public viewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = minflater.inflate(r.layout.activity_recycler_item, viewgroup, false); viewholder viewholder = new viewholder(view); viewholder.mimg = (imageview) view .findviewbyid(r.id.id_index_gallery_item_image); return viewholder; } /** * 设置值 */ @override public void onbindviewholder(final viewholder viewholder, final int i) { viewholder.mimg.setimageresource(mdatas.get(i)); } } }
以上所述是小编给大家介绍的android中recyclerview实现横向滑动代码,希望对大家有所帮助
推荐阅读
-
Android中RecyclerView实现横向滑动代码
-
Android中ScrollView实现滑动距离监听器的方法
-
Android实现listview滑动时渐隐渐现顶部栏实例代码
-
Android中RecyclerView布局代替GridView实现类似支付宝的界面
-
Android代码实现AdapterViews和RecyclerView无限滚动
-
Android中ViewPager实现滑动条及与Fragment结合的实例教程
-
Android实现评论栏随Recyclerview滑动左右移动
-
Android中RecyclerView布局代替GridView实现类似支付宝的界面
-
Android App中ListView仿QQ实现滑动删除效果的要点解析
-
Android中实现基本的短信拦截功能的代码示例