Pages

Friday, April 5, 2013

Simple View Pager Example

1.Import library - Right click on project ,android tools, add support library.
2.Create an xml  (activity_main)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Page 1" android:id="@+id/textViewHeader"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center" android:padding="10dip" android:textStyle="bold"></TextView>
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/viewPager" />

</LinearLayout>

3. Create a java file 
main activity 
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter adap = new MyPagerAdapter(this);
vp.setAdapter(adap);
final TextView tvHeader = (TextView) findViewById(R.id.textViewHeader);
tvHeader.setText("Page 1");

vp.setOnPageChangeListener(new OnPageChangeListener(){

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
tvHeader.setText("Page " + (arg0 + 1));
}
});
}
private class MyPagerAdapter extends PagerAdapter {
private ArrayList<LinearLayout> arry;

public MyPagerAdapter(Context context) {
arry = new ArrayList<LinearLayout>();
// arry.add(new ListView1Page(context));
arry.add(new TextViewPage(context));
// views.add(new ListView2Page(context));
arry.add(new ButtonPage(context));
// use this when design done graphically 
}
@Override
public void destroyItem(View view, int arg1, Object object) {
((ViewPager) view).removeView((LinearLayout) object);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arry.size();
}
@Override
public Object instantiateItem(View view, int position) {
// View myView = views.get(position);
// ((ViewPager) view).addView(myView);
// return myView;
//
View v = new View(MainActivity.this.getApplicationContext());
       final LayoutInflater inflater = (LayoutInflater) MainActivity.this
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
       switch (position) {
       case 0:
           v = inflater.inflate(R.layout.j,
                   (ViewGroup) null, false);
//            ((Button) v.findViewById(R.id.pp1btnbck)).setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View arg0) {
//                    finish();
//                }
//            });

           break;
       case 1:
           v = inflater.inflate(R.layout.k, null, false
// xml file named k                    );
           break;
       default:

          
           break;
       }
       ((ViewPager) view).addView(v, 0);

       return v;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
public Parcelable saveState() {
return null;
}
}

}


4 . If design is from xmls ,create xml files ,here i have used two xmls k and j.
put wateva u want in those files


5. If done dynamically , create a class , i have named this as ButtonPage

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ButtonPage extends LinearLayout {

public ButtonPage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public ButtonPage(Context context) {
super(context);
init();
}

private void init() {
// setBackgroundColor(Color.RED);
// Button button = new Button(getContext());
// button.setText("4th Page");
//
// setGravity(Gravity.CENTER);
//
// LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
// LayoutParams.WRAP_CONTENT);
// addView(button, params);
}

}



No comments:

Post a Comment