0

I am animating a RecyclerView and everything is working fine except that the views inside the RecyclerView are not showing properly.

Using common anim XML

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:repeatCount="infinite"
    android:duration="3000"/>

//IS WORKING FINE, MOVES RECYCLERVIEW ACROSS SCREEN !

Code for Recyclerview

Animation animLinear;

//OnCreate
recyclerView = findViewById(R.id.item_list);    
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(new Adapter_Main_Markets());
recyclerView.startAnimation(animLinear);

So, what's happening is the visible view of the recylerview is moving across the screen. I would like some help with moving the views inside the screen across the RecyclerView but I have no idea how to get the views inside the recyclerview properly.

//Adapter Main Markets
@Override
public void onBindViewHolder(@NonNull Adapter_Main_Markets.MyViewHolder holder, int position) {
    holder.mIdView.setText("" + market_list[position]);
    holder.mPriceView.setText("$ " + int_list[position]);
    holder.mChangeView.setText(change_list[position]);

}

Will I need a loop for the views in the adapter? Maybe set animation there? Any help would be appreciated.

This is RecyclerView

Item A Item B Item C Item D Item E.

What actually shows on screen in the visible area is Item A Item B Item C.

So, of course, the visible "Item A Item B Item C" goes across the screen but that is not what I want.

I want Item A to go across the screen then Item B the Item C, then D, then E.

MarGin
  • 2,078
  • 1
  • 17
  • 28
SmulianJulian
  • 801
  • 11
  • 33
  • as this is on UI, why don't you add an animated gif on what you have, and possibly a picture of what you would like to have as well? – Alessio Sep 05 '18 at 03:43

1 Answers1

2

If you want to animate views inside RecyclerView then you need to animate itemview inside ViewHolder constructor like this,

class `Your view holder class` extends RecyclerView.ViewHolder {

    `Constructor of your class`(View view) {
        super(view);
        //This piece of code will animate your itemview inside recyclerview
        new Handler().postDelayed(() -> {
            view.setVisibility(View.VISIBLE);
            view.startAnimation(AnimationUtils.loadAnimation(view.getContext(), android.R.anim.fade_in // Change your anim file here));
        }, 100);
        setIsRecyclable(true);// Just to ensure that everytime view gets invisible, it should be recycled for next time recreation.
    }
}

Next thing is to remove repeat count from your anim.xml

Remove android:repeatCount="infinite" this so that view will animate only once it gets visible.

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • It works and shows the first two views but not the last two views. I also receive this error. I assuming it means the last two views cant be viewed E/View: isRecyclable decremented below 0: unmatched pair of setIsRecyable() calls for ViewHolder{6c37968 position=-1 id=-1, oldPos=-1, pLpos:-1 unbound no parent} – SmulianJulian Sep 05 '18 at 05:36
  • Issue about **E/View: isRecyclable decremented below 0:** is already discussed here [link](https://stackoverflow.com/questions/31065481/isrecyclable-decremented-below-0-unmatched-pair-of-setisrecyable-calls), as you can see there that `setIsRecyclable(true);` is _implicit_ and you can completely remove it if you don't want to handle it manually. – Jeel Vankhede Sep 05 '18 at 06:04
  • No matter what I do. Only 2 views show up though there are 4 there. – SmulianJulian Sep 05 '18 at 06:15