关于ScrollView和listview的冲突关于的滑动和宽度
listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突
自定义listview
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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滑到底部的时候,
1 | ScrollView会和listview实现连动 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | 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; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步