简体   繁体   中英

Row item width changes on scrolling in recyclerview in Android

The RecyclerView calling inside Alert Dialog, I have tried setting width MATCH_PARENT at run time in onCreateView in adapter class.

if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
               binding.root.getLayoutParams ().width = parent.getWidth ()

Tried calling inside in onCreateView but leaving space vertically. I tried all possible combination which is present on SO but nothing is working.

private fun initDialog() {
    if (mAlertDialog == null) {
        val mBuilder = AlertDialog.Builder(this)
        val view1 = layoutInflater.inflate(R.layout.select_itinerary_dialog, null, false)
        val mRecyclerView = view1.findViewById<RecyclerView>(R.id.mRecyclerView)

        mRecyclerView?.adapter = SelectItineraryAdapter(this@AddItineraryActivity)
        val llm = LinearLayoutManager(this@AddItineraryActivity)
        //   llm.isAutoMeasureEnabled = false
        mRecyclerView?.layoutManager = llm
        //mRecyclerView?.layoutManager = LinearLayoutManager(this@AddItineraryActivity)
        mCheckBoxSelectAll = view1.findViewById<CheckBox>(R.id.checkSelctAll)
        mCheckBoxSelectAll?.setOnCheckedChangeListener { _, b ->

            if (!fromBroadcastReceiver) {
                for (item in mItinerarylistResponse?.data?.itinerary!!) {
                    item?.isSelected = b
                }
                mRecyclerView?.adapter!!.notifyDataSetChanged()
            }
            fromBroadcastReceiver = false

        }

        view1.findViewById<AppCompatButton>(R.id.clearItinerary).setOnClickListener {

            mAlertDialog!!.dismiss()
        }
        view1.findViewById<AppCompatButton>(R.id.doneItinerary).setOnClickListener {

            try {
                for (item in mItinerarylistResponse?.data?.itinerary!!) {

                    viewContainer.visibility = View.VISIBLE
                    if (item!!.item == "Wake Up") {

                        if (item.isSelected) {
                            wakeUpTimeTV.visibility = View.VISIBLE
                            wakeUpTimeLay.visibility = View.VISIBLE
                        } else {
                            wakeUpTimeTV.visibility = View.GONE
                            wakeUpTimeLay.visibility = View.GONE }
                    }
                }
            } catch (e: Exception) {
            }
            mAlertDialog!!.dismiss()
        }

        mBuilder.setView(view1)
        mAlertDialog = mBuilder.create()
        mAlertDialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
        //  mAlertDialog?.setView(view1)
    }
}

Adapter

class SelectItineraryAdapter(val contecx: AddItineraryActivity) : RecyclerView.Adapter<SelectItineraryAdapter.MyHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {


        val binding = DataBindingUtil.inflate<RowItemItinararyBinding>(LayoutInflater.from(contecx), R.layout.row_item_itinarary, parent, false)
        // val v=  LayoutInflater.from(contecx).inflate(LayoutInflater.from(contecx), R.layout.row_item_itinarary, parent, false)

        /* if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
           binding.root.getLayoutParams ().width = parent.getWidth ()*/

        val dd = MyHolder(binding)
        return dd
    }

    override fun getItemCount(): Int {
        return mItinerarylistResponse?.data?.itinerary!!.size
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: MyHolder, position: Int) {

        holder.v.itinararyname.text = mItinerarylistResponse?.data?.itinerary!![position]?.item

        holder.v.markReadCheck.setOnCheckedChangeListener { _, isChecked ->
            mItinerarylistResponse?.data?.itinerary!![position]!!.isSelected = isChecked
            val receiverIntentDetail = Intent()
            receiverIntentDetail.action = isitinerarySelected
            holder.v.markReadCheck.context.sendBroadcast(receiverIntentDetail)
        }

        holder.v.detailLat.setOnTouchListener { view, motionEvent ->
            when (motionEvent.action) {
                MotionEvent.ACTION_DOWN -> {

                    com.socrpro.utils.error("ACTION_DOWN")
                }
                MotionEvent.ACTION_UP -> {
                    // if (itinerary[position].readStatus == 0) {
                    holder.v.markReadCheck.performClick()
                    //  }
                    com.socrpro.utils.error("ACTION_UP")
                }
                MotionEvent.ACTION_MOVE -> {
                    com.socrpro.utils.error("ACTION_MOVE")
                }
            }

            true
        }
        holder.v.markReadCheck.isChecked = mItinerarylistResponse?.data?.itinerary!![position]!!.isSelected
    }

    class MyHolder(val v: RowItemItinararyBinding) : RecyclerView.ViewHolder(v.root)
}

Row_item

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout

        android:id="@+id/detailLat" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:clickable="true" android:orientation="vertical">

        <LinearLayout android:id="@+id/readornot" android:layout_width="match_parent"
            android:layout_height="@dimen/_25sdp" android:background="@color/white"
            android:gravity="center_vertical" android:orientation="horizontal"
            android:paddingTop="@dimen/_5sdp" android:paddingBottom="@dimen/_5sdp">

            <CheckBox android:id="@+id/markReadCheck" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:button="@drawable/custom_checkbox" android:layoutDirection="rtl"
                android:text="" />
            <TextView android:id="@+id/itinararyname" android:layout_width="0dp"
                android:layout_height="wrap_content" android:layout_gravity="center_vertical"
                android:layout_marginStart="@dimen/_2sdp" android:layout_marginEnd="@dimen/_5sdp"
                android:layout_weight="1" android:singleLine="true"
                android:text="dfgdgfdggfdg" android:textColor="@color/colorPrimary"
                android:textSize="@dimen/_10sdp" />

        </LinearLayout>

        <View android:layout_width="match_parent" android:layout_height=".5dp"
            android:layout_alignBottom="@+id/readornot" android:background="@color/linecolorcc" />

    </RelativeLayout>
</layout>

在此处输入图像描述

Edited.. Adding a new possible approach to fix the issue..

TL;DR

Replace your vertical LinearLayout (which surrounds the RecyclerView) with a RelativeLayout

Approach 1 (old answer)

Before surrounding the layout with ScrollView

在此处输入图像描述

After adding ScrollView as root

在此处输入图像描述

Sample Code

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none">

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

        <EditText
                android:id="@+id/search"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/search"
                android:orientation="vertical">

                <!--Header-->
                <LinearLayout
                    android:id="@+id/static_header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    style="@style/HeaderStyle"/>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

    </RelativeLayout>
 </ScrollView>
</layout>

The downside of adding ScrollView is that it causes all the surrounded Views to be Scrolled! So try the next approach

Approach 2 (better one)

Actually, i was not satisfied with the previous answer because the header of the table should not disappear when user scrolls down the RecyclerView! So i've just replaced my vertical LinearLayout (which surrounds the RecyclerView) with a RelativeLayout

Sample Code

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">

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

            <EditText
                    android:id="@+id/search"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"/>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/search">

                    <!--Header-->
                    <LinearLayout
                        android:id="@+id/static_header"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignStart="@id/rv"
                        android:layout_alignEnd="@id/rv"
                        style="@style/HeaderStyle"/>

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rv"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/static_header"/>
                </RelativeLayout>
        </RelativeLayout>
</layout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM