I want to implement ImageView
and Button
in ViewPager
. But when I tried to implement it just crash my app and I am not able to implement it, Please help me as I am new for Android.
public class FullScreenImageActivity extends Activity {
private static int NUM_VIEWS = 5;
private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_view);
Intent i = getIntent();
int gotbiscuit = i.getExtras().getInt("position");
adapter = new MyPagerAdapter(this);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
public class MyPagerAdapter extends PagerAdapter {
public Integer[] images1 = { R.drawable.i_3, R.drawable.i_4,
R.drawable.i_6 };
public Integer[] images2 = { R.drawable.i_8, R.drawable.i_9,
R.drawable.i_10 };
public Context mContext;
public MyPagerAdapter(Context context) {
super();
this.mContext = context;
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images1.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((ImageView) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((ImageView) view);
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(container
.getContext());
View view1 = inflater.inflate(R.layout.fullscreen_view, null);
ImageView view = (ImageView) findViewById(R.id.imgDisplay);
Button btn = (Button) findViewById(R.id.wallb);
// view1.setScaleType(ScaleType.CENTER_CROP);
switch (gotbiscuit) {
case 0:
view.setImageResource(images1[position]);
break;
case 1:
view.setImageResource(images2[position]);
break;
}
view.setAdjustViewBounds(true);
((ViewPager) container).addView(view1, 0);
return view1;
}
}
}
this is my Pager xml file for pagerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
This
should ve
You also have
and inflate the same layout
should probabbly be
Modify the below according to your requirement
activity_main.xml
pagerview.xml
Snap