另一个滚动视图中的 Android 滚动视图不滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21824542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android scrollview inside another scrollview doesn't scroll
提问by Mihai Boisteanu
I'm trying to put a ScrollView inside another ScrollView and I tried the answers found on this site but it still doesn't seems to work completely. Here is the XML:
我正在尝试将 ScrollView 放在另一个 ScrollView 中,我尝试了在此站点上找到的答案,但它似乎仍然无法完全工作。这是 XML:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewDirectoryDetailName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/red" />
<LinearLayout
android:id="@+id/linearLayoutDirectoryDetailContainImage"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="@drawable/general_image"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:focusableInTouchMode="false" >
<TextView
android:id="@+id/textViewDirectoryDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="phone"
android:text="098 123 45 678"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
And here is the Java code:
这是Java代码:
parentScrollView = (ScrollView) findViewById(R.id.scrollView1);
childScrollView = (ScrollView) findViewById(R.id.scrollView2);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View p_v, MotionEvent p_event) {
childScrollView.getParent().requestDisallowInterceptTouchEvent(
false);
// We will have to follow above for all scrollable contents
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View p_v, MotionEvent p_event) {
// this will disallow the touch request for parent scroll on
// touch of child view
p_v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
// We will have to follow above for all child scrollable contents
What seems to happen is this: when I have text in the TextView it doesn't seem to let me scroll it at all. It just scrolls the parent. But when I have no text and I touch the child ScrollView it doesn't scroll the parent so I guess it's working then. But do you have any ideas why it doesn't work when I have text?
似乎发生的是这样的:当我在 TextView 中有文本时,它似乎根本不允许我滚动它。它只是滚动父级。但是当我没有文本并且我触摸子 ScrollView 时,它不会滚动父项,所以我猜它当时正在工作。但是你知道为什么当我有文字时它不起作用吗?
回答by Volodymyr Kulyk
You should use NestedScrollView.
您应该使用NestedScrollView。
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- -->
</android.support.v4.widget.NestedScrollView>
回答by devgrg
Old Question though, I am posting what worked for me,I understand the issue Coz i have gone through it too, I just removed the ScrollView for TextView in XML, rather implemented scrolling feature for TextView in java code. Thus, XML would be:
老问题虽然,我发布了对我有用的内容,我理解这个问题,因为我也经历过它,我只是在 XML 中删除了 TextView 的 ScrollView,而是在 Java 代码中为 TextView 实现了滚动功能。因此,XML 将是:
<TextView
android:id="@+id/textViewDirectoryDetailName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/red" />
<LinearLayout
android:id="@+id/linearLayoutDirectoryDetailContainImage"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="@drawable/general_image"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textViewDirectoryDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="phone"
android:text="098 123 45 678"
android:textAppearance="?android:attr/textAppearanceLarge" />
and the Java Code would be
和 Java 代码将是
TextView extViewDD =
(TextView)findViewById(R.id.textViewDirectoryDescription);
//for Scrolling of textView
textViewDD.setMovementMethod(new ScrollingMovementMethod());
//to stop parent linearlayout scrollview when touched on textview
textViewDD.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
回答by Sahil Mahajan Mj
Having a Scroll View inside another another Scroll View is not a good practice. However the code you have used could work in some cases. But it might fail when you are having multiple elements. In that case, it might not be able to identify the gesture.
在另一个 Scroll View 中放置一个 Scroll View 不是一个好习惯。但是,您使用的代码在某些情况下可以工作。但是当你有多个元素时它可能会失败。在这种情况下,它可能无法识别手势。
However, you can try this answer, if it helps ScrollView Inside ScrollView. It disables the parent ScrollView's touch, when a touch for the child is identified. Also have a look at this postif it helps.
但是,您可以尝试此答案,如果它有助于ScrollView 内部 ScrollView。当识别到孩子的触摸时,它会禁用父滚动视图的触摸。如果有帮助,也可以看看这篇文章。
回答by TeeTracker
I use RectF to test whether you finger locates in the range of your child_view.
我使用 RectF 来测试您的手指是否位于 child_view 的范围内。
I've used it for a case that a mix between scrollview and viewpager(that contains two pages with recycleviews).
我已经将它用于混合滚动视图和视图页面(包含两个带有回收视图的页面)的情况。
Try these codes and you will get what you wanna.
试试这些代码,你会得到你想要的。
findViewById(R.id.parent_view).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
View childV = findViewById(R.id.child_view);
if (childV != null) {
int[] l = new int[2];
childV.getLocationOnScreen(l);
RectF rect = new RectF(l[0], l[1], l[0] + childV.getWidth(), l[1] + childV.getHeight());
if(rect.contains( event.getX(), event.getY())) {
childV.getParent()
.requestDisallowInterceptTouchEvent(false);
childV.onTouchEvent(event);
return true;
}
}
childV.getParent()
.requestDisallowInterceptTouchEvent(true);
return false;
}
});
回答by venky
you can not use two Scrollview inside Scrollview OS get confuse which one have to scroll so your activity hang so do not use like this
您不能在 Scrollview OS 中使用两个 Scrollview 混淆哪一个必须滚动所以您的活动挂起所以不要像这样使用
回答by cescom
I'm almost sure it is impossible:
我几乎可以肯定这是不可能的:
How android handles touch events:
android如何处理触摸事件:
Parent gets event and decides if it has to handle it or if it should let handle it to the child;
父获取事件并决定它是否必须处理它或者是否应该让它处理它给孩子;
so,if a scrollview handles the touch, the one which handles it, is always the parent one
因此,如果滚动视图处理触摸,则处理它的始终是父视图
http://developer.android.com/training/gestures/viewgroup.html
http://developer.android.com/training/gestures/viewgroup.html
personally i don't know if is there a way to understand what is touched behind the parent scrollview to see if it is another scrollview and let it handle the movement
我个人不知道是否有办法了解父滚动视图背后触摸的内容,以查看它是否是另一个滚动视图并让它处理移动
回答by i.n.e.f
check thislink which shows that how to use two widget with scroll functionality in same activity. check this below code in that.
检查此链接,该链接显示如何在同一活动中使用具有滚动功能的两个小部件。检查下面的代码。
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v("PARENT", "PARENT TOUCH");
findViewById(R.id.child_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v("CHILD", "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
回答by Nicolai Nita
Here is a possible solution which works fine with ScrollView and ListView inside a ScrollView. ScrollView Inside ScrollView
这是一个可能的解决方案,它适用于 ScrollView 中的 ScrollView 和 ListView。ScrollView 内部 ScrollView
回答by Hanaa Mohamed
use this custom scrollview as inner scrollview:
将此自定义滚动视图用作内部滚动视图:
public class TouchMeScrollView extends ScrollView {
public TouchMeScrollView(Context context) {
super(context);
}
public TouchMeScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TouchMeScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(ev);
}
}