您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用RecyclerView怎么實現一個縱向和橫向滾動功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
縱向滾動
1、添加依賴庫:
打開app/build.gradle文件,在dependencies閉包中添加如下內容(compile 'com.android.support:recyclerview-v7:24.2.1'為添加的內容)
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' }
添加完之后點擊一下Sync Now來進行同步;
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>
因為RecyclerView不是內置在系統SDK中的,所以需要把完整的包路徑寫出來。
3、新建實體類,這里以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、為ListView的子項制定一個我們自定義的布局:
在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準備一個適配器:
新建BookAdapter類,讓這個類繼承RecyclerView.Adapter,并將泛型指定為BookAdapter.ViewHolder;
里邊自定義一個內部類ViewHolder,里邊的構造參數傳入view參數,這個參數就是RecyclerView的最外層布局,這樣就可以通過findViewById()來貨渠道布局中的ImageView和TextView的實例了;
BookAdapter中的構造函數,這個方法吧要展示的數據源傳進來,并賦值給一個全局變量mBookAdapter,我們后繼的所有操作都將在這個數據源的基礎上進行;重寫三個方法;
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中定制了可擴展的布局排列接口,子類按照接口中的規范來實現就可以定制出不同排雷方式的布局了 //配置布局,默認為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); } } }
此時運行即可看到縱向的展示樣例了。
實現橫向滾動:
修改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中定制了可擴展的布局排列接口,子類按照接口中的規范來實現就可以定制出不同排雷方式的布局了 //配置布局,默認為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怎么實現一個縱向和橫向滾動功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。