2016-06-13 10 views
0

私は良い方法で質問を記述しなかったので、質問を明確に編集します。notifyDataSetChanged()レイアウト全体をリフレッシュする

は、私はこれについてとても混乱午前:私はDragViewGroupGridViewのRelativeLayoutを書いた、と私はnotifyDatasetChangedを()を使用するたびに方法はDragViewGroupを引き起こすことに気づいたんDragViewGroupからまで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); 

     } 
    } 

}; 

} 

私はGridViewのを更新するnotifyDatasetChanged()メソッドを使用すると、私のDragViewGroupが元の位置自体もののまでスライドさせますそれは前に引きずられている。どうやらこのメソッドは、グリッドビュー自体を描画するのではなく、レイアウト全体を描画します。

私はDragViewGroupのドラッグ位置を維持する必要があり、私はこのようなDragViewGroupでそのオーバーライドonDrawメソッドを試してみました:

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

それは動作しますが、私はしたくない見えスライディングプロセスがあります。 誰かが私を助けてくれることを願っています。本当にありがとうございます。

+0

ScrollViewは 'notifyDataSetChanged()'には、このような方法がありません。このメソッドは[BaseAdapterクラス](https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged())に属し、通常はListView、GridViews、RecyclerViewsに関連付けられています。 –

+0

ええ、私はそれを知っています。つまり、notifyDataSetChange()を使用してGridViewのみを更新し、同じアクティビティのscrollViewを再描画します。 – toscanininini

+0

このメソッドをアダプタで呼び出すと、GridViewにレイアウト・パスが発生し、すべてのリスト項目を測定してレイアウト内に配置できます。 GridViewのサイズが正確であれば、レイアウトパスはレイアウト階層の上に進むべきではありません。おそらく 'wrap_content'をディメンションの1つとして提供していますか? –

答えて

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>; 
        } 
       } 
} 
}); 

はここにあなたの最後の位置が 「positionOfList」のinstedそして、ちょうどnotifydatasetChanged()呼び出しのGridViewを呼び出した後に入力します。 setSelection(scrollPos)

これがあなたを助けてくれることを願っています!

+0

ありがとう。しかし、私はちょうどなぜスクロールビューが再描画されているのだろうか。私はnotifyDataSetChangedだけlistview/gridview自体をリフレッシュすると思った。 – toscanininini

+0

グリッドビューがScrollView内にある場合は、グリッドビューには独自のスクロールビューがあり、スクロールビューをグリッドビューに配置する必要はありません –

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); 
+0

ありがとうございます。最小APIレベルは11です。setSelectionFromTopメソッドが少なくとも必要ですAPI21。 – toscanininini

関連する問題