问题
In my activity we have view pager which shows tabs and other views at top. ViewPager tabs shows different Fragments. Below is the code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:gravity="center"
android:text="Other views here" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.tabs.TabLayout
android:id="@+id/service_advisory_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:minHeight="46dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:tabIndicatorColor="@color/colorBlack"
app:tabIndicatorGravity="bottom"
app:tabIndicatorHeight="4dp"
app:tabMinWidth="100dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorBlack"
app:tabTextAppearance="@style/TabTextAppearance"
app:tabTextColor="@color/colorBlack">
</com.google.android.material.tabs.TabLayout>
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var fragmentsList: ArrayList<Fragment>
private lateinit var pagerAdapter: CardAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fragmentsList = ArrayList()
pagerAdapter = CardAdapter(supportFragmentManager)
pagerAdapter.addFrag(Tab1Fragment(),"TAB1")
pagerAdapter.addFrag(Tab2Fragment(),"TAB2")
pagerAdapter.addFrag(Tab3Fragment(),"TAB3")
container.adapter = pagerAdapter
}
}
CardAdapter
class CardAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getItem(position: Int): Fragment {
return mFragmentList.get(position)
}
fun addFrag(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
override fun getCount(): Int {
return mFragmentList.size
}
@Nullable
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList.get(position)
}
}
I want to make make main activity as scrolable so that when my view pager fragment content is more i can scroll up to see whole data. Can someone suggest like is ScrollView works when it has view pager?
来源:https://stackoverflow.com/questions/59056540/scrollview-not-working-when-it-has-viewpager-in-android