当前位置:   article > 正文

Android RecycleView实现横向,纵向都可滑动的列表_横向recycalview

横向recycalview

问题

1.RecycleView默认是纵向滑动的,可以通过setOrientation(LinearLayoutManager.HORIZONTAL)设置为横向滑动。

       //指定列表布局方式,默认是纵向垂直
       recycleView.setLayoutManager(new LinearLayoutManager(this));
       //指定列表线性布局,横向水平
        LinearLayoutManager lm = new LinearLayoutManager(this);
        lm.setOrientation(LinearLayoutManager.HORIZONTAL);
        hView.setLayoutManager(lm);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 哪怎么实现实现横向,纵向都可滑动的列表呢?
    答案:HorizontalScrollView+RecyclerView嵌套。效果如下:

在这里插入图片描述

代码

MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        rv.layoutManager = LinearLayoutManager(this)
        val numbers = arrayListOf<String>("one", "two", "three", "four", "one", "two", "three", "four", "one", "two", "three", "four", "one", "two", "three", "four", "one", "two", "three", "four")
        var adapter = Adapter(this, numbers)
        rv.adapter = adapter
    }


}

class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private var context: Context? = null
    private var list: ArrayList<String>? = null

    constructor(context: Context, list: ArrayList<String>) {
        this.context = context
        this.list = list
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        val view: View = LayoutInflater.from(context).inflate(R.layout.list, p0, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list!!.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, pos: Int) {

    }


    class ViewHolder : RecyclerView.ViewHolder {
        constructor(view: View) : super(view)
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
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:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">

	<HorizontalScrollView
		android:layout_width="match_parent"
		android:scrollbars="none"
		android:layout_height="wrap_content">

		<android.support.v7.widget.RecyclerView
			android:id="@+id/rv"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</HorizontalScrollView>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/174658
推荐阅读
相关标签
  

闽ICP备14008679号