【问题标题】:recycler view is not displaying all my content fetched from api回收站视图未显示我从 api 获取的所有内容
【发布时间】:2021-10-17 07:23:38
【问题描述】:

以下是我的活动布局文件

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        android:background="#B71C1C"/>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
    <RelativeLayout
        android:id="@+id/progresslayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff">
        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:indeterminateTint="@color/colorPrimaryDark"
            tools:targetApi="lollipop" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

以下是我的回收站代码

 <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="20dp"
    app:cardCornerRadius="8dp"

    app:cardElevation="6dp">

<LinearLayout
    android:id="@+id/llcontent"
    android:layout_width="match_parent"
    android:background="#E91E63"
    android:weightSum="6"
    android:layout_height="wrap_content">


    <RelativeLayout
        android:layout_weight="3.3"
        android:layout_width="290dp"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name of the file"
            android:padding="8dp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginLeft="80dp"

            android:textColor="#ffffff"/>
        <TextView
            android:id="@+id/txtQuote"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtTitle"
            android:text="name_of_the_author"

            android:padding="8dp"

            android:textColor="#ffffff"
            android:textSize="19sp"/>
        <TextView
            android:id="@+id/txtQuoteid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rs_299"
            android:padding="8dp"


            android:layout_toRightOf="@id/txtTitle"
            android:textSize="15sp"
            android:textStyle="bold"

            android:textColor="#ffffff"/>

    </RelativeLayout>




</LinearLayout>
    </androidx.cardview.widget.CardView>

问题是因为对于 textquote 文本视图,我正在获取并尝试显示大文本内容,某些内容未显示。如何解决此问题?。回收器的大小根据第一个内容自动修复的第一个回收站视图请帮助我

descactivitycode

class descactivity : AppCompatActivity() {
lateinit var recyclerview: RecyclerView
lateinit var adapter: Horizontal_RecyclerView
lateinit var layoutmanager: RecyclerView.LayoutManager
lateinit var progressBar: ProgressBar
lateinit var progressLayout: RelativeLayout
lateinit var button: Button
lateinit var toolbar: Toolbar
val bookInfoList = arrayListOf<Book>()
val ratingComparator = Comparator<Book> { book1, book2 ->
    book1.title.compareTo(book2.title, true)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_descactivity)


    recyclerview = findViewById(R.id.recyclerView)

    progressBar = findViewById(R.id.progressbar)
    layoutmanager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)


    progressLayout = findViewById(R.id.progresslayout)
    progressLayout.visibility = View.VISIBLE
    toolbar = findViewById(R.id.toolbar)

    setSupportActionBar(toolbar)
    val intent = intent
    val selectionOption = intent.getStringExtra("option")
    val selection = intent.getStringExtra("option1")
    if (selectionOption != null) {
        val queue = Volley.newRequestQueue(this)
        val url = "https://quotesapp-api.herokuapp.com/getquotes"
        if (ConnectionManager().checkConnectivity(this as Context)) {
            val JSONObjectRequest =
                object : JsonObjectRequest(Request.Method.GET, url, null, Response.Listener {
                    println("response is $it")

                    val success = it.getBoolean("success")
                    try {
                        if (success) {
                            progressLayout.visibility = View.GONE
                            val data = it.getJSONArray("data")
                            for (i in 0 until data.length()) {
                                val bookJsonObject = data.getJSONObject(i)
                                val BookObject = Book(
                                    bookJsonObject.getString("_id"),
                                    bookJsonObject.getString("title"),
                                    bookJsonObject.getString("quoteid"),
                                    bookJsonObject.getString("quote")
                                    //bookJsonObject.getInt("__v")


                                )
                                bookInfoList.add(BookObject)
                                adapter =
                                    Horizontal_RecyclerView(this as Context, bookInfoList)
                                recyclerview.adapter = adapter
                                recyclerview.layoutManager = layoutmanager
                            }
                        }
                    } catch (e: JSONException) {
                        Toast.makeText(this, "some unknown error occurred", Toast.LENGTH_SHORT)
                            .show()
                    }


                }, Response.ErrorListener {
                    Toast.makeText(this, "volley error occured", Toast.LENGTH_SHORT).show()
                    println("error is $it")


                }) {

                    override fun getHeaders(): MutableMap<String, String> {
                        val headers = HashMap<String, String>()

                        return headers
                    }

                }
            queue.add(JSONObjectRequest)

        }
    }

适配器

class Horizontal_RecyclerView(val context: Context, val itemList:ArrayList<Book>):RecyclerView.Adapter<Horizontal_RecyclerView.MyViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val view=LayoutInflater.from(parent.context).inflate(R.layout.row,parent,false)
    return MyViewHolder(view)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val book=itemList[position]
    holder.txtTitle.text=book.title
    holder.txtQuote.text=book.quote
    holder.txtQuoteid.text=book.quoteid



}

override fun getItemCount(): Int {
    return itemList.size
}
class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
    val txtTitle: TextView =itemView.findViewById(R.id.txtTitle)
    val txtQuote: TextView =itemView.findViewById(R.id.txtQuote)

    val txtQuoteid: TextView =itemView.findViewById(R.id.txtQuoteid)

   // val llcontent: LinearLayout =itemView.findViewById(R.id.llcontent )
}

}

【问题讨论】:

  • 你能把你的java/kotlin代码贴在这里吗?
  • @FlyingDutchman 我已添加代码
  • 添加您的 Horizo​​ntal_RecyclerView 适配器代码。
  • @AmarIlindra 我已添加。我只觉得布局文件有问题

标签: android xml android-layout android-recyclerview


【解决方案1】:

您好,关于回收站行 XML 文件,

<RelativeLayout
    android:layout_weight="3.3"
    android:layout_width="290dp"
    android:layout_height="wrap_content"

尝试将 layout_width 从 290dp 更改为 wrap_content/ match_parent,然后将宽度限制放在主 xml 文件上。 在 txtQuote TextView 标记 上的 加号 代替 padding 切换到边距。

如果失败请联系

【讨论】:

    【解决方案2】:

    Recyclerview 似乎不太清楚它的父级宽度和高度。当我使用“match_parent”和“wrap_content”时,我有一个错误加载的活动/片段。

    我最终做的不是最聪明的解决方案,但对我有用。我正在计算每个屏幕的倾角,然后动态计算屏幕宽度,然后将我的回收站视图设置为刚刚为我工作的数量(width - 45*(scale))。 45 是我在不同设备上测试过的一个数字,并认为足够好。再一次,这不是最明智的,它是唯一对我有用的东西。

    int pixel = 120;
    final float scale = getResources().getDisplayMetrics().density;
    int dip = (int) (pixel * scale + 0.5f);
    
    WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    int width = (windowManager.getDefaultDisplay().getWidth());
    int width2 = (width - 45*(Math.round(scale)));
    view.setLayoutParams(new RecyclerView.LayoutParams(width2, RecyclerView.LayoutParams.WRAP_CONTENT));
    

    【讨论】:

    • 你能告诉我如何在 kotlin 中使用这段代码
    • @kaarthikDeivasikamani Sultan 我所做的背后的逻辑实际上非常简单。我不知道 Kotlin,但我已经解释了该怎么做。稍微搜索一下就可以找到。我还记得,当我搜索我的问题时,我也看到了 Kotlin 解决方案
    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 2017-10-08
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多