<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注:本文参考http://www.eoeandroid.com/thread-246995-1-1.html中内容,但是原文有些地方说的不太清楚,在使用时还需要调好久,特此记录。感谢原文作者。</span>
问题:
ScrollView嵌套ListView冲突一般表现在两个方面:1.ListView只显示一行内容,2.ListView无法滚动
解决方案主要思想:
当ScrollView中嵌套ListView时,如果滑动的是ListView,则让ScrollView交出Touch权限,当滑动的是ScrollView时,则让ScrollView重新获取Touch权限。
运行结果图片:
步骤:
1.自定义ListView
package com.wuhang.test.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.ScrollView;
public class InnerListView extends ListView {
ScrollView parentScrollView;
public ScrollView getParentScrollView() {
return parentScrollView;
}
public void setParentScrollView(ScrollView parentScrollView) {
this.parentScrollView = parentScrollView;
}
private int maxHeight;
public int getMaxHeight() {
return maxHeight;
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
public InnerListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
if (maxHeight > -1) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(getChildAt(0));
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
setParentScrollAble(false);
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
setParentScrollAble(true);
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
/**
* @param flag
*/
private void setParentScrollAble(boolean flag) {
parentScrollView.requestDisallowInterceptTouchEvent(!flag);
}
}
每个Item对应的布局文件为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:id="@+id/list_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:layout_marginLeft="50dip"
android:text="1"
android:textColor="#000"
android:textSize="16dp"
tools:context=".ListViewActivity" />
</LinearLayout>
ScrollView的requestDisallowInterceptTouchEvent方法是控制ScrollView是否获取Touch权限。
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/sv_outter"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<com.wuhang.test.view.InnerListView
android:id="@+id/lv_student"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.wuhang.test.view.InnerListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_worlddfgdgdf" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_worlddfgdgdf" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#229977" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
3.在MainActivity中使用
在使用中有个注意点,一定要调用使用InnerListView对象调用
setMaxHeight(int maxHeight)
否则不能显示ListView(不能在布局文件中通过layout_height设置listview的高度是不可用的)。
MainActivity代码如下:
package com.wuhang.test.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ScrollView; import com.wuhang.test.R; import com.wuhang.test.view.InnerListView; public class MainActivity extends AppCompatActivity { private InnerListView mListView; private ScrollView mScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); init(); } private void init() { mListView.setMaxHeight(500); String[] strs = {"1","2","3","4","5","6","7","8","9","10","11","12","13"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs); mListView.setAdapter(adapter); //mScrollView.smoothScrollTo(0,0); } private void findView() { mScrollView = (ScrollView) findViewById(R.id.sv_outter); mListView = (InnerListView) findViewById(R.id.lv_student); mListView.setParentScrollView(mScrollView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
源代码下载:http://download.csdn.net/detail/kaoshibuguaa/9001583