RecyclerView的项目无法滚动?

4

我正在处理Recycler view相关的问题,我在一个布局中将三个Recycler view横向放置。问题是第二和第三个Recycler view的项可以显示出来,但无法滚动,即使使用了内部滚动嵌套。这里附上xml文件,请建议如何使第二和第三个Recycler view能够滚动。

<RelativeLayout
    android:id="@+id/layout_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="220dp"
    android:background="@color/color_bottom_sheet"
    android:elevation="@dimen/z_bottom_sheet"
    app:behavior_hideable="true"
    app:behavior_peekHeight="80dp"
    app:layout_behavior="@string/string_bottom_sheet_behavior">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rightLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <LinearLayout
            android:id="@+id/lay1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ff0000">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewmonth"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:fillViewport="true"
            android:verticalScrollbarPosition="right">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:id="@+id/lay2"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00ff00">
                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerViewdate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
        <LinearLayout
            android:id="@+id/lay3"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewyear"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>
    <TextView
        android:id="@+id/text_view_sheet_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/activity_vertical_margin"
        android:text="@string/text_pull_to_show_more"
        android:textSize="@dimen/text_size_medium"
        android:visibility="gone" />
    <TextView
        android:id="@+id/text_view_more_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_view_sheet_title"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin"
        android:text="@string/text_more_contet_to_user"
        android:textSize="@dimen/text_size_big"
        android:textStyle="bold"
        android:visibility="gone" />
    <Button
        android:id="@+id/btnclick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/text_view_more_content"
        android:layout_marginLeft="@dimen/activity_vertical_margin"
        android:layout_marginRight="@dimen/activity_vertical_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/text_click_me"
        android:visibility="gone" />
</RelativeLayout>

//Java

@Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewmonth);
   RecyclerView recyclerdate = (RecyclerView) findViewById(R.id.recyclerViewdate);
   RecyclerView recycleryear = (RecyclerView) findViewById(R.id.recyclerViewyear);
   ButterKnife.bind(this);
   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
   LinearLayoutManager linearLayoutManagerdate = new LinearLayoutManager(getApplicationContext());
   LinearLayoutManager linearLayoutManageryear = new LinearLayoutManager(getApplicationContext());

   recyclerView.setLayoutManager(linearLayoutManager);
   recyclerdate.setLayoutManager(linearLayoutManagerdate);
   recycleryear.setLayoutManager(linearLayoutManageryear);

   recyclerView.setNestedScrollingEnabled(false);
   recyclerdate.setNestedScrollingEnabled(false);
   recycleryear.setNestedScrollingEnabled(false);

   for (int i = 0; i <= 31; i++) {
       dayList.add(String.valueOf(i));
   }
   CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, monthList);
   recyclerView.setAdapter(customAdapter);


   DayAdapter dayAdapter=new DayAdapter(MainActivity.this,dayList);
   recyclerdate.setNestedScrollingEnabled(false);
   recyclerdate.setAdapter(dayAdapter);
   recyclerdate.setNestedScrollingEnabled(false);

   YearAdapter yearAdapter=new YearAdapter(MainActivity.this,monthList);
   recycleryear.setAdapter(yearAdapter);

在你的 RecyclerView 中使用 android:nestedScrollingEnabled="false" - AskNilesh
@Nilesh Rathod recyclerView.setNestedScrollingEnabled(false); 不起作用。 - Rajakumar
@Rajakumar 测试用例 ViewCompat.setNestedScrollingEnabled(recyclerView,false); - AskNilesh
@NileshRathod 根据她的代码,我在下面设置了您的代码:recycleryear.setNestedScrollingEnabled(false); ViewCompat.setNestedScrollingEnabled(recyclerdate,false); 无法滚动。 - Rajakumar
3个回答

2
将所有的Recycler视图放在Nested Scroll View中。

1
Section 1: Programming is the process of designing, writing, testing, and maintaining computer programs. It involves using a programming language to communicate instructions to a computer.编程是设计、编写、测试和维护计算机程序的过程。它涉及使用编程语言向计算机传达指令。Section 2: There are many different programming languages, each with its own syntax and rules. Some popular programming languages include Java, Python, C++, and JavaScript.有许多不同的编程语言,每种语言都有自己的语法和规则。一些流行的编程语言包括Java、Python、C++和JavaScript。Section 3: Programmers use integrated development environments (IDEs) to write and test their code. IDEs provide tools for debugging, code completion, and version control.程序员使用集成开发环境(IDE)来编写和测试他们的代码。IDE提供调试、代码完成和版本控制等工具。Section 4: Programming is used in a wide range of applications, from creating websites and mobile apps to developing software for businesses and scientific research.编程在各种应用中被广泛使用,从创建网站和移动应用到为企业和科学研究开发软件。 - Rajakumar
如果您希望每个Recycler View显示完整的数据,那么是的,这将起作用;否则,为Recycler View定义特定的高度也可以起作用。在这两种情况下,您都必须将所有Recycler View放在嵌套滚动视图中。 - user4662227

2

请在您初始化recycler views的地方发布您的Java代码。此外,请尝试从嵌套滚动视图中删除recycler view(您可以将它们放置在没有嵌套滚动视图的位置)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rightLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewmonth"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
            <LinearLayout
                android:id="@+id/lay2"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00ff00">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerViewdate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
    <LinearLayout
        android:id="@+id/lay3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#0000ff">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewyear"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

1
@Tarun Kumar 需要分别滚动每个部分。 - Rajakumar
是的@Rajakumar,当将回收视图放置在不同的线性布局中时,它们会独立滚动。您无需将它们包装在嵌套滚动视图中。 - Tarun Kumar
在正确的布局中,给出weightsum: 3,并且对于布局1、2、3,给出布局权重:1。这样它们将水平排列并具有相等的间距。是的@Manohari,请删除嵌套的滚动视图并检查。 - Tarun Kumar
@TarunKumar 谢谢您的回复。在实施您的建议后,我遇到了一个问题,即 lay2 RecyclerView 无法滚动,不知道出了什么问题。 - Rajakumar
第三个回收视图怎么样了?现在可以滚动吗?还要检查日志,是否有异常。 - Tarun Kumar
@TarunKumarsure 我会检查日志...不仅第二个和第三个,也不会滚动。 - Rajakumar

-1
将您的父布局高度更改为match_parent。像这样:
<RelativeLayout
    android:id="@+id/layout_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_bottom_sheet"
    android:elevation="@dimen/z_bottom_sheet"
    app:behavior_hideable="true"
    app:behavior_peekHeight="80dp"
    app:layout_behavior="@string/string_bottom_sheet_behavior">


</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接