私は水平方向にRecyclerViewを使用し、新しい要素は左から右です。スクロールはltrです。この方向を変更するには?
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);
アダプタ:
パブリッククラス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);
}
}
非常にシンプルです。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();
}
それを行う方法を見つけた、あなたがしなければならないすべては設定されています
linearLayoutManager.setStackFromEnd(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
linearLayoutManager.setStackFromEnd(true);
これを使うだけです:
Android:layoutDirection="rtl"
それで全部です :)
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" />
rTLにlayoutDirectionを使用する最良の方法
<Android.support.v7.widget.RecyclerView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layoutDirection="rtl" />
それは簡単です!
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);
別の解決策:
常に右から左にスクロールする(スマートフォンの言語に注意を払わずに)水平リサイクラービューを使用する場合は、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
クラスに対しても実行できます。
以下に示すコードのように、recyclerviewで要素を右から左に取得するには、逆レイアウトをtrueにします。
recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true)); // true is for reverse layout value