-1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/titleImageViewID"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/todo"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/mac" />

        <View
            android:id="@+id/view8"
            android:layout_width="0dp"
            android:layout_height="86dp"
            android:layout_marginTop="168dp"
            android:background="#8A474141"
            app:layout_constraintBottom_toBottomOf="@id/titleImageViewID"
            app:layout_constraintEnd_toEndOf="@+id/titleImageViewID"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/titleImageViewID"
            app:layout_constraintTop_toTopOf="@+id/titleImageViewID" />

        <TextView
            android:id="@+id/titleTextViewID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:text="@string/textview"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/view8"
            app:layout_constraintEnd_toEndOf="@+id/view8"
            app:layout_constraintStart_toStartOf="@+id/view8"
            app:layout_constraintTop_toBottomOf="@+id/titleImageViewID"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

Above code is sample_layout of a RecyclerView - please suggest for only ConstraintLayout, I use this layout because this is responsive to any device, so I don't need to create another layout for different screen sizes

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private RecycleAdapter recycleAdapter;
    private RecyclerView.LayoutManager manager;
    private ConstraintLayout constraintLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycleViewID);
        manager = new ConstraintLayout(this,ConstraintLayout,View.SCROLL_AXIS_HORIZONTAL,false);

       RecycleModel [] myThumbData = new RecycleModel[]{
       new RecycleModel("Goru banaisi", R.drawable.mac)};}

Can't figure out the main java code how do I access all of my XML in java code, anyone give me a proper code and explanation, I couldn't find any recycler view using constraint layout online. So problem solver help me to solve my problem**

Boken
  • 4,825
  • 10
  • 32
  • 42
  • Does this answer your question? [How to build a Horizontal ListView with RecyclerView?](https://stackoverflow.com/questions/28460300/how-to-build-a-horizontal-listview-with-recyclerview) – Ryan M Apr 20 '20 at 02:40

2 Answers2

1

Set your recyclerview manager as shown below:

manager = new LinearLayoutManager(this, RecyclerView.Horizontal, false)
reyclerview.manager = manager
//Then set the adapter
recyclerview.adapter = your adapter.

This should enable horizontal scroll for your recyclerview.

Hope this helps

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
1

LayoutManager

You have to set proper LayoutManager. There are few basic implementations e.g. LinearLayoutManager, GridLayoutManager or StaggeredGridLayoutManager.

In Android Documentation you can find information that:

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

Set from Code

You can set them from code:

layoutManager = new LinearLayoutManager(
        this,
        LinearLayoutManager.HORIZONTAL,   // Here, you have orientation of items
        false
);

// Remember to set your LayoutManager in recycler view, not in adapter!
recyclerView.setLayoutManager(layoutManager);

Set from XML

In your XML layout file, where you have your adapter, you have to set:

  • orientation and
  • layoutManager.
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

Recycler View

Community
  • 1
  • 1
Boken
  • 4,825
  • 10
  • 32
  • 42