Делаем RecyclerView вертикальным - PullRequest
0 голосов
/ 31 мая 2018

Добрый день народ.Я новичок в Android.У меня есть некоторые проблемы с RecyclerView.Я хочу сделать изображение вертикальным для моих изображений данных и их текстов, но оно выглядит горизонтальным.Я уже пытался написать ориентация как по вертикали в RecyclerView виджет, это FrameLayout, в CardView.Даже я ставлю android: полосы прокрутки как вертикальные.Но ни то, ни другое из них не помогло.

Вот горизонтальный результат RecyclerView:

Полученное изображение

The result image

файл фрагмента_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;
    }

}

Ответы [ 3 ]

0 голосов
/ 31 мая 2018

измените свой код на

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);
0 голосов
/ 31 мая 2018

Используйте это

myrv.setLayoutManager(new LinearLayoutManager(getContext()));

вместо

myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
0 голосов
/ 31 мая 2018

убедитесь, что вы инициализируете этот код:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
your_recycler_view.setLayoutManager(layoutManager);

по умолчанию ориентация: ВЕРТИКАЛЬНАЯ

надеюсь, это поможет

...