Thursday, September 26, 2019

How to custom ViewPager Adapter in Android

Hello everyone today I will show you about custom viewpager adapter in android in the simplest way, now there are many applications that require you to use android viewpager to be able to turn the next pages, to perform. processed viewpager adapter in android, you can perform the steps below.
How to custom ViewPager Adapter in Android

Step 1. Create a layout containing viewpager photo_acitivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:background="@color/deep_purple">
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 
</RelativeLayout>
Step 2. Create the layout item adapter of the viewPager adapter is item_adapter_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/img_item"
        android:layout_width="match_parent"
        android:background="@drawable/nen"
        android:scaleType="fitXY"
        android:contentDescription="@string/txt_setting"
        android:layout_height="match_parent" />
</RelativeLayout>
Step 3. Create an Adapter viewpager class in Android with name : CustomViewPagerAdapter.class

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.anhttvn.a3droselivewallpaper.R;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
 * adapter view pager
 */
public class AdapterViewPager extends PagerAdapter implements View.OnClickListener {
    private Context mContext;
    private ArrayList<String> listPhoto;
    private Onclick mOnclick;
    public AdapterViewPager(Context context, ArrayList<String> list,Onclick click){
        mContext =context;
        listPhoto = list;
        mOnclick = click;
    }
    @Override
    public int getCount() {
        return listPhoto.size();
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == ((RelativeLayout) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_photo_adapter, container, false);
        ImageView imageView = itemView.findViewById(R.id.img_item);
        InputStream inputstream= null;
        try {
            inputstream = mContext.getAssets().open("image/"
                    +listPhoto.get(position));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Drawable drawable = Drawable.createFromStream(inputstream, null);
        imageView.setImageDrawable(drawable);
        container.addView(itemView);

        imageView.setOnClickListener(this);
        imageView.setTag(position);
        return itemView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout) object);
    }
    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag() + "");
        switch (view.getId()){
            case R.id.img_item:
                mOnclick.clickSelect(position);
                break;
        }
    }
    public interface Onclick{
        void clickSelect(int position);
    }
}
Step 4. Implement custom viewpager adapter in MainActivity.class
public class ActivityPhoto extends BaseActivity implements AdapterViewPager.Onclick {
    private AdapterViewPager adapterViewPager;
    private ViewPager mViewPager;
    private AdView mAds;
    private String [] images;
    private ArrayList<String> listImages;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_activity);
        init();
        getData();
        AdapterView();
    }
    private void init(){
        mViewPager = findViewById(R.id.viewPager);
        mAds = findViewById(R.id.ads_view_3);
        runAdview(mAds);
    }
    private void getData(){
        try {
            images =getAssets().list("image");
        } catch (IOException e) {
            e.printStackTrace();
        }
        listImages = new ArrayList<String>(Arrays.asList(images));
    }
    private void AdapterView(){
        if(listImages != null){
            adapterViewPager = new AdapterViewPager(this,listImages,this);
            mViewPager.setAdapter(adapterViewPager);
            mViewPager.setCurrentItem(1);
        }
    }
    @Override
    public void clickSelect(int position) {
    }
}

No comments:

Post a Comment