All RecyclerView items are not visible when using RecyclerView in ConstraintLayout

问题: I am using ConstraintLayout as a parent and put recyclerview in it to populate the list. Suppose I have to give item count 5 to populate the list and run the code then the...

问题:

I am using ConstraintLayout as a parent and put recyclerview in it to populate the list. Suppose I have to give item count 5 to populate the list and run the code then the list is showing perfectly but the last item of the recyclerview is showing half and not fully visible, and scroll stops at there. I found a solution for this is if am giving height match_parent to the recyclerview then it works fine but then all other view is hidden behind the recyclerview. If I use another parent view like LinearLayout or RelativeLayout, then recyclerview with height wrap_content works fine and all the list item are fully visible.

I have tried by 2 ways that work for me but I found that wrong programming practice

  1. I give height match_parent to recyclerview and give top_margin to the recylerview so all the other UI item show.

  2. I give padding_bottom to the recyclerview until all the list item position visible

My XML file "activity_per_day_sale.xml" is below.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">


    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintStart_toStartOf="parent"

            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical">
            <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/btn_back"
                    android:padding="5dp"
                    android:onClick="OnClickPerDaySale"
                    android:background="?attr/selectableItemBackground"
                    android:src="@drawable/icon_back_white"
            />

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Signup"
                      android:layout_centerInParent="true"
                      android:id="@+id/home_title_text"
                      android:textColor="@color/black"
                      android:visibility="gone"
                      android:fontFamily="@font/seguisb"
                      android:textSize="18sp"/>

            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="SKIP"

                      android:padding="10dp"
                      android:layout_centerVertical="true"
                      android:visibility="gone"
                      android:id="@+id/skip"
                      android:textColor="@color/black"
                      android:fontFamily="@font/seguisb"
                      android:textSize="18sp"/>

        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="25sp"
              android:layout_marginTop="10dp"
              android:text="Per Day Sale Details"
              android:textColor="@color/yellow_app_logo_color"
              android:fontFamily="@font/segoeuib"
              app:layout_constraintTop_toBottomOf="@+id/toolbar"
              app:layout_constraintStart_toStartOf="parent"
              android:id="@+id/TV_signupScreenText"
              android:layout_marginLeft="45dp"/>

        <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rv_perday_sale"
                android:layout_marginTop="10dp"
                android:layout_marginHorizontal="10dp"
                app:layout_constraintTop_toBottomOf="@+id/TV_signupScreenText"/>

</android.support.constraint.ConstraintLayout>

Activity class is "PerDaySaleActivity.kt"

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.nq.NQManager.R
import com.nq.NQManager.utils.BaseActivity
import kotlinx.android.synthetic.main.activity_per_day_sale.*

class PerDaySaleActivity:BaseActivity(){
    var adapterPerdaySale : AdapterPerdaySale? = null

    companion object {
        fun start(context: Context) {
            val starter = Intent(context, PerDaySaleActivity::class.java)
            context.startActivity(starter)
        }
    }
    override fun getID(): Int {
        return R.layout.activity_per_day_sale
    }

    override fun iniView(savedInstanceState: Bundle?) {
        initViews()
    }
    fun OnClickPerDaySale(v: View){
        when(v){
            btn_back->{
                finish()
            }
        }
    }
    fun initViews() {
        adapterPerdaySale   = AdapterPerdaySale( this)
        rv_perday_sale.layoutManager = StaggeredGridLayoutManager(1, 1)
        rv_perday_sale.adapter = adapterPerdaySale
    }

    private fun setUpRecyclerView() {
        runOnUiThread { adapterPerdaySale!!.notifyDataSetChanged() }

    }

My Adapter class "AdapterPerdaySale.kt"

mport android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.nq.NQManager.R
import kotlinx.android.synthetic.main.adapter_perday_sale.view.*

class AdapterPerdaySale(context: Context):
    RecyclerView.Adapter<AdapterPerdaySale.MyViewHOlder>()  {

        private var ctx:Context?=context


        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHOlder {
            return MyViewHOlder(LayoutInflater.from(ctx).inflate(R.layout.adapter_perday_sale, p0, false))
        }

        override fun getItemCount(): Int {
            return 5
        }

        override fun onBindViewHolder(holder: MyViewHOlder, position: Int) {
            if (position==1){
                holder.tv_date.text="26 June, 2019"
            }else if(position==2){
                holder.tv_date.text="27 June, 2019"

            }
        }

        inner class MyViewHOlder(view: View) : RecyclerView.ViewHolder(view) {
            val tv_date=view.tv_date
        }
    }

My Adapter xml file "adapter_perday_sale.xml"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingVertical="5dp"
android:background="@color/black">
    <TextView
            android:id="@+id/tv_date"
            android:layout_below="@+id/txt_today"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:fontFamily="@font/segoeuib"
            android:text="25 June, 2019"
            android:textSize="23sp"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginRight="8dp" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1"
            android:orientation="horizontal"
            app:layout_constraintTop_toBottomOf="@+id/tv_date">

        <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="25sp"
                      android:layout_marginTop="5dp"
                      android:text="Orders"
                      android:layout_centerHorizontal="true"
                      android:textColor="@color/white"
                      android:fontFamily="@font/segoeuib"/>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="50sp"
                      android:layout_marginTop="0dp"
                      android:text="50"
                      android:layout_centerHorizontal="true"
                      android:textColor="@color/order_history_txt_color"
                      android:fontFamily="@font/segoeui"/>
        </LinearLayout>
        <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="25sp"
                      android:layout_marginTop="5dp"
                      android:text="Revenue"
                      android:layout_centerHorizontal="true"
                      android:textColor="@color/white"
                      android:fontFamily="@font/segoeuib"/>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:textSize="50sp"
                      android:layout_marginTop="0dp"
                      android:text="£550"
                      android:layout_centerHorizontal="true"
                      android:textColor="@color/order_history_txt_color"
                      android:fontFamily="@font/segoeui"/>
        </LinearLayout>

    </LinearLayout>
    <View android:layout_width="match_parent"
          android:layout_height="1.5dp"
          android:background="@color/gray"
          android:id="@+id/view_1"
          app:layout_constraintTop_toBottomOf="@+id/linearLayout1"
          android:layout_marginLeft="8dp"
          android:layout_marginTop="10dp"
          android:layout_marginBottom="10dp"
          app:layout_constraintStart_toStartOf="parent"
          android:layout_marginStart="8dp"/>

</android.support.constraint.ConstraintLayout>

回答1:

Add this app:layout_constraintBottom_toBottomOf="parent" property to your recyclerview

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/btn_back"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="?attr/selectableItemBackground"
                android:onClick="OnClickPerDaySale"
                android:padding="5dp"
                android:src="@drawable/ic_background" />

            <TextView
                android:id="@+id/home_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Signup"
                android:textColor="#000"
                android:textSize="18sp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/skip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_centerVertical="true"
                android:padding="10dp"
                android:text="SKIP"
                android:textColor="#000"
                android:textSize="18sp"
                android:visibility="gone" />

        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>

    <TextView
        android:id="@+id/TV_signupScreenText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="10dp"
        android:text="Per Day Sale Details"
        android:textColor="#AD4E4E"
        android:textSize="25sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_perday_sale"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/TV_signupScreenText" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 发表于 2019-07-08 00:29
  • 阅读 ( 138 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除