溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ScrollView viewPager ListView 結(jié)合使用的問題及解決辦法

發(fā)布時間:2020-07-22 10:31:48 來源:網(wǎng)絡(luò) 閱讀:1077 作者:wutongkege 欄目:開發(fā)技術(shù)

1. 解決ScrollView 和viewPager滑動沖突的問題

    需要重寫ScrollView ,使得viewpager獲取到橫向滑動事件

    代碼如下

public class PagerScrollView extends ScrollView {  

   

    private GestureDetector mGestureDetector;  

 

    public PagerScrollView(Context context, AttributeSet attrs, int defStyle) {  

        super(context, attrs, defStyle);  

        init();  

    }  

 

    public PagerScrollView(Context context) {  

        super(context);  

        init();  

    }  

 

    public PagerScrollView(Context context, AttributeSet attrs) {  

        super(context, attrs);  

        init();  

    }  

 

    private void init() {  

        mGestureDetector = new GestureDetector(getContext(),  

                new YScrollDetector());  

        setFadingEdgeLength(0);  

    }  

 

    @Override  

    public boolean onInterceptTouchEvent(MotionEvent ev) {  

        return super.onInterceptTouchEvent(ev)  

                && mGestureDetector.onTouchEvent(ev);  

    }  

 

    private class YScrollDetector extends SimpleOnGestureListener {  

        @Override  

        public boolean onScroll(MotionEvent e1, MotionEvent e2,  

                float distanceX, float distanceY) {  

             

            if (Math.abs(distanceY) >= Math.abs(distanceX)) {  

                return true;  

            }  

            return false;  

        }  

    }  

}  

2. viewpager在scrollVIew不顯示問題

    

    需要設(shè)置scrollView 的屬性 android:fillViewport="true"

    viewPager先給一個固定高度,之后會動態(tài)改變

3. viewPager中加入ListView不能滾動問題

    需要計(jì)算listView的高度,同時需要動態(tài)設(shè)置viewpager的高度

    

     重寫listView

public class ListViewForScrollView extends ListView {

 public ListViewForScrollView(Context context) {

  super(context);

 }

 public ListViewForScrollView(Context context, AttributeSet attrs) {

  super(context, attrs);

 }

 public ListViewForScrollView(Context context, AttributeSet attrs,

   int defStyle) {

  super(context, attrs, defStyle);

 }

 @Override

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,

    MeasureSpec.AT_MOST);

  super.onMeasure(widthMeasureSpec, expandSpec);

 }

}

計(jì)算listview 的高度

/**

  * 獲取Listview的高度,然后設(shè)置ViewPager的高度

  *

  * @param listView

  * @return

  */

 public static int setListViewHeightBasedOnChildren1(ListView listView) {

  // 獲取ListView對應(yīng)的Adapter

  ListAdapter listAdapter = listView.getAdapter();

  if (listAdapter == null) {

   // pre-condition

   return 0;

  }

  int totalHeight = 0;

  for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回?cái)?shù)據(jù)項(xiàng)的數(shù)目

   View listItem = listAdapter.getView(i, null, listView);

   listItem.measure(0, 0); // 計(jì)算子項(xiàng)View 的寬高

   totalHeight += listItem.getMeasuredHeight(); // 統(tǒng)計(jì)所有子項(xiàng)的總高度

  }

  ViewGroup.LayoutParams params = listView.getLayoutParams();

  params.height = totalHeight

    + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

  // listView.getDividerHeight()獲取子項(xiàng)間分隔符占用的高度

  // params.height最后得到整個ListView完整顯示需要的高度

  listView.setLayoutParams(params);

  return params.height;

 }

5. scrollView滾動到頂端問題

uiHandler.post(new Runnable() {

       @Override

       public void run() {

        // TODO Auto-generated method stub

        scorllView.scrollTo(0, 0);

       }

      });

基本這5步可以解決問題


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI