gpt4 book ai didi

android - RecyclerView 从右到左增长元素

转载 作者:IT老高 更新时间:2023-10-28 23:16:44 29 4
gpt4 key购买 nike

我在水平方向使用 RecyclerView,新元素从左到右。滚动是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)类 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();
}

关于android - RecyclerView 从右到左增长元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728837/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com