无尽的滚动kotlin回收视图/ ListView

时间:2018-01-22 15:17:30

标签: android android-recyclerview kotlin infinite-scroll

我正在拼命尝试使用kotlin在Android应用上实现无休止的滚动。所有教程都没用,因为他们没有正确解释事情。例如: https://github.com/chetdeva/recyclerview-bindings

看起来很有希望,但作者使用的短语 “把它放在你的BindingAdapter中”所以我看看这个BindingAdapter是什么,我发现了一个java文件,但是如果你在那里插入任何东西我就会收到错误。就像我尝试的任何东西都直接失败。

其他教程是用java编写的,即使使用“translate to kotlin”选项也没用,因为翻译后的代码会抛出100个错误。

我尝试过这样的事情:

setContentView(R.layout.activity_main)
    list.layoutManager = LinearLayoutManager(this)
    list.hasFixedSize()
    list.adapter = ListAdapter(this, getLists())
    val list_view: RecyclerView = findViewById(R.id.list)
    fun setRecyclerViewScrollListener() {
        list_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                val height = list_view.getHeight()

                val diff = height-dy
                if (diff < 1000){
                    /*load next list */
                }
            }
        })
    }
    setRecyclerViewScrollListener()
}

或者

val inflater = LayoutInflater.from(this@MainActivity)
val layout = inflater.inflate(R.layout.append_list, null, false)
button.setOnClickListener{screen.addView(layout)}

是否有防弹方法可以简单地添加与html和js相同的元素?我在2分钟内写了这个片段。在Android / Kotlin中有类似的“简单”方式吗?

$("#next").click(function(){
  $(".append_text").append("new text <img src='http://static.webshopapp.com/shops/015426/files/005031634/560x625x2/kek-amsterdam-wandtattoo-hase-forest-friends-braun.jpg'/>")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next">Load</button>

<span class="append_text"> </span>

一般来说,我在选择错误的布局时会遇到很多错误。我尝试了Listview和contrainlayout并回收布局和垂直滚动布局等。是否有一个简单的body标签,你只需附加一个xml文件?

我认为我一直走错路,因为我通过网络的眼睛看到了一切。开发。虽然android没有经典的DOM。任何人都可以通过一个关于如何在按钮点击/滚动时将xml文件附加到主xml文件的最小例子向我解释一下吗?

3 个答案:

答案 0 :(得分:6)

我使用此方法为Kotlin中的recyclerview添加无限滚动功能:

private fun setRecyclerViewScrollListener() {
    scrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            val totalItemCount = recyclerView!!.layoutManager.itemCount
            if (totalItemCount == lastVisibleItemPosition + 1) {
                Log.d("MyTAG", "Load new list")
                recycler.removeOnScrollListener(scrollListener)
            }
        }
    }
    recycler.addOnScrollListener(scrollListener)
}

变量lastVisibleItemPosition声明如下:

private val lastVisibleItemPosition: Int get() = linearLayoutManager.findLastVisibleItemPosition()

private lateinit var scrollListener: RecyclerView.OnScrollListener

每次您需要将此功能添加到recyclerView时,只需调用setRecyclerViewScrollListener()方法。

希望它有所帮助,

莱昂纳多

答案 1 :(得分:1)

在滚动侦听器的回收器视图中设置

//THIS is what I want to get in the end...
[
  {
    "league": "League Cup",
    "tournaments": [
      {
        "id": 3402,
        "league": {
          "id": 4141,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3403,
        "league": {
          "id": 4141,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      }
    ]
  },
  {
    "league": "League Cup 2",
    "tournaments": [
      {
        "id": 3401,
        "league": {
          "id": 4145,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup 2",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3405,
        "league": {
          "id": 4145,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup 2",
          "slug": "league-slug",
          "url": null
        }
      }
    ]
  }
]

答案 2 :(得分:1)

嗯,如果这解决了您的问题,我现在不知道,但我使用它来实现一个回收器视图,当滚动结束时将新数据添加到我的回收器视图中:

productsListActivityBinding.recyclerViewProducts.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            if (!recyclerView.canScrollVertically(1)){
                //function that add new elements to my recycler view
            }
        }

    })