在 RecyclerView 中禁用滚动
Posted
技术标签:
【中文标题】在 RecyclerView 中禁用滚动【英文标题】:Disable Scrolling in RecyclerView 【发布时间】:2020-11-10 13:31:01 【问题描述】:在代码中,RecyclerView 在 ViewPager 中。
当 RecyclerView 大于 ViewPager 时,我想在 ViewPager 中启用滚动。
在recyclerView中展开所有列表,如何只能滚动viewPager?
我做了nestedScrollingEnabled="false"
,但没有工作,也不是好方法。
这是我的代码
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context="StoreFragment">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
//...
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:gravity="center"
android:background="@color/colorGray">
//...
</LinearLayout>
<RelativeLayout
android:layout_
android:layout_
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
//...
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewMenu"
android:layout_
android:layout_
android:layout_below="@+id/textMenuMain"
android:nestedScrollingEnabled="false"/>
</RelativeLayout>
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_
android:layout_
tools:context="Activity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_
android:layout_>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_
android:layout_>
<Toolbar...>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_
android:layout_
android:elevation="1dp"
app:tabTextColor="#000" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</androidx.viewpager.widget.ViewPager>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
Fragment.java
public class Fragment extends Fragment
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
List<String> list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
recyclerView = viewGroup.findViewById(R.id.recyclerViewMenu);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
list = new ArrayList<>();
for (int i = 0; i <= 10; i++)
list.add(i+1 + "List");
adapter = new MenuListAdapter(list);
recyclerView.setAdapter(adapter);
return viewGroup;
class MenuListAdapter extends RecyclerView.Adapter<MenuListAdapter.MyViewHolder>
List<String> list ;
public MenuListAdapter(List<String> lists)
this.list = lists;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.store_menu_item, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position)
String text = list.get(position);
holder.textMenuName.setText(text);
holder.textMenuPrice.setText("Price:00");
@Override
public int getItemCount()
return this.list.size();
public class MyViewHolder extends RecyclerView.ViewHolder
TextView textMenuName;
TextView textMenuPrice;
Button buttonCartAdd;
public MyViewHolder(View view)
super(view);
textMenuName = (TextView) view.findViewById(R.id.textMenuName);
textMenuPrice = (TextView) view.findViewById(R.id.textMenuPrice);
buttonCartAdd = (Button) view.findViewById(R.id.buttonCartAdd);
Activity.java
public class Activity extends AppCompatActivity
TabLayout tabLayout;
ViewPager viewPager;
TextView textViewNotice;
TextView textViewTitle;
String title;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_detail);
tabLayout = findViewById(R.id.tabs);
viewPager = findViewById(R.id.pager);
textViewNotice = findViewById(R.id.textNotice);
textViewTitle = findViewById(R.id.textTitle);
textViewNotice.setSelected(true);
Intent intent = getIntent();
title = intent.getExtras().getString("Title");
textViewTitle.setText(title);
tabLayout.addTab(tabLayout.newTab().setText("정보"));
tabLayout.addTab(tabLayout.newTab().setText("리뷰"));
Activity.MyPagerAdapter adapter = new Activity.MyPagerAdapter(getSupportFragmentManager());
Fragment Fragment = new Fragment();
adapter.addItem(Fragment);
//...
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
viewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
class MyPagerAdapter extends FragmentStatePagerAdapter
ArrayList<Fragment> items = new ArrayList<>();
public MyPagerAdapter(FragmentManager fm)
super(fm);
public void addItem(Fragment item)
items.add(item);
@NonNull
@Override
public Fragment getItem(int position)
return items.get(position);
@Override
public int getCount()
return items.size();
【问题讨论】:
【参考方案1】:试试这个
LinearLayoutManager layoutManager = new LinearLayoutManager(context)
@Override
public boolean canScrollVertically()
return false;
;
recyclerView.setLayoutManager(layoutManager);
【讨论】:
以上是关于在 RecyclerView 中禁用滚动的主要内容,如果未能解决你的问题,请参考以下文章
带有 MotionLayout 的 Android 折叠工具栏 - 当 RecyclerView 为空/不可滚动时禁用运动
Android:为什么TextView中的文本在LinearLayout中滚动而在Recyclerview中滚动