Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView Grow Element From Right to Left

i use RecyclerView in horizontal direction and New element is left to right. and scrolling is ltr. how to change this direction?

enter image description here

Xml Code:

 <android.support.v7.widget.RecyclerView                     android:id="@+id/rc3"                     android:layout_gravity="right"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content" /> 

And Java:

    RecyclerView rc1 = (RecyclerView) findViewById(R.id.rc1);     AdapterMainPrice mainPrice = new AdapterMainPrice(StructPrice.getThreePrice());     rc1.setHasFixedSize(false);     LinearLayoutManager llm = new LinearLayoutManager(G.context);     llm.setOrientation(LinearLayoutManager.HORIZONTAL);     rc1.setLayoutManager(llm);     rc1.setAdapter(mainPrice); 

Adapter:

public class AdapterMainPrice extends RecyclerView.Adapter {

private List<StructPrice> prices;  public AdapterMainPrice(List<StructPrice> catList) {     this.prices = catList;  }   @Override public int getItemCount() {     return prices.size(); }   @Override public void onBindViewHolder(NewsViewHolder ghazaViewHolder, int position) {      StructPrice price = prices.get(position);     ghazaViewHolder.vTitle.setText(price.getProductName());     Glide.with(G.context)             .load(price.getProductPic())             .placeholder(R.drawable.loading_spinner)             .crossFade()             .into(ghazaViewHolder.Vimg);     ghazaViewHolder.cardView.startAnimation(ghazaViewHolder.animation); }  @Override public NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {     View itemView = LayoutInflater.             from(viewGroup.getContext()).             inflate(R.layout.adapter_item_main, viewGroup, false);     return new NewsViewHolder(itemView); }  public static class NewsViewHolder extends RecyclerView.ViewHolder {     protected TextView vTitle;     protected ImageView Vimg;     protected Animation animation;     protected CardView cardView;      public NewsViewHolder(View v) {         super(v);         vTitle = (TextView) v.findViewById(R.id.mainRCtv);         Vimg = (ImageView) v.findViewById(R.id.mainRCimg);         animation = AnimationUtils.loadAnimation(G.context, R.anim.fadein);         cardView = (CardView) v.findViewById(R.id.mainRCCard);     }   } 
like image 685
javadroid Avatar asked Jul 30 '15 16:07

javadroid


People also ask

How to use CardView in RecyclerView in Android?

Card Layout: A card Layout is used to display a list of data. It is the design of a single item of our RecyclerView. For creating a Card Layout navigate to the app > res > layout > Right-Click on it > New > Layout Resource File > Give a name to it(here card_layout).

What is LinearLayoutManager Android?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A RecyclerView. LayoutManager implementation which provides similar functionality to android. widget. ListView .


1 Answers

it is pretty simple, just call setReverseLayout(true) for your LayoutManager :

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);         layoutManager.setReverseLayout(true); 

it is explained in its documentation :

 /**  * Used to reverse item traversal and layout order.  * This behaves similar to the layout change for RTL views. When set to true, first item is  * laid out at the end of the UI, second item is laid out before it etc.  *  * For horizontal layouts, it depends on the layout direction.  * When set to true, If {@link android.support.v7.widget.RecyclerView} is LTR, than it will  * layout from RTL, if {@link android.support.v7.widget.RecyclerView}} is RTL, it will layout  * from LTR.  *  * If you are looking for the exact same behavior of  * {@link android.widget.AbsListView#setStackFromBottom(boolean)}, use  * {@link #setStackFromEnd(boolean)}  */ public void setReverseLayout(boolean reverseLayout) {     assertNotInLayoutOrScroll(null);     if (reverseLayout == mReverseLayout) {         return;     }     mReverseLayout = reverseLayout;     requestLayout(); } 
like image 186
Ashkan Ghodrat Avatar answered Sep 19 '22 06:09

Ashkan Ghodrat