当前位置:   article > 正文

安卓RecyclerView的横向效果_android recyclerview 横向

android recyclerview 横向

1.导包:

 implementation 'androidx.recyclerview:recyclerview:1.2.0'

2.布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <androidx.recyclerview.widget.RecyclerView
  6. android:id="@+id/recycler_view"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" />
  9. </RelativeLayout>

 3.主界面代码:

  1. package com.example.recyclerviewapplication
  2. import android.os.Bundle
  3. import androidx.appcompat.app.AppCompatActivity
  4. import androidx.recyclerview.widget.GridLayoutManager
  5. import androidx.recyclerview.widget.RecyclerView
  6. import java.security.AccessController.getContext
  7. class MainActivity : AppCompatActivity() {
  8. override fun onCreate(savedInstanceState: Bundle?) {
  9. super.onCreate(savedInstanceState)
  10. setContentView(R.layout.activity_main)
  11. val data: MutableList<String> = ArrayList()
  12. for (i in 0..200){
  13. data.add("Item $i")
  14. }
  15. val adapter = MyAdapter(
  16. this,
  17. data
  18. )
  19. val recyclerView: RecyclerView =
  20. findViewById(R.id.recycler_view)
  21. recyclerView.layoutManager = GridLayoutManager(
  22. this,
  23. 1,
  24. GridLayoutManager.HORIZONTAL,
  25. false
  26. )
  27. recyclerView.adapter = adapter
  28. }
  29. }

4.MyAdpate:

  1. package com.example.recyclerviewapplication;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.List;
  10. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  11. private List<String> mData;
  12. public MyAdapter(Context context, List<String> data) {
  13. mData = data;
  14. }
  15. @NonNull
  16. @Override
  17. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  18. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
  19. return new ViewHolder(view);
  20. }
  21. @Override
  22. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  23. holder.mText.setText(mData.get(position));
  24. }
  25. @Override
  26. public int getItemCount() {
  27. return mData.size();
  28. }
  29. static class ViewHolder extends RecyclerView.ViewHolder {
  30. TextView mText;
  31. ViewHolder(@NonNull View itemView) {
  32. super(itemView);
  33. mText = itemView.findViewById(R.id.textView);
  34. }
  35. }
  36. }

5.适配器布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:padding="25dp"
  6. android:clipChildren="false">
  7. <TextView
  8. android:id="@+id/textView"
  9. android:layout_width="300px"
  10. android:padding="10dp"
  11. android:textSize="20dp"
  12. android:gravity="center"
  13. android:textColor="#fff"
  14. android:layout_height="wrap_content"/>
  15. <ImageView
  16. android:id="@+id/imageView"
  17. android:layout_width="300px"
  18. android:padding="5dp"
  19. android:background="#f00"
  20. android:layout_marginTop="60dp"
  21. android:layout_height="300px" />
  22. </RelativeLayout>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/174729
推荐阅读
相关标签
  

闽ICP备14008679号