<i id='ItbNe'><tr id='ItbNe'><dt id='ItbNe'><q id='ItbNe'><span id='ItbNe'><b id='ItbNe'><form id='ItbNe'><ins id='ItbNe'></ins><ul id='ItbNe'></ul><sub id='ItbNe'></sub></form><legend id='ItbNe'></legend><bdo id='ItbNe'><pre id='ItbNe'><center id='ItbNe'></center></pre></bdo></b><th id='ItbNe'></th></span></q></dt></tr></i><div id='ItbNe'><tfoot id='ItbNe'></tfoot><dl id='ItbNe'><fieldset id='ItbNe'></fieldset></dl></div>
    1. <small id='ItbNe'></small><noframes id='ItbNe'>

      1. <tfoot id='ItbNe'></tfoot>
        • <bdo id='ItbNe'></bdo><ul id='ItbNe'></ul>
      2. <legend id='ItbNe'><style id='ItbNe'><dir id='ItbNe'><q id='ItbNe'></q></dir></style></legend>

        ViewPager 中的 ScrollView:滑动不起作用

        时间:2023-10-04

            <i id='4p27p'><tr id='4p27p'><dt id='4p27p'><q id='4p27p'><span id='4p27p'><b id='4p27p'><form id='4p27p'><ins id='4p27p'></ins><ul id='4p27p'></ul><sub id='4p27p'></sub></form><legend id='4p27p'></legend><bdo id='4p27p'><pre id='4p27p'><center id='4p27p'></center></pre></bdo></b><th id='4p27p'></th></span></q></dt></tr></i><div id='4p27p'><tfoot id='4p27p'></tfoot><dl id='4p27p'><fieldset id='4p27p'></fieldset></dl></div>
            <legend id='4p27p'><style id='4p27p'><dir id='4p27p'><q id='4p27p'></q></dir></style></legend>

            <small id='4p27p'></small><noframes id='4p27p'>

              <tbody id='4p27p'></tbody>
            <tfoot id='4p27p'></tfoot>
                <bdo id='4p27p'></bdo><ul id='4p27p'></ul>
                1. 本文介绍了ViewPager 中的 ScrollView:滑动不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三个片段的 ViewPager,其中之一是 FrameLayout,里面有 ScrollView:

                  I have ViewPager with three fragments and one of them is FrameLayout with ScrollView inside:

                  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
                  
                      <ScrollView
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content">
                  
                          <LinearLayout
                              android:id="@+id/table"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:orientation="vertical"/>
                      </ScrollView>
                  
                      <Button
                          android:id="@+id/buttonRemove"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@drawable/bg_button_red"
                          android:layout_gravity="bottom"
                          android:layout_margin="4dp"
                          android:text="@string/button_remove_last_round"
                          android:textColor="#ffffff"
                          android:textSize="24sp"/>
                  
                  
                  </FrameLayout>
                  

                  如果我在按钮上滑动,它就可以工作.但是如果我在 ScrollView 上滑动,它不起作用,只能向上/向下滚动

                  and if I swipe over button, it works. But if I swipe over the ScrollView, it doesn't work, only scroll up/down works

                  我试图覆盖 Scrollview 的 OnTouchEvent:

                  I tried to override OnTouchEvent of Scrollview:

                  @Override
                  public boolean onTouchEvent(MotionEvent ev) {
                  
                      switch (ev.getAction()){
                          case MotionEvent.ACTION_DOWN:
                              touchX = ev.getX();
                              touchY = ev.getY();
                              return super.onTouchEvent(ev);//if I change it to 'break', then only swipe works, but no scroll
                          case MotionEvent.ACTION_MOVE:
                              if(Math.abs(touchX-ev.getX())<40){
                                  return super.onTouchEvent(ev);//Scroll works perfectly
                              }else{
                                  return false;//Scroll disabled, but swipe still not working
                              }
                          case MotionEvent.ACTION_CANCEL:
                          case MotionEvent.ACTION_UP:
                              touchX=0;
                              touchY=0;
                              break;
                      }
                      return false;
                  }
                  

                  现在我可以禁用滚动,但是滑动仍然不起作用,如果X点之间的差异超过40,我将事件传递给viewpager,但是viewpager的onTouchListener没有得到这个事件;

                  Now I can disable scroll, but swipe still not working, if the difference between X points more than 40, I pass the event to the viewpager, but the viewpager's onTouchListener doesn't get this event;

                  推荐答案

                  好的,我在@yedidyak 的帮助下找到了解决方案.我写了我的自定义 ScrollView:

                  Ok, I found the solution with help of @yedidyak. I wrote my custom ScrollView:

                  public class CustomScrollView extends ScrollView {
                  
                  float touchX = 0;
                  float touchY = 0;
                  
                  ViewPager parentPager;
                  
                  public void setParentPager(ViewPager parentPager) {
                      this.parentPager = parentPager;
                  }
                  
                  public CustomScrollView(Context context, AttributeSet attrs) {
                      super(context, attrs);
                  }
                  
                  public CustomScrollView(Context context) {
                      super(context);
                  }
                  
                  @Override
                  public boolean onTouchEvent(MotionEvent ev) {
                  
                      switch (ev.getActionMasked()){
                          case MotionEvent.ACTION_DOWN:
                              touchX = ev.getX();
                              touchY = ev.getY();
                              return super.onTouchEvent(ev);
                          case MotionEvent.ACTION_MOVE:
                              if(Math.abs(touchX-ev.getX())<40){
                                  return super.onTouchEvent(ev);
                              }else{
                                  if (parentPager==null) {
                                      return false;
                                  } else {
                                      return parentPager.onTouchEvent(ev);
                                  }
                              }
                          case MotionEvent.ACTION_CANCEL:
                          case MotionEvent.ACTION_UP:
                              touchX=0;
                              touchY=0;
                              break;
                      }
                      return super.onTouchEvent(ev);
                  }
                  }
                  

                  然后在片段中,我将 viewpager 放到这个视图中,它可以完美运行

                  then in the fragment I put the viewpager to this view and it works perfectly

                  这篇关于ViewPager 中的 ScrollView:滑动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以在 Android 的 Viewpager 中使用 Horizontal Scrollview 吗? 下一篇:将drawable添加到CirclePageIndicator

                  相关文章

                2. <tfoot id='WyZ0y'></tfoot>
                  <legend id='WyZ0y'><style id='WyZ0y'><dir id='WyZ0y'><q id='WyZ0y'></q></dir></style></legend>
                  1. <small id='WyZ0y'></small><noframes id='WyZ0y'>

                      • <bdo id='WyZ0y'></bdo><ul id='WyZ0y'></ul>
                      <i id='WyZ0y'><tr id='WyZ0y'><dt id='WyZ0y'><q id='WyZ0y'><span id='WyZ0y'><b id='WyZ0y'><form id='WyZ0y'><ins id='WyZ0y'></ins><ul id='WyZ0y'></ul><sub id='WyZ0y'></sub></form><legend id='WyZ0y'></legend><bdo id='WyZ0y'><pre id='WyZ0y'><center id='WyZ0y'></center></pre></bdo></b><th id='WyZ0y'></th></span></q></dt></tr></i><div id='WyZ0y'><tfoot id='WyZ0y'></tfoot><dl id='WyZ0y'><fieldset id='WyZ0y'></fieldset></dl></div>