RecyclerView實現(xiàn)橫向滾動效果
更新時間:2021年01月02日 09:11:05 作者:" 霎那凝眸,三生不忘゛
這篇文章主要為大家詳細介紹了RecyclerView實現(xiàn)橫向滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了RecyclerView實現(xiàn)橫向滾動效果的具體代碼,供大家參考,具體內(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);
}
//初始化動物數(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("鯊魚", R.drawable.animal_eight);
animalList.add(shayu);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android 中RecyclerView通用適配器的實現(xiàn)
- 淺談Android中適配器的notifyDataSetChanged()為何有時不刷新
- Android之自定義實現(xiàn)BaseAdapter(通用適配器三)
- Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹
- 詳解xamarin Android 實現(xiàn)ListView萬能適配器
- Kotlin編寫Android適配器Adapter
- Android SimpleAdapter適配器使用詳解
- Android 通過ViewHolder優(yōu)化適配器的實現(xiàn)方法(必看)
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- Android RecyclerView網(wǎng)格布局示例解析
- Android實現(xiàn)的RecyclerView適配器
相關文章
詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android編程之listView中checkbox用法實例分析
這篇文章主要介紹了Android編程之listView中checkbox用法,結合實例形式分析了Android中checkbox的頁面布局及功能實現(xiàn)相關技巧,需要的朋友可以參考下2016-01-01
Android實現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
FragmentStatePagerAdapter保存恢復下拉刷新Fragment內(nèi)存數(shù)據(jù)
這篇文章主要為大家介紹了FragmentStatePagerAdapter保存恢復下拉刷新Fragment內(nèi)存數(shù)據(jù)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

