【发布时间】:2018-07-01 06:35:13
【问题描述】:
我正在拼命尝试使用 kotlin 在 android 应用程序上实现无限滚动。所有的教程要么没用,因为它们没有正确解释。例如: https://github.com/chetdeva/recyclerview-bindings
看起来很有希望,但作者使用了类似的短语
“把它放在你的 BindingAdapter 中”,所以我看看这个 BindingAdapter 是什么,我找到了一个 java 文件,但是如果你在其中插入任何东西,我就会出错。就像我尝试的任何事情都直接失败了。
其他教程是用 java 编写的,即使使用“翻译为 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 分钟内写了这个 sn-p。 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 以及回收 Layout 和 Vertical Scrolling 布局等。是否有一个简单的正文标签,您可以在其中简单地附加一个 xml 文件?
我认为我一直走错路,因为我通过网络的眼睛看到了一切。开发。而android没有经典的DOM。任何人都可以用一个关于如何在按钮单击/滚动时将 xml 文件附加到主 xml 文件的最小示例向我解释一下吗?
【问题讨论】:
标签: android android-recyclerview kotlin infinite-scroll