Reputation: 47
Good day folk. I am new to Android. I have some issues with RecyclerView
. I want to make appearance vertical for my data images and their texts, but it appears horizontal. I have already tried to write orientation as vertical in RecyclerView
widget, it's FrameLayout
, in CardView
. Even I put android:scrolbars as vertical. But neither this nor that of them helped.
Here the horizontal result of RecyclerView
:
The resulting image
fragment_blank.xml file:
<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>
UPDATE: RecyclerView initialization:
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;
}
}
Upvotes: 2
Views: 8100
Reputation: 186
recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
Upvotes: 1
Reputation: 7669
Use this
myrv.setLayoutManager(new LinearLayoutManager(getContext()));
instead of
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
Upvotes: 2
Reputation: 205
change your code to
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);
Upvotes: 1
Reputation: 3711
make sure you init this code:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
your_recycler_view.setLayoutManager(layoutManager);
in default the orientation is VERTICAL
hope this helps
Upvotes: 2