Viewpager 2 in Android
useful blogs & videos :
https://youtu.be/-wB_JE_PRTo
https://medium.com/google-developer-experts/exploring-the-view-pager-2-86dbce06ff71
https://blog.usejournal.com/early-introduction-of-viewpager2-ff38c60d2169
UnLike RecyclerView , the Single Row Design must have a height of "Match Parent" , else viewpager2 wont work.
ViewPager2 needs an adapter. For this, we can either use RecyclerView.Adapter or FragmentStateAdapter which supports fragment out of the box. We will use FragmentStateAdapter for now.
-------------------------------------------------------------------------------------------------------
Viewpager 2 implements a RecyclerView behind the scenes.
To Attach ViewPager2 & TabLayout , you can use the below code of line to attach them inside the OnCreate( )
viewpager2.setAdapter(new ViewPagerAdapter(Arrays.asList(images)));
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
tabLayout,viewpager2,
(tab, position) -> tab.setText("OBJECT " + (position + 1)) );
tabLayoutMediator.attach();
----------------------------------------------------------------------------------------------------
SIMPLE VIEWPAGER IN JAVA :
(swipe single images)
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.deepesh.viewpager2app;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
ViewPager2 viewpager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager2 = findViewById(R.id.viewpager2);
Integer[] images = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img5
};
viewpager2.setAdapter(new ViewPagerAdapter(Arrays.asList(images)));
viewpager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
}
}
ViewPagerAdapter.java
package com.deepesh.viewpager2app;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.MyViewHolder> {
List<Integer> images;
ViewPagerAdapter(List<Integer> images) {
this.images = images;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_page, parent, false);
return new MyViewHolder(view1);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Integer currentImageId = images.get(position);
holder.imageView.setImageResource(currentImageId);
}
@Override
public int getItemCount() {
return images.size();
}
//VIEWHOLDER CLASS
static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.single_page_image);
}
}
}
single_page.xml
<?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="match_parent">
// In Case of ViewPager , the Layout_Height cannot be "Wrap Content".
<ImageView
android:id="@+id/single_page_image"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="55dp"
android:layout_marginTop="42dp"
android:src="@drawable/ic_launcher_background"
android:layout_marginEnd="55dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
----------------------------------------------------------------------------------------------------------

Comments
Post a Comment