日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android使用ConstraintLayout 加载RecyclerView数据显示不全

發布時間:2025/3/15 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android使用ConstraintLayout 加载RecyclerView数据显示不全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天學習KT的時候,發現的一個問題,現在做個筆記

在由于才學習kt,所以很多東西沒辦法想JAVA那樣順手,所以正在一步一步的填坑中。。。

先看問題:
我在List中添加了幾條個人數據,然后再添加了20條數據,這樣列表就能夠滑動,
但是呢,結果發現RecyclerView 只顯示了for循環的15條,第16條-20條都不見了


Activity的代碼:

class MainActivity : AppCompatActivity() {var testList = ArrayList<String>()lateinit var testAdapter: KtReAdapteroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)initImage()initRecycView()}private fun initRecycView() {testList.add("側滑菜單功能界面")testList.add("TAB菜單功能界面")testList.add("ScrollingActivity功能界面")testList.add("FullscreenActivity功能界面")testList.add("SettingsActivity功能界面")for (i in 1..20) {testList.add("測試---"+i)}val mylayoutManager = LinearLayoutManager(this)re_view.layoutManager = mylayoutManagertestAdapter = KtReAdapter(this, this.testList)re_view.adapter = testAdapter} }

看到這個結果 我就納悶了,為啥后面的數據沒顯示呢, 之后我就在
Adapter里面打印日志,結果發現數據是加載出來了,但是,但是并沒有顯示

Log日志

然后就到了找問題的時候了,現在數據沒有問題 那么就是布局的問題了,我看了看布局,然后百度了一下 搜索了ConstraintLayout的屬性說明 以及其中的含義 發現里一些有用的信息

<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/re_view"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@+id/img" />

我的RecyclerView的高度有問題 ,按照ConstraintLayout的屬性說明,如果我的RecyclerView要完全占滿下面的所有位置,可以把 高度設置為0,但是Y軸上面就要設置屬性,所以我又加了一句

app:layout_constraintBottom_toBottomOf="parent"

OK,問題解決,重新設置一下RecyclerView的屬性即可

<androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/re_view"android:layout_width="match_parent"//********** 修改的部分 *******android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"//********** 修改的部分 *******app:layout_constraintTop_toBottomOf="@+id/img" />

成功后的截圖:

總結

以上是生活随笔為你收集整理的Android使用ConstraintLayout 加载RecyclerView数据显示不全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。