notifydatasetchanged()没有看到向下滚动效果

时间:2017-01-06 13:38:59

标签: android android-cardview

我尝试做这样的事情: enter image description here

要展开或折叠我的cardView,我调用notifydatasetchanged ()方法,但我执行此方法会将视图重置为第一个元素,并快速滚动到我的旧视图。用户怎么能看不到这个卷轴?为了说明我的问题,这里有一段视频:https://drive.google.com/open?id=0B7LMfg4tJlisVVlXajVuVlIyd3c

我的适配器

公共类CardsViewAdapter扩展了RecyclerView.Adapter {     私人游戏[] mDataset; // private boolean isPopupVisible = false;     int rotationAngle = 0;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ImageView imageView;
    public LinearLayout test2;
    public TextView test3;
    boolean isPopupVisible;

    public ViewHolder(View v) {
        super(v);
        mTextView = (TextView) v.findViewById(R.id.text_cards);
        imageView = (ImageView) v.findViewById(R.id.item_description_game_more);
        test2 = (LinearLayout) v.findViewById(R.id.popup_layout);
        test3 = (TextView) v.findViewById(R.id.test_view);
        isPopupVisible = false;

    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public CardsViewAdapter(Game[] myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cards_resume_game, parent, false);
    // set the view's size, margins, paddings and layout parameters
    //...

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    //TODO : complete
    final int pos = position;
    holder.mTextView.setText(String.valueOf(mDataset[position].getId_game()));
    holder.test3.setText("Position : "+pos);
    // Set the expanded or collapsed mode here
    if(mDataset[position].expanded)
        expandView(holder.test2,holder.imageView);
    else
        collapseView(holder.test2,holder.imageView);


    // Now set the onClickListener like this
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            // Animate the imageView here
            animateImageView(holder.imageView);

            // Toggle the expanded attribute value
            if(mDataset[pos].expanded) mDataset[pos].expanded = false;
            else mDataset[pos].expanded = true;

            // Now call notifyDataSetChanged to make the change to effect
            refreshList();
        }
    });



}

// Extra functions inside your adapter class to improve readability
private void collapseView(View v,ImageView imageView) {
    CardsAnimationHelper.collapse(v);
}


private void expandView(View v,ImageView imageView) {
    CardsAnimationHelper.expand(v);
}

private void animateImageView(ImageView imageView) {
    ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "rotation",rotationAngle, rotationAngle + 180);
    anim.setDuration(animationDuration);
    anim.start();
    rotationAngle += 180;
    rotationAngle = rotationAngle % 360;
}

long animationDuration = 500;

private void refreshList() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    }, animationDuration);
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}

3 个答案:

答案 0 :(得分:1)

使用RecyclerView的notifyItemChanged(int position)方法,然后您的列表完全无法刷新。

要使视觉上更具吸引力,您还可以在布局文件中使用android:animateLayoutChanges="true",这将自动设置布局更改(https://developer.android.com/training/animation/layout.html)。

答案 1 :(得分:0)

notifydatasetchanged()不会滚动它只是更新。使用recyclerview.smoothScrollToPosition(int position)滚动

答案 2 :(得分:0)

您可以使用两种方式滚动。

  1. smoothScrollToPosition(int position)[如果你想实现smoothscroll]
  2. setSelection(int position)
  3. 根据您的要求在post或postDelay中执行此操作。