listView中Item有事件处理并且改变了子布局中某控件的呈现时,由于getView()返回View的复用性,导致滑动的时候出现复用的样式,具体问题如下:内部监听问题可参照:http://blog.csdn.net/tom_xiaoxie/article/details/50827185
解决办法:重写listView的onMeasure()方法:
public class ScrollListView extends ListView{ public ScrollListView(Context context) { super(context); } public ScrollListView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
将之前用到的ListView换成重写后的ListView,并在外层布局嵌套一层ScrollView。这样就可以解决问题。修改后的布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.example.tom.study.testListView.ScrollListView android:id="@+id/test_listIner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView> </RelativeLayout>
这种解决方式跟ScrollView中嵌套ListView滑动冲突解决的方式一样,可查看:http://blog.csdn.net/tom_xiaoxie/article/details/50500538