Ardi Sugiarto
Ardi Sugiarto

Reputation: 95

How to add horizontal scroll on vertical recyclerview?

So i have a recyclerview like this :

enter image description here

as you can see, the screen width is not enough for displaying all the text nicely, so i need to add horizontal scroll so user can scroll horizontally and vertically at the same time, how do i do this?

Upvotes: 4

Views: 4336

Answers (6)

Rediska
Rediska

Reputation: 1470

Use this layout manager to allow horizontal scrolling of long items in a vertical LinearLayoutManager.

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class FullScrollLayoutManager extends LinearLayoutManager {

    private int offset;
    private int maxOffset;

    public FullScrollLayoutManager(Context context) {
        super(context);
    }

    @Override
    public void onLayoutCompleted(RecyclerView.State state) {
        super.onLayoutCompleted(state);
        updateLayout();
    }

    private void updateLayout() {
        int n = getChildCount();
        maxOffset = 0;
        for(int i=0; i<n; ++i) {
            View view = getChildAt(i);
            updateMaxOffset(view);
        }
        if(offset>maxOffset) offset = maxOffset;
        offsetChildren();
    }

    private void updateMaxOffset(View view) {
        int x = view.getRight();
        int ownWidth = getWidth();
        if(x>ownWidth) maxOffset = Math.max(maxOffset,x-ownWidth);
    }

    @Override
    public boolean canScrollHorizontally() {
        return true;
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        if(dx<0) {
            if(-dx>offset) dx = -offset;
        }
        else
        if(dx>0) {
            if(dx+offset>maxOffset) dx = maxOffset-offset;
        }
        offset += dx;
        offsetChildren();
        return dx;
    }

    private void offsetChildren() {
        int n = getChildCount();
        for(int i=0; i<n; ++i) {
            View view = getChildAt(i);
            view.setTranslationX(-offset);
        }
    }

    public int getOffset() {
        return offset;
    }

    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
        child.setTranslationX(-offset);
        child.post(()->updateMaxOffset(child));
    }
}

Upvotes: 2

Arahim
Arahim

Reputation: 303

Try this

     val linearLayoutManager = LinearLayoutManager(this)
                linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
                selected_recycler_view.layoutManager = linearLayoutManager

adapter = TAdapter(this, existing.selectedArrayList)

Upvotes: 0

Ali Zaidi
Ali Zaidi

Reputation: 317

Put vertical RecyclerView inside HorizontalScrollView like below.

<HorizontalScrollView
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tasks_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

</HorizontalScrollView>

Also the root element of the item view for the RecyclerView has to have width wrap_content not match_parent, by setting it's width to wrap_content it allows it to expand the width of its parent which in this case would be the RecyclerView.

Now in the item view either give wrap_content or fixed widths to the inner views which in your case are the views to display "No", "Wonum", "Item num", "Quantity" and "UOM". By giving inner views fixed or wrap_content option they will expand automatically or according to the given width hence expanding their parent.

Upvotes: 1

karan
karan

Reputation: 8853

Use HorizontalScrollView with LinearLayout as child. Set it's Orientation to horizontal and add your dynamic views to it.

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

    <LinearLayout
            android:id="@+id/ll_main"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
            // Add your dynamic views to this layout.
    </LinearLayout>

</HorizontalScrollView>

Upvotes: 2

Pankaj Sati
Pankaj Sati

Reputation: 2529

  1. You can add android:scrollHorizontally="true" in your TextView
  2. If there are more than one view in your list item, you can use HorizontalScrollView

Upvotes: 0

milad salimi
milad salimi

Reputation: 1660

You can use this in your row layout :

<HorizontalScrollView 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:android="http://schemas.android.com/apk/res/android">

     //Your layout item here

</HorizontalScrollView>

Upvotes: 1

Related Questions