RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
本文實(shí)例為大家分享了RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
布局文件
<LinearLayout 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" tools:context=".RecyclerViewActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="8dp"/> </LinearLayout>
Item
android:layout_width="100dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="5dp"> <ImageView android:id="@+id/iv_recyclerview_imag" android:layout_width="wrap_content" android:layout_height="100dp" /> <TextView android:id="@+id/tv_recyclerview_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老虎" android:textSize="17sp" android:layout_gravity="center" android:textStyle="bold" android:padding="3dp"/> </LinearLayout>
適配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { private List<Animal> animalList; private int resource; public RecyclerViewAdapter(List<Animal> animalList, int resource) { this.animalList = animalList; this.resource = resource; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(resource,parent, false); ViewHolder holder = new ViewHolder(itemView); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Animal animal = animalList.get(position); holder.animalImag.setImageResource(animal.getImageId()); holder.animalName.setText(animal.getName()); } @Override public int getItemCount() { return animalList.size(); } static class ViewHolder extends RecyclerView.ViewHolder{ ImageView animalImag; TextView animalName; public ViewHolder(View itemView){ super(itemView); animalImag = itemView.findViewById(R.id.iv_recyclerview_imag); animalName = itemView.findViewById(R.id.tv_recyclerview_name); } } }
核心代碼
public class RecyclerViewActivity extends AppCompatActivity { private List<Animal> animalList = new ArrayList<>(); private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); recyclerView = findViewById(R.id.recyclerView_view); initAnimals(); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(linearLayoutManager); RecyclerViewAdapter adapter = new RecyclerViewAdapter(animalList,R.layout.recyclerview_item); recyclerView.setAdapter(adapter); } //初始化動(dòng)物數(shù)據(jù) private void initAnimals() { Animal daxaing = new Animal("大象", R.drawable.animal_one); animalList.add(daxaing); Animal shizi = new Animal( "袋鼠", R.drawable.animal_two); animalList.add(shizi); Animal daishu = new Animal("二哈", R.drawable.animal_three); animalList.add(daishu); Animal laohu = new Animal("獅子", R.drawable.animal_four); animalList.add(laohu); Animal zhu = new Animal("豬", R.drawable.animal_five); animalList.add(zhu); Animal songshu = new Animal("猴子", R.drawable.animal_six); animalList.add(songshu); Animal baozi = new Animal("豹子", R.drawable.animal_seven); animalList.add(baozi); Animal shayu = new Animal("鯊魚(yú)", R.drawable.animal_eight); animalList.add(shayu); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 中RecyclerView通用適配器的實(shí)現(xiàn)
- 淺談Android中適配器的notifyDataSetChanged()為何有時(shí)不刷新
- Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器三)
- Android ListView和Adapter數(shù)據(jù)適配器的簡(jiǎn)單介紹
- 詳解xamarin Android 實(shí)現(xiàn)ListView萬(wàn)能適配器
- Kotlin編寫(xiě)Android適配器Adapter
- Android SimpleAdapter適配器使用詳解
- Android 通過(guò)ViewHolder優(yōu)化適配器的實(shí)現(xiàn)方法(必看)
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- Android RecyclerView網(wǎng)格布局示例解析
- Android實(shí)現(xiàn)的RecyclerView適配器
相關(guān)文章
詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09Android應(yīng)用退出登錄的實(shí)現(xiàn)方法
每一個(gè)app都會(huì)有一個(gè)”退出登陸”的功能,當(dāng)點(diǎn)擊退出之后需要將所有的Activity都finish掉,開(kāi)始是想將棧中的所有Activity清除掉,但是沒(méi)有找到方法,后來(lái)用廣播實(shí)現(xiàn)了。下面小編給大家分享android應(yīng)用退出登錄的實(shí)現(xiàn)方法,需要的朋友參考下2017-04-04Flutter組件開(kāi)發(fā)過(guò)程完整講解
這篇文章主要介紹了Flutter組件開(kāi)發(fā)過(guò)程,F(xiàn)lutter是Google開(kāi)源的構(gòu)建用戶界面(UI)工具包,幫助開(kāi)發(fā)者通過(guò)一套代碼庫(kù)高效構(gòu)建多平臺(tái)精美應(yīng)用,支持移動(dòng)、Web、桌面和嵌入式平臺(tái)。Flutter 開(kāi)源、免費(fèi),擁有寬松的開(kāi)源協(xié)議,適合商業(yè)項(xiàng)目2022-11-11Android編程之listView中checkbox用法實(shí)例分析
這篇文章主要介紹了Android編程之listView中checkbox用法,結(jié)合實(shí)例形式分析了Android中checkbox的頁(yè)面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2016-01-01如何使用Mock修改Android設(shè)備上的features
這篇文章主要介紹了如何使用Mock修改Android設(shè)備上的features,想了解Mock的同學(xué)可以參考下2021-04-04Android實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04Android實(shí)現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09FragmentStatePagerAdapter保存恢復(fù)下拉刷新Fragment內(nèi)存數(shù)據(jù)
這篇文章主要為大家介紹了FragmentStatePagerAdapter保存恢復(fù)下拉刷新Fragment內(nèi)存數(shù)據(jù)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android生存指南之:開(kāi)發(fā)中的注意事項(xiàng)
本篇文章是對(duì)在Android開(kāi)發(fā)中的一些注意事項(xiàng),需要的朋友可以參考下2013-05-05