listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突
自定义listview
package com.exmple.listscrow; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; public class MyListView extends ListView { int mLastMotionY; boolean bottomFlag; public MyListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } //解决宽度的冲突 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 对height重新赋值 // heightMeasureSpec // = heightMeasureSpec = MeasureSpec.makeMeasureSpec(500/* * Integer.MAX_VALUE * >> 2 */, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
解决滑动的冲突
@Override public boolean dispatchTouchEvent(MotionEvent ev) { getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); }
}
在主方法里贴以下代码,以下这个代码可以实现以下效果,当listview滑到底部的时候,
ScrollView会和listview实现连动
package com.exmple.listscrow;
import java.util.ArrayList;
import java.util.List;
import com.exaple.vo.Super;
import android.R.fraction;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnScrollListener {
String[] name = new String[] { "陈克", "陈多多", "张小杰", "张杰", "宋静萌", "郑芳蓓",
"杨洋", "谢娜", "刘忠杰", "天下", "走路去纽约", "一切都值得", "最美的太阳", "我们都一样",
"娜样纯洁的爱恋", "一切都值得", "最美的太阳", "我们都一样", "娜样纯洁的爱恋" };
String[] price = new String[] { "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" };
private List<Super> list;
boolean island;
private MyListView listview;
private ScrollView sc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (MyListView) findViewById(R.id.listview);
sc = (ScrollView) findViewById(R.id.sc);
getData();
Apap ap = new Apap(list, MainActivity.this);
listview.setAdapter(ap);
listview.setOnScrollListener(this);
//不用自定义listview时用
/*listview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == 2) {
sc.requestDisallowInterceptTouchEvent(true);
} else {
sc.requestDisallowInterceptTouchEvent(false);
}
return false;
}
});
*/
// listview.getParent().requestDisallowInterceptTouchEvent(false);
}
public List<Super> getData() {
list = new ArrayList<Super>();
for (int i = 0; i < name.length; i++) {
Super super1 = new Super();
super1.setName(name[i]);
list.add(super1);
}
return list;
}
//
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (island && scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Toast.makeText(MainActivity.this, "kaisjia", 0).show();
sc.requestDisallowInterceptTouchEvent(false);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
island = (firstVisibleItem + visibleItemCount) == totalItemCount;
}
}