Listview不会滚动到最后一个元素

时间:2014-01-18 08:10:02

标签: android android-listview scroll

我有一个应用程序,我通过扩展ListView创建自定义列表视图。问题是它在某些avd中工作但在其他一些avd上我无法滚动到listview中的最后一个元素。谁能告诉我为什么会这样?

这是扩展listview的代码:

public class CustomListView extends ListView {
   private float touchSlop = 0;
   private float downY = 0;
   private boolean consumeTouchEvents = false;

    public CustomListView(Context context) {
        super(context);
        init();
    }

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        boolean isHandled = true;
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_MOVE:
                float distance = downY - ev.getY();
                if (!consumeTouchEvents && Math.abs(distance) > touchSlop) {

                    //send CANCEL event to the AbsListView so it doesn't scroll
                    ev.setAction(MotionEvent.ACTION_CANCEL);
                    isHandled = super.onTouchEvent(ev);
                    consumeTouchEvents = true;
                    handleScroll(distance);
                }
                break;
            case MotionEvent.ACTION_DOWN:
                consumeTouchEvents = false;
                downY = ev.getY();
                //fallthrough
            default:
                if (!consumeTouchEvents) {
                    isHandled = super.onTouchEvent(ev);
                }
                break;

        }
        return isHandled;
    }

    private void handleScroll(float distance) {
        if (distance > 0) {
            //scroll up
            if (getFirstVisiblePosition() < getAdapter().getCount() - 1) {
                smoothScrollToPositionFromTop(getFirstVisiblePosition() + 1, 0, 300);
            }
        } else {
            //scroll down
            if (getFirstVisiblePosition() > 0) {
                smoothScrollToPositionFromTop(getFirstVisiblePosition() - 1, 0, 300);
            }
        }
    }
    }

这是包含自定义列表视图的布局文件:

            <android.support.v4.widget.DrawerLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

       <!-- Framelayout to display Fragments -->
       <FrameLayout
       android:id="@+id/frame_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >



    <LinearLayout
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 

     >



     <com.example.CustomListView 
     android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    ></com.example..CustomListView>



       </LinearLayout>



       </FrameLayout>

      <!-- Listview to display slider menu -->
       <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
   android:background="#fff"
    android:dividerHeight="1dp"       
    />
     </android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案