Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中在recyclerview中滚动时的行项目宽度更改_Java_Android_Android Recyclerview_Android Alertdialog - Fatal编程技术网

Java Android中在recyclerview中滚动时的行项目宽度更改

Java Android中在recyclerview中滚动时的行项目宽度更改,java,android,android-recyclerview,android-alertdialog,Java,Android,Android Recyclerview,Android Alertdialog,在RecyclerView调用内部警报对话框时,我尝试在适配器类的onCreateView中的运行时设置宽度MATCH\u PARENT if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT) binding.root.getLayoutParams ().width = parent.getWidth () 尝试在onCreateView中调用内部,

在RecyclerView调用内部警报对话框时,我尝试在适配器类的
onCreateView
中的运行时设置宽度
MATCH\u PARENT

if (binding.root.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
               binding.root.getLayoutParams ().width = parent.getWidth ()
尝试在
onCreateView
中调用内部,但垂直留出空间。我尝试了所有可能的组合,但没有任何效果

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)
    }
}
private fun initDialog(){
if(mAlertDialog==null){
val mBuilder=AlertDialog.Builder(此)
val view1=LAYOUTINGFLATER.充气(R.layout.select_TINERARY_对话框,null,false)
val mRecyclerView=view1.findViewById(R.id.mRecyclerView)
mRecyclerView?.adapter=选择行程适配器(this@AddItineraryActivity)
val llm=线性布局管理器(this@AddItineraryActivity)
//llm.isAutoMeasureEnabled=错误
mRecyclerView?.layoutManager=llm
//mRecyclerView?.layoutManager=LinearLayoutManager(this@AddItineraryActivity)
mCheckBoxSelectAll=view1.findViewById(R.id.checkSelctAll)
mCheckBoxSelectAll?.setOnCheckedChangeListener{{,b->
if(!fromBroadcastReceiver){
对于(mItinerarylistResponse?数据?行程中的项目!!){
项目?.isSelected=b
}
mrecycerview?.adapter!!.notifyDataSetChanged()
}
fromBroadcastReceiver=false
}

视图1.findViewById已编辑..添加一种新的可能方法来解决问题..

TL;DR

将垂直线性布局(环绕回收视图)替换为相对线布局

方法1(旧答案)

在使用ScrollView包围布局之前

将ScrollView添加为root之后

示例代码

<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>
<?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>


添加ScrollView的缺点是它会导致所有包围的视图都被滚动!所以请尝试下一种方法

方法2(更好的方法)

事实上,我对前面的答案不满意,因为当用户向下滚动RecyclerView时,表的标题不应该消失!所以我刚刚用RelativeLayout替换了我的垂直线性布局(环绕RecyclerView)

示例代码

<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>
<?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>


您能用您的解决方案的代码片段来补充吗?@Leo我在代码片段中添加了另一个更好的解决方法。。