TabLayout里面RecyclerView的滚动问题
Scrolling Problem of RecyclerView inside TabLayout
下面是 TabLayout
的 XML 代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bapji.cubeon.FirstFragment"
android:id="@+id/first"
android:orientation="vertical"
android:background="@color/any">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="@color/any"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/white"
app:tabIndicatorColor="@color/white"
android:layout_above="@+id/container"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/any"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
下面是 RecyclerView
的 XML 代码
<android.support.constraint.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">
<android.support.v7.widget.RecyclerView
android:id="@+id/solve_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
这里我无法滚动此 RecyclerView,而在 TabLayout 中我只能获得该 RecyclerView 的固定项目和我看不到的其他项目,所以如果有人可以帮助我解决这个问题将对我有所帮助.
编辑 - 问题的屏幕截图
ScreenShot of problem
下面是 FragmentPagerAdapter 的代码
public class SectionPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
RecyclerViewAdapter java 代码
public class SolveAdapter extends RecyclerView.Adapter<SolveAdapter.SolveViewHolder> {
Context context;
List<EachTime> mSolves;
public SolveAdapter(Context context, List<EachTime> mSolves) {
this.context = context;
this.mSolves = mSolves;
}
@Nullable
@Override
public SolveViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layout;
layout = LayoutInflater.from(context).inflate(R.layout.fragment_each_time,parent,false);
return new SolveViewHolder(layout);
}
@Override
public void onBindViewHolder(SolveViewHolder holder, int position) {
holder.eachSolveTime.setText(mSolves.get(position).getCount());
holder.eachSolveScramble.setText(mSolves.get(position).getScramble());
holder.eachSolveDate.setText(mSolves.get(position).getDate());
}
@Override
public int getItemCount() {
return mSolves.size();
}
public class SolveViewHolder extends RecyclerView.ViewHolder{
TextView eachSolveTime,eachSolveDate,eachSolveScramble;
public SolveViewHolder(View itemView) {
super(itemView);
eachSolveDate = itemView.findViewById(R.id.each_solve_date);
eachSolveScramble = itemView.findViewById(R.id.each_solve_scramble);
eachSolveTime = itemView.findViewById(R.id.each_solve_title);
}
}
}
尝试在 ViewPager 上设置 android:layout_height="0dp" 因为您使用 android:layout_weight="1" 在您的 xml 代码中
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/any"
android:layout_weight="1"
/>
编辑 --
我认为你的问题与此重复
兄弟,我通过包含一个嵌套滚动视图来解决这个问题希望它对你有所帮助。
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/size20"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/size20"
android:layout_marginEnd="@dimen/size20"
android:layout_marginBottom="@dimen/size10" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
如果您添加页外限制,请移除查看寻呼机页外限制。
例如 viewPager.setOffscreenPageLimit(1);删除此行或注释然后尝试。
viewPager = findViewById(R.id.viewpager2);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//。在您的代码中注释以下行
// viewPager.setOffscreenPageLimit(1);
final MyAdapter1 adapter = new MyAdapter1(this,ArtistActivity.this,
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
然后在您的 XML 中添加 Nested ScrollView :
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:paddingBottom="10dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
请同时在您的 RecyclerView 中添加此行:android:nestedScrollingEnabled="false"。
下面是 TabLayout
的 XML 代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bapji.cubeon.FirstFragment"
android:id="@+id/first"
android:orientation="vertical"
android:background="@color/any">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="@color/any"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/white"
app:tabIndicatorColor="@color/white"
android:layout_above="@+id/container"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/any"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
下面是 RecyclerView
的 XML 代码<android.support.constraint.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">
<android.support.v7.widget.RecyclerView
android:id="@+id/solve_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
这里我无法滚动此 RecyclerView,而在 TabLayout 中我只能获得该 RecyclerView 的固定项目和我看不到的其他项目,所以如果有人可以帮助我解决这个问题将对我有所帮助.
编辑 - 问题的屏幕截图
ScreenShot of problem
下面是 FragmentPagerAdapter 的代码
public class SectionPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
RecyclerViewAdapter java 代码
public class SolveAdapter extends RecyclerView.Adapter<SolveAdapter.SolveViewHolder> {
Context context;
List<EachTime> mSolves;
public SolveAdapter(Context context, List<EachTime> mSolves) {
this.context = context;
this.mSolves = mSolves;
}
@Nullable
@Override
public SolveViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layout;
layout = LayoutInflater.from(context).inflate(R.layout.fragment_each_time,parent,false);
return new SolveViewHolder(layout);
}
@Override
public void onBindViewHolder(SolveViewHolder holder, int position) {
holder.eachSolveTime.setText(mSolves.get(position).getCount());
holder.eachSolveScramble.setText(mSolves.get(position).getScramble());
holder.eachSolveDate.setText(mSolves.get(position).getDate());
}
@Override
public int getItemCount() {
return mSolves.size();
}
public class SolveViewHolder extends RecyclerView.ViewHolder{
TextView eachSolveTime,eachSolveDate,eachSolveScramble;
public SolveViewHolder(View itemView) {
super(itemView);
eachSolveDate = itemView.findViewById(R.id.each_solve_date);
eachSolveScramble = itemView.findViewById(R.id.each_solve_scramble);
eachSolveTime = itemView.findViewById(R.id.each_solve_title);
}
}
}
尝试在 ViewPager 上设置 android:layout_height="0dp" 因为您使用 android:layout_weight="1" 在您的 xml 代码中
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/any"
android:layout_weight="1"
/>
编辑 --
我认为你的问题与此重复
兄弟,我通过包含一个嵌套滚动视图来解决这个问题希望它对你有所帮助。
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/size20"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/size20"
android:layout_marginEnd="@dimen/size20"
android:layout_marginBottom="@dimen/size10" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
如果您添加页外限制,请移除查看寻呼机页外限制。 例如 viewPager.setOffscreenPageLimit(1);删除此行或注释然后尝试。
viewPager = findViewById(R.id.viewpager2);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//。在您的代码中注释以下行 // viewPager.setOffscreenPageLimit(1);
final MyAdapter1 adapter = new MyAdapter1(this,ArtistActivity.this,
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
然后在您的 XML 中添加 Nested ScrollView :
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:paddingBottom="10dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
请同时在您的 RecyclerView 中添加此行:android:nestedScrollingEnabled="false"。