美好的一天。我是Android新手。我在RecyclerView
时遇到了一些问题。我想为我的数据图像及其文本制作垂直外观,但它看起来是水平的。我已经尝试在RecyclerView
窗口小部件中将 orientation 写为 orientation ,它是FrameLayout
,位于CardView
。即使我把 android:scrolbars 作为垂直。但是这和他们都没有帮助。
这里是RecyclerView
的水平结果:
生成的图片
fragment_blank.xml文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ganz.afex_with_default_navigation.fragments.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
CardView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/cardview_id"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/tovar_img_id"
android:layout_width="match_parent"
android:layout_height="225px"
android:background="#2d2d2d"/>
<TextView
android:id="@+id/tovar_title_id"
android:textColor="@color/redable"
android:textSize="20sp"
android:paddingLeft="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tovar"/>
<!-- </ImageView>-->
</LinearLayout>
</android.support.v7.widget.CardView>
更新 RecyclerView初始化:
package com.example.ganz.afex_with_default_navigation.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.ganz.afex_with_default_navigation.R;
import com.example.ganz.afex_with_default_navigation.adapters.Recyclerview_Adapter;
import com.example.ganz.afex_with_default_navigation.models.Tovar_tavsiya;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
//Tovar list BEGIN
List<Tovar_tavsiya> lstTovar;
lstTovar = new ArrayList<>();
lstTovar.add(new Tovar_tavsiya("Modadagi kiyimlar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_1));
lstTovar.add(new Tovar_tavsiya("Chet eldan uskunalar", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_2));
lstTovar.add(new Tovar_tavsiya("Tekstil mahsulotlari", "Tovar kategoriyasi", "Description", R.drawable.tile_bg_3));
RecyclerView myrv = (RecyclerView) v.findViewById(R.id.recyclerview_id);
Recyclerview_Adapter myrv_Adapter = new Recyclerview_Adapter(getContext(), lstTovar);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(myrv_Adapter);
return v;
}
}
答案 0 :(得分:1)
使用此
myrv.setLayoutManager(new LinearLayoutManager(getContext()));
而不是
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
答案 1 :(得分:0)
确保您初始化此代码:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
your_recycler_view.setLayoutManager(layoutManager);
默认情况下,方向为 VERTICAL
希望这会有所帮助
答案 2 :(得分:0)
将您的代码更改为
Recyclerview_Adapter myrv_Adapter = new Recyclerview_Adapter(getContext(), lstTovar);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//when you want vertical
layoutManager.setOrientation(context,LinearLayoutManager.VERTICAL,false);
myrv.setLayoutManager(layoutManager);
myrv.setAdapter(myrv_Adapter);
答案 3 :(得分:0)
recyclerView.layoutManager = LinearLayoutManager(上下文, RecyclerView.VERTICAL ,否)
recyclerView.layoutManager = LinearLayoutManager(上下文, RecyclerView.HORIZONTAL ,否)