更加強(qiáng)大的滾動控件 RecyclerView #P123
基本用法
- 需要在build.gradle添加依賴庫 recyclerview
- activity_main.xml文件添加RecyclerView
- 圖片,Furit類,fruit_item.xml
- 新建FruitAdapter類,作為適配器,繼承自RecyclerView.Adapter ,并且泛型指定為FruitAdapter.ViewHolder ViewHolder是內(nèi)部類
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" />
新建FruitAdapter
- 定義內(nèi)部類ViewHoler ,繼承自RecyclerView.ViewHolder
- ViewHoler的構(gòu)造函數(shù)傳入一個View參數(shù),這個參數(shù)通常就是RecyclerView子項的最外層布局,通過findViewById() 方法來獲取布局中的Imageiew和TextView的實例
- FruitAdapter 中也有構(gòu)造函數(shù),這個方法用于把要展示的數(shù)據(jù)源傳進(jìn)來,并賦值給一個全部變量mFruitList
- FruitAdapter繼承自RecyclerView.Adapter,所以要重寫
onCreatViewHolder(),onBingViewHolder(),getItemCount()
-
onCreatViewHolder()
用來創(chuàng)建ViewHolder實例,將fruit_item布局加載進(jìn)來,然后創(chuàng)建一個ViewHoler實例,并把加載出來的布局傳入構(gòu)造函數(shù)中,最后將ViewHoler的實例返回 -
onBingViewHolder()
用于對RecyclerView子項的數(shù)據(jù)進(jìn)行賦值,會在每個子項被滾動到屏幕內(nèi)的時候執(zhí)行,通過position 參數(shù)得到當(dāng)前項的Fruit實例,然后將數(shù)據(jù)設(shè)置到ViewHolder的ImageView和TextView當(dāng)中即可 - getItemCount() 告訴RecyclerView有多少子項 ,返回 mFruitList.size()
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> { private List<Fruit> mFruitList; /** * Created by Mason on 2017/11/16. */ static class ViewHolder extends RecyclerView.ViewHolder { ImageView fruitImage; TextView fruitName; public ViewHolder(View view) { super(view); fruitImage = (ImageView) view.findViewById(R.id.fruit_image); fruitName = (TextView) view.findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> fruitList) { mFruitList = fruitList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Fruit fruit = mFruitList.get(position); holder.fruitImage.setImageResource(fruit.getImageId()); holder.fruitName.setText(fruit.getName()); } @Override public int getItemCount() { return mFruitList.size(); } }
-
改變?yōu)闄M向RecyclerView
- fruit_item.xml 的LinearLayout布局改為
orientation:"vertical"
垂直布局 ,默認(rèn)horizontal
水平布局 - 修改TextView和ImageView的layout_gravity 修改成水平居中center_horizontal
- MainActivity.java文件 調(diào)用LinearLayoutManager的setOrientation()方法設(shè)置布局排列方向 添加
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL)
- xml
<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/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" /> </LinearLayout>
- MainActivity.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intiFruits(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); FruitAdapter adapter = new FruitAdapter(fruitList); recyclerView.setAdapter(adapter); }
RecyclerView 點擊事件
- 需要自己給子項具體的View注冊點擊事件
- 雖然比ListView繁瑣,但是ListView給單獨按鈕注冊事件更加繁瑣
- 修改ViewHolder,添加fruitView變量來保存子項最外層布局的實例
- 然后在onCreatViewHolder()方法中注冊點擊事件就行了.
- 這里項目分別為最外層布局和Image注冊了點擊事件.
- RecyclerView強(qiáng)大在于,可以輕松實現(xiàn)子項中任意控件或布局的點擊事件
- 我們先獲取用戶點擊的position
- 通過position拿到相應(yīng)的Fruit實例
- 再使用Toast彈出兩種不同的內(nèi)容
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> { private List<Fruit> mFruitList; /** * Created by Mason on 2017/11/16. */ static class ViewHolder extends RecyclerView.ViewHolder { View fruitView; // 添加 ImageView fruitImage; TextView fruitName; public ViewHolder(View view) { super(view); fruitView = view; //添加 fruitImage = (ImageView) view.findViewById(R.id.fruit_image); fruitName = (TextView) view.findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> fruitList) { mFruitList = fruitList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false); final ViewHolder holder = new ViewHolder(view); // 添加 holder.fruitView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "you click view " + fruit.getName(), Toast.LENGTH_SHORT).show(); } }); holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Fruit fruit = mFruitList.get(position); Toast.makeText(v.getContext(), "you click image " + fruit.getName(), Toast.LENGTH_SHORT).show(); } }); return holder; } ... }
這是我學(xué)習(xí)Android的筆記,參考書<<第一行代碼-第二版>>,感謝郭霖大神
如有不足之處,可以一起討論
附上郭霖的blog http://blog.csdn.net/guolin_blog