簡體   English   中英

如何使水平回收器視圖中的水平滾動視圖工作?

[英]How to make Horizontal Scroll View inside Horizontal Recycler View work?

我已經搜索了類似的問題並嘗試了幾個答案但沒有解決.. 我有顯示城市名稱和一些數據的水平回收器視圖問題是我將 canvas 條形圖或 RV 圖表放置在每個 position 的主回收器視圖適配器中,如下圖所示水平滾動此圖表我將圖表放在水平滾動視圖中以查看圖表的長水平方向數據,同時從右到左滾動此圖表,反之亦然,但實際上它不會僅水平滾動主回收器視圖滾動水平以顯示城市時觸摸它,並且對於 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 Horizontal 圖表凍結而不滾動雖然它在水平滾動視圖內

這是我的房車

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

將 WeatherChartViewBars 包裝在 HorizontalScrollView 內的 Horizontal LinearLayout 中。 它會起作用的。

@Brendon 讓我走上了正確的道路,因為我必須在觸摸水平滾動視圖項目(如條形圖)時停止在水平回收器視圖中滾動,關鍵是水平滾動視圖的 getparent。 我也為適配器視圖持有者中想要水平滾動它的每個項目做了它

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

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


            return false;
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM