RecyclerView从右到左成长元素

时间:2015-07-30 16:06:42

标签: android android-recyclerview right-to-left

我在水平方向使用RecyclerView,而New元素从左到右。和滚动是ltr。如何改变这个方向?

enter image description here

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扩展了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);
    }


}

9 个答案:

答案 0 :(得分:47)

非常简单,只需为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();
}

答案 1 :(得分:7)

了解如何操作,您只需设置

即可

linearLayoutManager.setStackFromEnd(真);

  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                linearLayoutManager.setStackFromEnd(true);

答案 2 :(得分:6)

请使用:

android:layoutDirection="rtl"

全部:)

答案 3 :(得分:4)

您可以直接从XML添加它  添加app:reverseLayout属性:

<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" />

答案 4 :(得分:2)

这很容易!

<强> setReverseLayout(真)

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

答案 5 :(得分:1)

对于在recyclerview中从右到左获取元素的反向布局为true,如下面的代码所示:

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

答案 6 :(得分:1)

最好的方法是将layoutDirection用于rtl

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

答案 7 :(得分:1)

另一种解决方案:

如果您想拥有一个水平的回收站视图,该视图可以始终从右向左滚动(无需注意智能手机语言),则可以简单地扩展LinearLayoutManager类并覆盖其{ {1}}方法。

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); 类执行此方案。

答案 8 :(得分:1)

简单的解决方案。

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

最后一个标志应该为假。