RecyclerView

Using the RecyclerView Scrolling Widget

Continuing to Improve RecyclerView.Adapter

The adapter’s two methods handle view storage and widget value assignment.

public ViewHolder onCreateViewHolder

public void onBindViewHolderCode language: PHP (php)
package com.foxdevelop.www.recyclerviewtest;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {

    private List<Book> books;

    // Define an inner static class that extends RecyclerView.ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView imageUI;
        TextView textViewUI;

        public  ViewHolder(View view)
        {
            super(view);
            this.imageUI = (ImageView)view.findViewById(R.id.image_view);
            this.textViewUI = (TextView)view.findViewById(R.id.text_view);
        }
    }

    public BookAdapter(List<Book> books)
    {
        this.books = books;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_list,parent,false);
        ViewHolder viewHolder = new ViewHolder(view); // Pass the recycler item view into our ViewHolder
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Book book = books.get(position); // Get the current book
        holder.textViewUI.setText(book.getName()); // Assign values to the two widgets respectively
        holder.imageUI.setImageResource(book.getImageId());
    }

    @Override
    public int getItemCount() {
        return books.size();
    }

}Code language: JavaScript (javascript)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_gravity="center"
        />
</LinearLayout>Code language: HTML, XML (xml)

Horizontal Layout with RecyclerView

Implementing Horizontal Scrolling in RecyclerView Is Very Simple

ListView simply can’t do this.

First, modify the book_list layout.

Modifying the book_list Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>Code language: HTML, XML (xml)

Setting the Orientation of LinearLayoutManager

public class MainActivity extends AppCompatActivity {
    private List<Book> courses = new ArrayList<Book>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         ………………
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
         ……………..
    }Code language: PHP (php)

Staggered Grid Layout with RecyclerView

RecyclerView delegates layout to a LayoutManager, making it very flexible.

Besides horizontal arrangement, RecyclerView also supports:

GridLayoutManager for grid layouts

StaggeredGridLayoutManager for staggered grid (waterfall) layouts.

Setting Up Staggered Grid Layout – Modify book_list View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    >
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="10dp"
        />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="left"
        />
</LinearLayout>Code language: HTML, XML (xml)

Then Set the Staggered Grid Layout on RecyclerView

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LoadBooks();
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.testrecy);
    StaggeredGridLayoutManager sg = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(sg);
    BookAdapter bookAdapter = new BookAdapter(courses); // Pass the data source when initializing the adapter
    recyclerView.setAdapter(bookAdapter);
}Code language: JavaScript (javascript)

Grid Table Layout with RecyclerView

The difference between grid layout and staggered grid is that grid cells have uniform width and height.

In a staggered grid layout, the item heights may vary.

The modification is quite simple.

GridLayoutManager layoutManager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(layoutManager);Code language: JavaScript (javascript)

RecyclerView

Leave a Reply

Your email address will not be published. Required fields are marked *