notifydatasetchanged() 看不到向下滚动效果
notifydatasetchanged() don't see scroll down effect
我试着做这样的事情:
为了展开或折叠我的 cardView
我调用了 notifydatasetchanged ()
方法,但我执行此方法会将视图重置为第一个元素并快速滚动到我的旧视图。用户怎么会看不到这个卷轴呢?为了说明我的问题,这里有一个视频:https://drive.google.com/open?id=0B7LMfg4tJlisVVlXajVuVlIyd3c
我的适配器
public class 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;
}
notifydatasetchanged()
不滚动它只是更新。使用 recyclerview.smoothScrollToPosition(int position)
滚动
您可以使用两种滚动方式。
- smoothScrollToPosition(int position)[如果要实现smoothscroll]
- setSelection(整数位置)
根据您的要求在 post 或 post 延迟执行此操作。
使用 RecyclerView 的 notifyItemChanged(int position)
方法,您的列表将不会完全刷新。
为了在视觉上更具吸引力,您还可以在布局文件中使用 android:animateLayoutChanges="true"
,这将自动为布局更改设置动画 (https://developer.android.com/training/animation/layout.html)。
我试着做这样的事情:
为了展开或折叠我的 cardView
我调用了 notifydatasetchanged ()
方法,但我执行此方法会将视图重置为第一个元素并快速滚动到我的旧视图。用户怎么会看不到这个卷轴呢?为了说明我的问题,这里有一个视频:https://drive.google.com/open?id=0B7LMfg4tJlisVVlXajVuVlIyd3c
我的适配器
public class 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;
}
notifydatasetchanged()
不滚动它只是更新。使用 recyclerview.smoothScrollToPosition(int position)
滚动
您可以使用两种滚动方式。
- smoothScrollToPosition(int position)[如果要实现smoothscroll]
- setSelection(整数位置)
根据您的要求在 post 或 post 延迟执行此操作。
使用 RecyclerView 的 notifyItemChanged(int position)
方法,您的列表将不会完全刷新。
为了在视觉上更具吸引力,您还可以在布局文件中使用 android:animateLayoutChanges="true"
,这将自动为布局更改设置动画 (https://developer.android.com/training/animation/layout.html)。