Android RecyclerView从右向左生长元素
我在水平方向使用RecyclerView,新元素从左到右。滚动是ltr。如何改变这个方向 Xml代码:Android RecyclerView从右向左生长元素,android,android-recyclerview,right-to-left,Android,Android Recyclerview,Right To Left,我在水平方向使用RecyclerView,新元素从左到右。滚动是ltr。如何改变这个方向 Xml代码: <android.support.v7.widget.RecyclerView android:id="@+id/rc3" android:layout_gravity="right" android:layout_width="fill_parent"
<android.support.v7.widget.RecyclerView
android:id="@+id/rc3"
android:layout_gravity="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
适配器:
公共类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);
}
}
私人标价;
公共适配器价格(列表catList){
这个价格=catList;
}
@凌驾
public int getItemCount(){
返回价格。大小();
}
@凌驾
BindViewHolder上的公共无效(新闻视图持有人ghazaViewHolder,内部位置){
StructPrice=prices.get(位置);
ghazaViewHolder.vTitle.setText(price.getProductName());
带(G.context)的滑动
.load(price.getProductPic())
.placeholder(R.drawable.loading_微调器)
.crossFade()
.into(ghazaViewHolder.Vimg);
ghazaViewHolder.cardView.startAnimation(ghazaViewHolder.animation);
}
@凌驾
公共新闻视图持有者onCreateViewHolder(视图组视图组,int i){
视图项视图=布局调整器。
来自(viewGroup.getContext())。
充气(R.layout.adapter\u item\u main,viewGroup,false);
返回新的NewsViewHolder(itemView);
}
公共静态类NewsViewHolder扩展了RecyclerView.ViewHolder{
受保护的文本视图vTitle;
保护图像视图Vimg;
受保护的动画;
受保护的CardView CardView;
公共新闻视图持有者(视图v){
超级(五);
vTitle=(TextView)v.findViewById(R.id.mainRCtv);
Vimg=(图像视图)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)
您可以直接从XML添加它 通过添加
app:reverseLayout
属性:
这很简单
setReverseLayout(真)
只要用这个:
android:layoutDirection="rtl"
仅此而已:)实现反向布局,以便在recyclerview中从右向左获取元素,如下所示代码:
recycler_view.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true)); // true is for reverse layout value
使用layoutDirection到rtl的最佳方法
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl" />
另一种解决方案:
如果您想拥有一个始终从右向左滚动的水平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
类执行此方案。简单解决方案
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
最后一个标志应为false。API级别17(4.2)中添加了可能的重复标志,因此旧版本不支持它,顺便说一下,它在API级别17上的工作
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl" />
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);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);