我制作了一个Horizontal RecyclerView,它工作正常(感谢this),但滚动和数据的方向从左向右扩展;那么如何更改RecyclerView滚动方向,如下图所示?
我的代码:
StaggeredGridLayoutManager staggeredGridLayoutManager =
new StaggeredGridLayoutManager(
2, //The number of Columns in the grid
LinearLayoutManager.HORIZONTAL);
答案 0 :(得分:94)
假设您在RecyclerView中使用LinearLayoutManager
,那么您可以在LinearLayoutManager构造函数中将true
作为第三个参数传递。
例如:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
如果您使用的是StaggeredGridLayoutManager
,则可以使用它提供的setReverseLayout
方法。
答案 1 :(得分:25)
你可以只使用xml。
app:reverseLayout =“true”完成工作!
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:orientation="horizontal"
app:reverseLayout="true"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
答案 2 :(得分:3)
带有imageview和textview的水平RecyclerView
xml文件
main.xml中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:background="#070e94">
<View
android:background="#787878"
android:layout_width="match_parent"
android:layout_height="1dp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/wallet"
android:background="#070e94"
android:layout_width="match_parent"
android:layout_height="100dp"/>
item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/bus"
android:layout_gravity="center"/>
<TextView
android:textColor="#000"
android:textSize="12sp"
android:layout_gravity="center"
android:padding="5dp"
android:id="@+id/txtView"
android:textAlignment="center"
android:hint="Electronics"
android:layout_width="80dp"
android:layout_height="wrap_content" />
Java Class
ActivityMaim.java
public class MainActivity extends AppCompatActivity{
private RecyclerView horizontal_recycler_view;
private ArrayList<Arraylist> horizontalList;
private CustomAdapter horizontalAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
horizontalList = new ArrayList<Arraylist>();
for (int i = 0; i < MyData.nameArray.length; i++) {
horizontalList.add(new Arraylist(
MyData.nameArray[i],
MyData.drawableArray[i]
));
}
horizontalAdapter=new CustomAdapter(horizontalList);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
horizontal_recycler_view.setAdapter(horizontalAdapter);
}}
Adaper Class
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList<Arraylist> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
ImageView imageViewIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.txtView);
//this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.image);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (getPosition()==0)
{
Toast.makeText(v.getContext(), " On CLick one", Toast.LENGTH_SHORT).show();
} if (getPosition()==1)
{
Toast.makeText(v.getContext(), " On CLick Two", Toast.LENGTH_SHORT).show();
} if (getPosition()==2)
{
Toast.makeText(v.getContext(), " On CLick Three", Toast.LENGTH_SHORT).show();
} if (getPosition()==3)
{
Toast.makeText(v.getContext(), " On CLick Fore", Toast.LENGTH_SHORT).show();
}
}
});
}
}
public CustomAdapter(ArrayList<Arraylist> data) {
this.dataSet = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
//view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
// TextView textViewVersion = holder.textViewVersion;
ImageView imageView = holder.imageViewIcon;
textViewName.setText(dataSet.get(listPosition).getName());
//textViewVersion.setText(dataSet.get(listPosition).getVersion());
imageView.setImageResource(dataSet.get(listPosition).getImage());
}
@Override
public int getItemCount() {
return dataSet.size();
}}
Arraylist.java
public class Arraylist{
String name;
int image;
public Arraylist(String name, int image) {
this.name = name;
this.image=image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}}
MyData.java
public class MyData {
static String[] nameArray = {"Gas", "Insurance", "Electronics", "Other Services"};
static Integer[] drawableArray = {R.drawable.gas_gas, R.drawable.insurance, R.drawable.electric, R.drawable.services};}
答案 3 :(得分:1)
只需添加两行代码即可将recyclerview的方向设置为水平。因此,在初始化Recyclerview时添加这些行。
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
my_recycler.setLayoutManager(linearLayoutManager);
答案 4 :(得分:1)
使用androidx的XML方法:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/my_recycler_view"
android:orientation="horizontal"
tools:listitem="@layout/my_item"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_height="wrap_content">
答案 5 :(得分:1)
在“回收站布局”管理器中,第二个参数是spanCount跨度计数的增加或减少将改变屏幕上显示的元素数量
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2, //The number of Columns in the grid
,GridLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(mLayoutManager);
答案 6 :(得分:0)
我已经尝试了以上所有答案,它向我展示了相同的垂直回收视图,所以我尝试了另一个例子。
初始化适配器
private Adapter mAdapter;
像这样设置适配器
mAdapter = new Adapter();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recycler_view.setLayoutManager(linearLayoutManager);
recycler_view.setAdapter(mAdapter);
希望这对你也有用 对于完整代码,请参阅this链接
答案 7 :(得分:0)
在片段中尝试此操作
layoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false);
答案 8 :(得分:0)
下面的代码就足够了
RecyclerView recyclerView;
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true);
recyclerView.setLayoutManager(layoutManager);
答案 9 :(得分:0)
这是关于波斯语的问题, 只需旋转您的ListView,GridView或....,然后旋转您的单元格。 您可以在xml android:rotate =“ 360”中完成此操作。
答案 10 :(得分:0)
要更改滑动方向,您可以使用
reverselayout属性= true。
在科特林,
val layoutManager = LinearLayoutManager(this@MainActivity,LinearLayoutManager.HORIZONTAL,true)
recyclerview.layoutManager = layoutManager
在Java中,
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
recyclerview.setLayoutManager(layoutManager);
实际上,它颠倒了布局。
如果如下图所示
1.2..3 .... 10
它将更改为
10.9..8 .... 1
创建卧式RecyclerView的方法很多。
答案 11 :(得分:0)
//in fragment page:
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity(), HORIZONTAL,true));
//this worked for me but before that please import :
implementation 'com.android.support:recyclerview-v7:28.0.0'
答案 12 :(得分:0)
For Horizontal Scroolview in RecyclerView in Kotlin: - We use this Code
recycler_upcoming.layoutManager =
LinearLayoutManager(sContext, LinearLayoutManager.HORIZONTAL, false)
recycler_upcoming.adapter =
InboxUpcomingAdapter(this@InboxMessageFragment, arrayList, sContext)
-> sContext mean Ur Context
-> If you pass false then scroll right to left
-> If You pass true then scroll left to right