notifyDataSetChanged()刷新整个布局

时间:2016-06-13 12:15:26

标签: android gridview notifydatasetchanged

我没有很好地描述这个问题,所以我编辑问题要清楚。

我对此非常困惑:我用 DragViewGroup gridview 写了 RelativeLayout ,我注意到每当我使用 notifyDatasetChanged()方法会导致 DragViewGroup 执行重绘事务。 DragViewGroup LinearLayout 扩展而来 ViewDragHelper ,因此我无法保持已滚动到的位置。

这是我的xml文件:

<?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"
            android:orientation="vertical">
<com.xxxx.DragViewGroup
    android:id="@+id/dragview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3f83f7"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="665dp"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="310dp"
            android:id="@+id/mainview"
            >

            ...

        </RelativeLayout>

        <include
            android:layout_width="match_parent"
            android:layout_height="355dp"
            android:layout_marginBottom="62dp"
            layout="@layout/fake_panel" />

    </LinearLayout>
</com.xxxx.DragViewGroup>
<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/layout"
    android:layout_alignParentBottom="true"
    android:layout_height="210dp"
    android:background="#ffffff"
    android:clickable="true"
    >

    <HorizontalScrollView
        android:id="@+id/btn_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <GridView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:listSelector="@color/transparent"
                android:horizontalSpacing="6dp"
                android:scrollbars="none" />
        </LinearLayout>
    </HorizontalScrollView>

    <com.xxxx.UniversalSeekBar
        ....
        android:layout_marginTop="18dp"/>
</RelativeLayout>

</RelativeLayout>

DragViewGroup代码:

public class DragViewGroup extends LinearLayout {
     private ViewDragHelper viewDragHelper;
     private View mainView;
     private int mainHeight;



public DragViewGroup(Context context) {
    super(context);
    initView();
}

public DragViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
}

public DragViewGroup(Context context, AttributeSet attrs, int defStyleAttr)         {
    super(context, attrs, defStyleAttr);
    initView();
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    mainView = getChildAt(0);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mainHeight = mainView.findViewById(R.id.mainview).getMeasuredHeight();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return viewDragHelper.shouldInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    viewDragHelper.processTouchEvent(event);
    return true;
}

@Override
public void computeScroll() {
    if (viewDragHelper.continueSettling(true)) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

private void initView() {
    viewDragHelper = ViewDragHelper.create(this, callback);
}

private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
    @Override
    public boolean tryCaptureView(View child, int pointerId) {
        return mainView == child;
    }

    @Override
    public int clampViewPositionHorizontal(View child, int left, int dx) {
        return 0;
    }

    @Override
    public int clampViewPositionVertical(View child, int top, int dy) {
        return top;
    }

    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
        super.onViewReleased(releasedChild, xvel, yvel);
        if (mainView.getTop() > -500) {
            viewDragHelper.smoothSlideViewTo(mainView, 0, 0);
            ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);

        } else {
            viewDragHelper.smoothSlideViewTo(mainView, 0, -mainHeight);
            ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);

        }
    }

};

}

一旦我使用notifyDatasetChanged()方法更新gridview,我的DragViewGroup将滑动到原始位置,尽管之前已被拖动。显然,该方法绘制整个布局而不是仅绘制gridview本身。

我需要维护DragViewGroup的拖动位置,我试过在DragViewGroup中重写onDraw方法,如下所示:

@Override
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   // set isDragged label in onViewReleased()   
   if (isDragged) {
      viewDragHelper.smoothSlideViewTo(main, 0, -mainHeight);
   }
}

它有效,但有一个我不想要的可见滑动过程。 希望有人可以帮助我。非常感谢。

2 个答案:

答案 0 :(得分:0)

在滚动视图时存储ScrollValue,只需在gridview上使用setSelectionMethod

gridview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
            int count = gridview.getCount();
                if (scrollState == SCROLL_STATE_IDLE) {
                    if (gridview.getLastVisiblePosition() >= count
                            - 1) {
                        scrollPos = <positionOfList>;
                    }
                }
}
});

这里输入你的最后一个位置&#34; positionOfList&#34; 在调用notifydatasetChanged()之后调用gridview.setSelection(scrollPos)

希望这会对你有帮助!

答案 1 :(得分:0)

只需在您更新显示的类中使用以下代码:

 int index = myList.getFirstVisiblePosition();
    View v = myList.getChildAt(0);
    int top = (v == null) ? 0 : v.getTop();

    myList.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    myList.setSelectionFromTop(index, top);