RecyclerView實(shí)現(xiàn)縱向和橫向滾動(dòng)
為方便自己以后學(xué)習(xí),自己記錄學(xué)習(xí),大家也可以參考,有什么問題一起探討。
今天學(xué)習(xí)RecyclerView,下邊來說一下實(shí)現(xiàn)數(shù)據(jù)垂直滾動(dòng)和數(shù)據(jù)橫向滾動(dòng)。先上圖為敬:
所用工具:Android Studio
縱向滾動(dòng)
1、添加依賴庫(kù):
打開app/build.gradle文件,在dependencies閉包中添加如下內(nèi)容(compile 'com.android.support:recyclerview-v7:24.2.1'為添加的內(nèi)容)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.0-alpha1' compile 'com.android.support:recyclerview-v7:24.2.1' testCompile 'junit:junit:4.12' }
添加完之后點(diǎn)擊一下Sync Now來進(jìn)行同步;
2、修改activity_main.xml中的代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dell.practice_recyclerview.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/id_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
因?yàn)镽ecyclerView不是內(nèi)置在系統(tǒng)SDK中的,所以需要把完整的包路徑寫出來。
3、新建實(shí)體類,這里以Book類作為演示:
package com.example.dell.practice_recyclerview; /** * Created by dell on 2018/6/3. * Created by qiyueqing on 2018/6/3. */ public class Book { private String name; private int imageId; public Book(String name, int imageId) { this.name = name; this.imageId = imageId; } public String getName() { return name; } public int getImageId() { return imageId; } }
4、為L(zhǎng)istView的子項(xiàng)制定一個(gè)我們自定義的布局:
在layout目錄下新建book_item.xml
<?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="wrap_content"> <ImageView android:id="@+id/id_book_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/id_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/> </LinearLayout>
5、為RecyclerView準(zhǔn)備一個(gè)適配器:
新建BookAdapter類,讓這個(gè)類繼承RecyclerView.Adapter,并將泛型指定為BookAdapter.ViewHolder;
里邊自定義一個(gè)內(nèi)部類ViewHolder,里邊的構(gòu)造參數(shù)傳入view參數(shù),這個(gè)參數(shù)就是RecyclerView的最外層布局,這樣就可以通過findViewById()來貨渠道布局中的ImageView和TextView的實(shí)例了;
BookAdapter中的構(gòu)造函數(shù),這個(gè)方法吧要展示的數(shù)據(jù)源傳進(jìn)來,并賦值給一個(gè)全局變量mBookAdapter,我們后繼的所有操作都將在這個(gè)數(shù)據(jù)源的基礎(chǔ)上進(jìn)行;重寫三個(gè)方法;
package com.example.dell.practice_recyclerview; 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.List; /** * Created by dell on 2018/6/3. * Created by qiyueqing on 2018/6/3. */ public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder>{ private List<Book> mBookList; static class ViewHolder extends RecyclerView.ViewHolder{ ImageView bookImage; TextView bookName; public ViewHolder(View view){ super(view); bookImage=view.findViewById(R.id.id_book_image); bookName=view.findViewById(R.id.id_book_name); } } public BookAdapter(List<Book> bookList){ mBookList=bookList; } @Override public BookAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_item,parent,false); RecyclerView.ViewHolder holder=new ViewHolder(view); return (ViewHolder) holder; } @Override public void onBindViewHolder(BookAdapter.ViewHolder holder, int position) { Book book=mBookList.get(position); holder.bookImage.setImageResource(book.getImageId()); holder.bookName.setText(book.getName()); } @Override public int getItemCount() { return mBookList.size(); } }
6、修改MainActivity中的代碼:
package com.example.dell.practice_recyclerview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Book> bookList=new ArrayList<>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initBooks(); RecyclerView recyclerView= (RecyclerView) findViewById(R.id.id_recycler_view); LinearLayoutManager layoutManager=new LinearLayoutManager(this); //LinearLayoutManager中定制了可擴(kuò)展的布局排列接口,子類按照接口中的規(guī)范來實(shí)現(xiàn)就可以定制出不同排雷方式的布局了 //配置布局,默認(rèn)為vertical(垂直布局),下邊這句將布局改為水平布局 //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); BookAdapter adapter=new BookAdapter(bookList); recyclerView.setAdapter(adapter); } private void initBooks(){ for (int i=1;i<11;i++){ Book book=new Book("春起之苗"+i,R.drawable.icon_book); bookList.add(book); } } }
此時(shí)運(yùn)行即可看到縱向的展示樣例了。
實(shí)現(xiàn)橫向滾動(dòng):
修改book_item中的代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="100dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/id_book_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> <TextView android:id="@+id/id_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dp"/> </LinearLayout>
2、修改MainActivity中的代碼
package com.example.dell.practice_recyclerview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Book> bookList=new ArrayList<>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initBooks(); RecyclerView recyclerView= (RecyclerView) findViewById(R.id.id_recycler_view); LinearLayoutManager layoutManager=new LinearLayoutManager(this); //LinearLayoutManager中定制了可擴(kuò)展的布局排列接口,子類按照接口中的規(guī)范來實(shí)現(xiàn)就可以定制出不同排雷方式的布局了 //配置布局,默認(rèn)為vertical(垂直布局),下邊這句將布局改為水平布局 layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); BookAdapter adapter=new BookAdapter(bookList); recyclerView.setAdapter(adapter); } private void initBooks(){ for (int i=1;i<11;i++){ Book book=new Book("春起之苗"+i,R.drawable.icon_book); bookList.add(book); } } }
例子下載地址:RecyclerView實(shí)現(xiàn)縱向和橫向滾動(dòng)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果
- Android RecyclerView 滾動(dòng)到中間位置的方法示例
- Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
- Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
- XRecyclerView實(shí)現(xiàn)下拉刷新、滾動(dòng)到底部加載更多等功能
- Android_RecyclerView實(shí)現(xiàn)上下滾動(dòng)廣告條實(shí)例(帶圖片)
- Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
- Android使用RecyclerView實(shí)現(xiàn)水平滾動(dòng)控件
- Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無限滾動(dòng)
- RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
相關(guān)文章
Android Flutter實(shí)現(xiàn)3D動(dòng)畫效果示例詳解
在Flutter中提供了AnimatedWidget組件用于構(gòu)建可復(fù)用的動(dòng)畫組件。本文我們用AnimatedWidget來實(shí)現(xiàn)組件的3D旋轉(zhuǎn)效果,感興趣的可以了解一下2022-03-03Android DaggerActivityComponent錯(cuò)誤解決辦法詳解
這篇文章主要介紹了Android DaggerActivityComponent錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2017-05-05關(guān)于AndroidStudio R文件莫名其妙缺失的快速解決方法
下面小編就為大家?guī)硪黄P(guān)于AndroidStudio R文件莫名其妙缺失的快速解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03詳解android webView獨(dú)立進(jìn)程通訊方式
本篇文章主要介紹了android webView獨(dú)立進(jìn)程通訊方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09淺析Flutter AbsorbPointer 與 IgnorePointer的區(qū)別
Flutter是Google一個(gè)新的用于構(gòu)建跨平臺(tái)的手機(jī)App的SDK。這篇文章主要介紹了Flutter AbsorbPointer 與 IgnorePointer的區(qū)別,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android SharedPreferences四種操作模式使用詳解
這篇文章主要介紹了Android SharedPreferences四種操作模式使用詳解的相關(guān)資料,這里介紹了獲取Android SharedPreferences的兩種方法及比較,和操作模式的介紹,需要的朋友可以參考下2017-07-07android開發(fā)教程之實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例
這篇文章主要介紹了android實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例,需要的朋友可以參考下2014-03-03