每当父级recyclerview调用ondatasetchanged()时,我都很难为嵌套的recyclerview禁用动画。因此,在某些情况下,一个recyclerview持有类似火种的交换卡,并且在每个卡内都有一个嵌套的recyclerview,以滚动浏览用户图像。每次滑动后,都会调用ondatasetchanged并在嵌套的recyclerview中引起严重的闪烁。我已经尝试从父级和子级recyclerviews禁用动画,并且在这一点上我几乎可以想到的任何东西都不能使用。下面的代码是我的每张卡的视图持有人。
public class CardStackHolder extends RecyclerView.ViewHolder implements OnItemSwipePercentageListener {
ImageButton profileInfoBtn;
RecyclerView images_recyclerView;
PagerSnapHelper snapHelper;
ProfileImagesAdapter imagesAdapter;
String profileName;
TextView nameBox;
TextView ageBox;
TextView workBox;
List<String> list;
public CardStackHolder(final View itemView) {
super(itemView);
profileInfoBtn = (ImageButton) itemView.findViewById(R.id.ID_datingRecyclerView_profileInfoBtn);
profileInfoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show profile info dialog
User_Info_Dialog dialog = User_Info_Dialog.getDialog(mHostId, mCurrentProfile.getGender(), mCurrentProfile.getOrientation(), mCurrentProfile.getInterestedIn(), mCurrentProfile.getIntro(), mCurrentProfile.getStatus());
dialog.show(getChildFragmentManager(), "");
}
});
nameBox = (TextView) itemView.findViewById(R.id.ID_datingRecyclerView_profile_name);
ageBox = (TextView) itemView.findViewById(R.id.ID_datingRecyclerView_profile_age);
images_recyclerView = (RecyclerView) itemView.findViewById(R.id.ID_datingRecyclerView_profileImagesRecyclerView);
RecyclerView.ItemAnimator animator = images_recyclerView.getItemAnimator();
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
animator.setChangeDuration(0);
animator.setRemoveDuration(0);
images_recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(images_recyclerView);
workBox = (TextView) itemView.findViewById(R.id.ID_datingRecyclerView_profile_work);
}
public void onBind(Context context, User_Profile profile) {
getImages(profile.getProfileId());
profileName = profile.getFirst_name();
profileName = profileName.substring(0, 1).toUpperCase() + profileName.substring(1);
nameBox.setText(profileName);
ageBox.setText(profile.getAge());
String workStr = profile.getWork();
workStr = workStr.substring(0, 1).toUpperCase() + workStr.substring(1);
workBox.setText(workStr);
mCurrentProfile = profile;
}
public void getImages(String id) {
mFirebaseDatabase.getReference().child("profile_pictures").child(id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
list = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.hasChild("dating_card_imagePath")) {
list.add(snapshot.child("dating_card_imagePath").getValue().toString());
}
}
imagesAdapter = new ProfileImagesAdapter(list);
imagesAdapter.setHasStableIds(true);
images_recyclerView.setAdapter(imagesAdapter);
} else {
// no images found for profile
imagesAdapter = new ProfileImagesAdapter();
imagesAdapter.setHasStableIds(true);
images_recyclerView.setAdapter(imagesAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onItemSwipePercentage(double percentage) {
//log percentage moved
//Log.d("Enchanted", Double.toString(percentage));
if (percentage > -0.20 && percentage < 0.20) {
swipeResultImageView.setVisibility(View.GONE);
} else if (percentage < 0.20) {
swipeResultImageView.setImageResource(R.drawable.ic_swipe_no_like);
swipeResultImageView.setVisibility(View.VISIBLE);
} else if (percentage > -0.20) {
swipeResultImageView.setImageResource(R.drawable.ic_swipe_like);
swipeResultImageView.setVisibility(View.VISIBLE);
}
}
}
下面的代码是我的适配器
//CardStack Adapter
class CardStackAdapter extends RecyclerView.Adapter<CardStackHolder> {
LayoutInflater mInflater;
List<User_Profile> profileList;
RecyclerView.RecycledViewPool ImagePool;
public CardStackAdapter(Context context, List<User_Profile> list) {
mInflater = LayoutInflater.from(context);
profileList = new ArrayList<>(list);
ImagePool = new RecyclerView.RecycledViewPool();
}
@Override
public CardStackHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item_dating_profile_card, parent, false);
return new CardStackHolder(view);
}
@Override
public void onBindViewHolder(final CardStackHolder holder, int position) {
holder.images_recyclerView.setRecycledViewPool(ImagePool);
profileList.get(position).setItemId(holder.getItemId());
holder.onBind(mContext, profileList.get(position));
}
public void removeTopItem() {
profileList.remove(0);
notifyDataSetChanged();
}
public void addItemToTop() {
profileList.add(0, mPreviousProfile);
notifyItemInserted(0);
}
public void updateProfileList(List<User_Profile> p) {
ImagePool.clear();
profileList.clear();
profileList.addAll(p);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return profileList.size();
}
@Override
public long getItemId(int position) {
return profileList.get(position).getItemId();
}
}