RecyclerView 从右向左增长元素

30

我在水平方向上使用了RecyclerView,新元素从左到右排列,滚动也是从左往右的。如何改变这个方向?

图片描述

Xml代码:

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

而 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);

适配器:

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);
    }


}

1
可能是如何使用GridLayoutManager从右到左填充RecyclerView的重复问题。 - Armin Ghoreishi
9个回答

66

很简单,只需为您的LayoutManager调用setReverseLayout(true):

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

它在文档中有解释:

 /**
 * 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();
}

16

只需使用此代码:

android:layoutDirection="rtl"

就这些 :)


属性layoutDirection仅在API级别17及更高版本中使用! - SadeQ digitALLife

8
发现如何实现这个功能,你所需要做的就是设置: linearLayoutManager.setStackFromEnd(true);
  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                linearLayoutManager.setStackFromEnd(true);

6
您可以通过添加 app:reverseLayout 属性直接从 XML 中添加它:
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    app:layout_constraintEnd_toStartOf="@+id/imageButton2"
    app:layout_constraintTop_toTopOf="@+id/imageButton2"
    app:reverseLayout="true"
    tools:listitem="@layout/images_new_post_item" />

5

如何最好地使用layoutDirection实现从右到左的布局

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl" />

2
它是在API Level 17(即4.2)中添加的,因此旧版本不支持它,顺便提一下,它可以在API level 17上运行。 - javadroid

3

将反向布局设置为true,以便在recyclerview中从右到左获取元素,如下所示的代码:

 recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true));  // true is for reverse layout value

3

另一种解决方案:

如果您想要一个水平的RecyclerView,这个RecyclerView总是从右到左滚动(不用考虑智能手机的语言设置),您可以简单地扩展LinearLayoutManager类并覆盖其isLayoutRTL()方法。

import androidx.recyclerview.widget.LinearLayoutManager;

public class RtlLinearLayoutManager extends LinearLayoutManager {

    public RtlLinearLayoutManager(Context context) {
        super(context);
    }

    public RtlLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public RtlLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected boolean isLayoutRTL() {
        return true;
    }
}

//...
RtlLinearLayoutManager layoutManager = new RtlLinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);



注意:
你也可以对 GridLayoutManager 类执行此场景。

2

It's easy!

setReverseLayout(true)

RecyclerAdapterHoTarakoneshha recyclerAdapterHoTarakoneshha = new RecyclerAdapterHoTarakoneshha(mContext, arrayList);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
    linearLayoutManager.setReverseLayout(true);
    recyclerTarakonesh.setLayoutManager(linearLayoutManager);
    recyclerTarakonesh.setHasFixedSize(true);
    recyclerTarakonesh.addItemDecoration(new HorizntalSpaceItemDecoration(mContext, 10));
    recyclerTarakonesh.setAdapter(recyclerAdapterHoTarakoneshha);

1
简单的解决方案。
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

最后一个标志应该为false。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接