【问题标题】:How to make Horizontal Scroll View inside Horizontal Recycler View work?如何使水平回收器视图中的水平滚动视图工作?
【发布时间】:2021-06-08 17:15:09
【问题描述】:

我搜索了类似的问题并尝试了几个答案但没有解决.. 我有显示城市名称和一些数据的水平回收器视图 问题是我在 Main Recycler View 适配器中的每个位置都放置了如下图的画布条形图或 RV 图表并水平滚动此图表我将图表放在水平滚动视图中以在滚动此图表时查看长水平方向的图表数据从右到左,反之亦然,但实际上它不滚动水平仅主回收器视图滚动水平以在触摸它时显示城市,并且对于 RV 内的此图表在触摸它滚动时没有滚动(冻结) 回收站视图是否仅水平滚动并阻止其子项内的任何其他滚动??

这是我的回收站视图项目布局

<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.core.widget.NestedScrollView
    android:id="@+id/scv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        android:padding="10dp">
    <androidx.cardview.widget.CardView
        android:id="@+id/main_cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="12dp"
        app:cardBackgroundColor="@android:color/transparent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayoutWeather"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtCityName2"
                android:textColor="@color/text_light"
                android:textSize="42sp"
                android:textStyle="bold"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:paddingTop="10dp">

                <ImageView
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:id="@+id/imgWeather2"
                    android:src="@mipmap/ic_launcher"/>

                <TextView
                    android:paddingLeft="10dp"
                    android:id="@+id/txtCityTemperature2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="30 C"
                    android:textColor="@color/text_light"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    android:paddingTop="45dp"/>

            </LinearLayout>

            
        </LinearLayout>
    </androidx.cardview.widget.CardView>


        <RelativeLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <HorizontalScrollView             // here is the problem (doesn't scroll)

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_marginStart="0dp"
                android:layout_marginEnd="0dp"
                android:scrollbars="horizontal">


                <WeatherChartViewBars2
                    android:id="@+id/bars"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:isScrollContainer="true"

                    />
            </HorizontalScrollView>

        </RelativeLayout>


    </LinearLayout>
</androidx.core.widget.NestedScrollView>
 </RelativeLayout>

我试图让 Recycler View 布局管理器垂直滚动并且水平图表成功水平滚动但是当使 RV Horizo​​ntal 图表冻结而不滚动虽然它在水平滚动视图中

这是我的房车

  recyclerViewWeather.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, 
  false));
    adapter = new WeatherAdapter(this, cityList, dataManager);
   recyclerViewWeather.setHasFixedSize(true);
    recyclerViewWeather.setAdapter(adapter);

   recyclerViewWeather.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE){
               position = getCurrentItem();
               

                try {
                    UpdateDailyWeather();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    });
    PagerSnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(recyclerViewWeather);

【问题讨论】:

    标签: android android-studio android-recyclerview horizontal-scrolling nestedrecyclerview


    【解决方案1】:

    将 WeatherChartViewBars 包装在 Horizo​​ntalScrollView 内的 Horizo​​ntal LinearLayout 中。它会起作用的。

    【讨论】:

    • 谢谢.. 它在模拟器上不起作用,但我在实际设备中尝试过它的工作速度非常慢并且冻结水平移动条形图。我的意思是条形图冻结和城市回收器视图水平滚动,因此结果条形图滚动非常慢或在尝试水平滚动时冻结,如果我使 RV 垂直,它可以很好地工作滚动条水平
    • 我还没有考虑让回收站视图线性布局管理器水平阻止任何水平滚动,即使我把这个条形图放在水平滚动视图中,主 RV 只水平滚动并冻结滚动其中的任何子项
    • 当触摸条形图锁定回收站视图从水平滚动并且只允许条形图滚动并且完成后允许 RV 滚动时是否存在
    • 我认为它接近我想要的,但解决方案似乎错误或不清楚,可能是我必须在触摸子 recyclerview 项目时阻止父 recyclerview 滚动但我不知道如何实现
    【解决方案2】:

    @Brendon 让我走上了正确的道路,因为我必须在触摸水平滚动视图项目(如条形图)时停止在水平回收器视图中滚动,关键是水平滚动视图的 getparent。我也为适配器视图持有者中想要水平滚动它的每个项目做了它

        holder.horizontalScrollView.setOnTouchListener(new 
            View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                
         holder.horizontalScrollView.getParent().
          requestDisallowInterceptTouchEvent(true);
    
    
                return false;
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多