Skip to content Skip to sidebar Skip to footer

Correctly Implementing Pageradapter In Android

I have problems with implementation of my custom PagerAdapter and using it with a ViewPager. This sample PagerAdapter has 10 items, every item is a button with it's index as text.

Solution 1:

Here is complete code:

xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

MyPagerAdapter class:

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

publicclassMyPagerAdapterextendsPagerAdapter {

    @Overridepublic int getCount() {
        return10;
    }

    @OverridepublicbooleanisViewFromObject(View view, Object o) {
        return o==view;
    }

    @OverridepublicObjectinstantiateItem(final ViewGroup container, int position) {
        Button button = newButton(container.getContext());
        ViewGroup.LayoutParams params = newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setText(String.valueOf(position));

        final int page = position;
        button.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View v) {
                Toast.makeText(container.getContext(), "You clicked: " + page + ". page.", Toast.LENGTH_SHORT).show();
            }
        });

        container.addView(button);
        return button;
    }

    @OverridepublicvoiddestroyItem(ViewGroup container, int position, Objectobject) {
        container.removeView((Button)object);
    }
}

MainActivity:

import android.support.v4.view.ViewPager;
import android.app.Activity;
import android.os.Bundle;

publicclassMainActivityextendsActivity {
    ViewPager viewPager;
    MyPagerAdapter myPagerAdapter;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager)findViewById(R.id.pager);
        myPagerAdapter = newMyPagerAdapter();
        viewPager.setAdapter(myPagerAdapter);
    }
}

You will see that Buttons are full screen. To avoid that you need to create some layout (like LinearLayout) and add button to that layout.

Example:

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

publicclassMyPagerAdapterextendsPagerAdapter {

    @OverridepublicintgetCount() {
        return10;
    }

    @OverridepublicbooleanisViewFromObject(View view, Object o) {
        return o==view;
    }

    @Overridepublic Object instantiateItem(final ViewGroup container, int position) {
        Buttonbutton=newButton(container.getContext());
        ViewGroup.LayoutParamsparams=newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setText(String.valueOf(position));

        LinearLayoutlayout=newLinearLayout(container.getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParamslayoutParams=newViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        //add buton to layout
        layout.addView(button);

        finalintpage= position;
        button.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View v) {
                Toast.makeText(container.getContext(), "You clicked: " + page + ". page.", Toast.LENGTH_SHORT).show();
            }
        });
        //to container add layout instead of button
        container.addView(layout);
        //return layout instead of buttonreturn layout;
    }

    @OverridepublicvoiddestroyItem(ViewGroup container, int position, Object object) {
        //cast to LinearLayout
        container.removeView((LinearLayout)object);
    }
}

Solution 2:

if you want to inflate views in pager you must have to implement two methods. instantiateItem and destroyItem

publicclassDialogPagerAdapterextendsPagerAdapter {

    privateContext mContext;

    //view inflating..@OverridepublicObjectinstantiateItem(ViewGroup collection, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.account_dialog_signin_viewpagers,
                collection, false);

        TextView tvLabel = (TextView) layout.findViewById(R.id.textView);
        switch (position) {
            case0:
                tvLabel.setText("Log In");
                tvLabel.setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                    }
                });
                break;
            case1:
                tvLabel.setText("Sign Up");
                tvLabel.setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                    }
                });
                break;
            case2:
                tvLabel.setText("Send Reset Link");
                tvLabel.setOnClickListener(newView.OnClickListener() {

                    @OverridepublicvoidonClick(View v) {
                        //onOptionClickForgot.OnOptionClick();
                    }
                });
                break;
        }

        collection.addView(layout);
        return layout;
    }

    @OverridepublicvoiddestroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
}

    @Overridepublic int getCount() {
        return3;
    }

    @OverridepublicbooleanisViewFromObject(View view, Objectobject) {
        return view == object;
    }
}

Simply call it like

viewPager.setAdapter(new DialogPagerAdapter);

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dialog_button_height"
    android:paddingLeft="@dimen/dimen_2"
    android:paddingRight="@dimen/dimen_2"
    android:minHeight="@dimen/dialog_button_height">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_medium" />
</RelativeLayout>

Post a Comment for "Correctly Implementing Pageradapter In Android"