Open sunshaobei opened 1 year ago
class MainActivity : AppCompatActivity() {
val adapter = MyAdapter(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<ViewPager2>(R.id.vp).adapter = adapter
}
}
class MyAdapter(activity: MainActivity) : FragmentStateAdapter(activity) {
override fun getItemCount(): Int {
return 1
}
override fun createFragment(position: Int): Fragment {
return MainFragment()
}
}
------------------------------------------------------- activity_main.layout
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
/**
* @author sunshaobei on 2023/8/4
*/
class MainFragment:Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val dataBinding:FragmentMainBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_main,container,false)
dataBinding.testBtn.setOnClickListener {
val value = dataBinding.tv.isLayoutRequested
dataBinding.cl.isVisible = !dataBinding.cl.isVisible
}
return dataBinding.root
}
}
------------------------------------------------------- fragment_main.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">
<data>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl"
android:layout_width="match_parent"
android:layout_height="30dp"
android:visibility="gone"
android:layout_gravity="center">
<View
android:id="@+id/tv"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:paddingStart="6dp"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/test_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="show"
android:text="显示"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</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">
<data>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/cl"
android:layout_width="match_parent"
android:layout_height="30dp"
android:visibility="gone"
android:layout_gravity="center">
<View
android:id="@+id/tv"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:paddingStart="6dp"
android:textSize="13sp"
/>
</LinearLayout>
<Button
android:id="@+id/test_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="show"
android:text="显示"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</layout>
if i change the ContraintLayout to other viewgroup , that will be fine, or i delete the paddingStart of contraintlayout's child that will be fine too
or i just remove the viewpager2(recyclerview) that will be fine
i have no idear about this problem , and i have read a lot of source code of View、constraintlayout and recyclerview , then i found nothing
So, can you determine if this problem is ConstraintLayout‘s problem??? or recyclerview‘s
In this case , i found recyclerView+ConstraintString with RTL set like paddingStrat will be making this problem ,i fix this problem with a custom ConstraintLayout witch override it's addview(function) and add getPaddingStart() code only
layout : ViewPage2 or RecyclerView bind a itemView witch is ConstraintLayout with a child , and this child set paddingStart or paddingEnd,
first: I set the ConstraintLayout‘visibility Gone
second : when the view created i set ConstraintLayout‘visibility visible
then I found the child can not show