【问题标题】:Scrolling Problem of RecyclerView inside TabLayoutTabLayout内RecyclerView的滚动问题
【发布时间】:2020-03-17 18:19:51
【问题描述】:

下面是 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>

在这里,我无法在 TabLayout 中滚动此 RecyclerView,我只能获得该 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);


    }
}
}

【问题讨论】:

    标签: android android-recyclerview android-tablayout


    【解决方案1】:

    尝试在 ViewPager 上设置 android:layout_height="0dp" 因为您在 xml 代码中使用 android:layout_weight="1"

    <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"
            />
    

    编辑 --

    我认为您的问题与answer 重复

    【讨论】:

    • 还是一样,我无法滚动整个列表
    • @devamdarji22 你确定你在 recyclerView 中的物品超过了手机屏幕的高度吗?
    • @devamdarji22 可以为适配器的屏幕孔代码添加屏幕截图
    • @devamdarji22 是的,请
    • 是的,我已经编辑了问题并添加了我面临的问题的屏幕截图
    【解决方案2】:

    伙计,我通过包含嵌套滚动视图来解决此问题,希望对您有所帮助。

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

    【讨论】:

      【解决方案3】:

      如果您添加了页外限制,请删除查看寻呼机的页外限制。 例如 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"。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-31
        • 2020-12-18
        • 2017-10-10
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        相关资源
        最近更新 更多