[英]ScrollView Inside ScrollView
我知道谷歌的人已經要求我們不要將Scrollable視圖放在另一個Scrollable視圖中,但他們是否有任何官方聲明指示我們不要這樣做?
試試這個吧
注意:這里parentScrollView
表示外部ScrollView,而childScrollView
表示childScrollView
ScrollView
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "PARENT TOUCH");
findViewById(R.id.child_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Atul Bhardwaj上面的回答是正確的方法。 但是如果有人需要將它應用到你對父母的控制較少的ScrollView,我認為這足夠靈活,只是它應該工作的方式:
private void makeMyScrollSmart() {
myScroll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View __v, MotionEvent __event) {
if (__event.getAction() == MotionEvent.ACTION_DOWN) {
// Disallow the touch request for parent scroll on touch of child view
requestDisallowParentInterceptTouchEvent(__v, true);
} else if (__event.getAction() == MotionEvent.ACTION_UP || __event.getAction() == MotionEvent.ACTION_CANCEL) {
// Re-allows parent events
requestDisallowParentInterceptTouchEvent(__v, false);
}
return false;
}
});
}
private void requestDisallowParentInterceptTouchEvent(View __v, Boolean __disallowIntercept) {
while (__v.getParent() != null && __v.getParent() instanceof View) {
if (__v.getParent() instanceof ScrollView) {
__v.getParent().requestDisallowInterceptTouchEvent(__disallowIntercept);
}
__v = (View) __v.getParent();
}
}
該函數的作用是向myScroll
添加一個觸摸偵聽器,當觸摸在子節點中啟動時禁用父觸摸攔截,然后在觸摸實際結束時將其恢復。 您不需要對父ScrollView
的引用,它不必是直接父級...它將在顯示列表中移動直到找到它。
在我看來,兩全其美。
這夠近了嗎?
你永遠不應該使用帶有ListView的HorizontalScrollView,因為ListView負責自己的滾動。 最重要的是,這樣做會使ListView中的所有重要優化都無法處理大型列表,因為它有效地強制ListView顯示其整個項目列表以填充HorizontalScrollView提供的無限容器。
http://developer.android.com/reference/android/widget/HorizontalScrollView.html
更新:
由於您可能被迫使用二維滾動視圖,您可以考慮使用此: blog.gorges.us/2010/06/android-two-dimensional-scrollview/的Internet存檔
我沒有用過這個,但這可能是一個合理的方法。
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
v.getParent()=父scrollView。
這是一個可能的解決方案。 當到達子ScrollView的末尾時,它將控件傳遞給父ScrollView進行滾動。 它適用於ScrollView中的ScrollView和ListView。
第1步 - 設置父OnTouchListener
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
第2步 - 設置子項的OnTouchListener(ScrollView或ListView)
aChildScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
v.getParent().requestDisallowInterceptTouchEvent(shouldRequestDisallowIntercept((ViewGroup) v, event));
return false;
}
});
aListView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(shouldRequestDisallowIntercept((ViewGroup) v, event));
return false;
}
});
第3步 - 這是正確功能所需的魔術方法
protected boolean shouldRequestDisallowIntercept(ViewGroup scrollView, MotionEvent event) {
boolean disallowIntercept = true;
float yOffset = getYOffset(event);
if (scrollView instanceof ListView) {
ListView listView = (ListView) scrollView;
if (yOffset < 0 && listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() >= 0) {
disallowIntercept = false;
}
else if (yOffset > 0 && listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1 && listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
disallowIntercept = false;
}
}
else {
float scrollY = scrollView.getScrollY();
disallowIntercept = !((scrollY == 0 && yOffset < 0) || (scrollView.getHeight() + scrollY == scrollView.getChildAt(0).getHeight() && yOffset >= 0));
}
return disallowIntercept;
}
protected float getYOffset(MotionEvent ev) {
final int historySize = ev.getHistorySize();
final int pointerCount = ev.getPointerCount();
if (historySize > 0 && pointerCount > 0) {
float lastYOffset = ev.getHistoricalY(pointerCount - 1, historySize - 1);
float currentYOffset = ev.getY(pointerCount - 1);
float dY = lastYOffset - currentYOffset;
return dY;
}
return 0;
}
我找到了一個很好的解決方案。 請使用此代碼。
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Utils.showLog("PARENT TOUCH");
findViewById(R.id.activity_mesh_child_scrollView).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Utils.showLog("CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
這肯定會奏效。 如果不能正常工作,請嘗試告訴我。
[......]是否有任何官方聲明指示我們不要這樣做?
我想雖然我似乎無法在筆記中找到它。 我知道在嘗試在列表活動中使用滾動視圖時,我發現了這樣的聲明。 我認為Android UI系統處理嵌套滾動條的方式實際上存在一個邏輯焦點“bug”,可能應該更好地檢測並傳達給開發人員。 但我的建議是......
最后,為了用戶的利益,最好考慮單個可滾動視圖。 這就像在HTML頁面上的滾動條內有滾動條; 它可能是合法的,但卻是一種糟糕的用戶體驗。
實際上,有一個關於它的官方聲明,在一個名為“ ListView世界 ” 的相當古老的視頻中。 他們說不要將任何可滾動視圖放在另一個視圖中(當兩者都在同一方向時)。
但是,現在我們有了一個新視圖,允許兩個視圖同時滾動,可能顯示一個很酷的效果:
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
我沒有找到任何這方面的例子,所以我寫的只是猜測它的作用和它的用途。
Android支持v4庫有一個名為NestedScrollView的類。
嘗試嵌套滾動視圖: http : //ivankocijan.xyz/android-nestedscrollview/
您可以將ScrollView放在另一個ScrollView中。 只需擴展子ScrollView以覆蓋onTouchEvent方法。 像這樣
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class ChildScrollView extends android.widget.ScrollView {
private int parent_id;
public ChildScrollView(Context context) {
super(context);
}
public ChildScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ChildScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
在這里,我在ScrollView中創建了一個與ScrollView相關的示例項目。 一種觀點是可滾動的兩種方式。 看看這個 :-
MainActivity.java -
package com.example.dev_task_193_scrollview;
import com.example.dev_task_196_scrollview.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
ImageView imageView1,imageView2,imageView3,IVimage1,IVimage2,IVimage3,IVimage4,IVimage5,IVimage6;
ListView listView1,listView2;
HorizontalScrollView horizontalScrollView1,horizontalScrollView2;
ScrollView parentScrollView, scrollView1;
RelativeLayout relativeLayout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobileWindowsMobileWindowsMobileWindowsMobile" };
relativeLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setBackgroundResource(R.drawable.info);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setBackgroundResource(R.drawable.info);
imageView3 = (ImageView) findViewById(R.id.imageView3);
imageView3.setBackgroundResource(R.drawable.info);
listView1 = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item, values);
listView1.setAdapter(adapter);
listView2 = (ListView) findViewById(R.id.listView2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
R.layout.list_item, values);
listView2.setAdapter(adapter1);
parentScrollView = (ScrollView) findViewById(R.id.parentScrollView);
scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
horizontalScrollView1 = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
horizontalScrollView2 = (HorizontalScrollView) findViewById(R.id.horizontalScrollView2);
IVimage1 = (ImageView) findViewById(R.id.IVimage1);
IVimage2 = (ImageView) findViewById(R.id.IVimage2);
IVimage3 = (ImageView) findViewById(R.id.IVimage3);
IVimage4 = (ImageView) findViewById(R.id.IVimage4);
IVimage5 = (ImageView) findViewById(R.id.IVimage5);
IVimage6 = (ImageView) findViewById(R.id.IVimage6);
scrollView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
horizontalScrollView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
listView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked "+parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
listView2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
horizontalScrollView2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
/*imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
imageView3.setOnClickListener(this);*/
IVimage1.setOnClickListener(this);
IVimage2.setOnClickListener(this);
IVimage3.setOnClickListener(this);
IVimage4.setOnClickListener(this);
IVimage5.setOnClickListener(this);
IVimage6.setOnClickListener(this);
imageView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
return false;
}
});
imageView2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
return false;
}
});
imageView3.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// Disallow the touch request for parent scroll on touch of child view
parentScrollView.requestDisallowInterceptTouchEvent(true);
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.imageView1:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.imageView2:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.imageView3:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage1:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage2:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage3:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage4:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage5:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.IVimage6:
Toast.makeText(getApplicationContext(), "Clicked "+v.getTag(), Toast.LENGTH_SHORT).show();
break;
}
// TODO Auto-generated method stub
}
}
activity_main.xml -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/login_bg" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="300dp" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fillViewport="false" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/bg" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="400dp"
android:tag="imageView1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_toRightOf="@+id/imageView1"
android:tag="imageView2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="300dp"
android:layout_height="400dp"
android:layout_toRightOf="@+id/imageView2"
android:tag="imageView3" />
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
<ListView
android:id="@+id/listView1"
android:layout_width="500dp"
android:layout_height="400dp"
android:layout_below="@+id/scrollView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/ic_launcherwrweq" >
</ListView>
<HorizontalScrollView
android:id="@+id/horizontalScrollView2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:background="@drawable/claim_detail_header_bg"
android:fillViewport="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/IVimage1"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/a"
android:tag="a" >
</ImageView>
<ImageView
android:id="@+id/IVimage2"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/b"
android:tag="b" >
</ImageView>
<ImageView
android:id="@+id/IVimage3"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/c"
android:tag="c" >
</ImageView>
<ImageView
android:id="@+id/IVimage4"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/g"
android:tag="g" >
</ImageView>
<ImageView
android:id="@+id/IVimage5"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/e"
android:tag="e" >
</ImageView>
<ImageView
android:id="@+id/IVimage6"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/f"
android:tag="f" >
</ImageView>
</LinearLayout>
</HorizontalScrollView>
<ListView
android:id="@+id/listView2"
android:layout_width="500dp"
android:layout_height="400dp"
android:layout_below="@+id/horizontalScrollView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="@drawable/ic_launcherwrweq" >
</ListView>
</RelativeLayout>
</ScrollView>
list_item.xml(用於ListView) -
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="25sp"
android:maxLines="1"
android:singleLine="true"
/>
如果有人正在尋找答案,我的實現略有不同。 我擴展了ScrollView類並在子節點中實現了onTouchListener,並在構造函數中將其設置為self。
在onTouch回調中,如果運動事件對象帶有指針計數值為2,則返回true,否則返回false。 這樣,如果兩個手指在屏幕上移動,它會將其視為縮放的縮放,否則會將其視為正常滾動。 我沒有要求父母觸摸禁用等。
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getPointerCount() == 2){
mCallbacks.onPinchZoomAction(motionEvent);
return true;
}
return false;
}
不僅谷歌說它的不良做法,它只是沒有多大意義。 假設您有兩個嵌套在另一個內部的垂直可滾動視圖。 當您將手指移動到滾動視圖上時,您要移動哪一個,內部還是外部?
你應該重新考慮你的UI設計不需要這個,有很多方法可以創建一個很棒的用戶界面,並且仍然保持簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.