如何使水平回收器视图中的水平滚动视图工作?

时间:2021-03-10 17:49:38

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

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

enter image description here

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

<?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);

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

    holder.horizontalScrollView.setOnTouchListener(new 
        View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            
     holder.horizontalScrollView.getParent().
      requestDisallowInterceptTouchEvent(true);


            return false;
        }
    });