Reputation: 1710
I have a ViewPager and it is working. It is showing one image and text on every page.
This is my adapter:
private class SuggestionsAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
private LayoutInflater inflater;
private ViewPager pager;
public SuggestionsAdapter(ViewPager pager) {
this.pager = pager;
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
pager.setCurrentItem(gallery.size(), true);
} else if (position == gallery.size() + 1) {
pager.setCurrentItem(1, true);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public int getCount() {
return gallery.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.one_suggestion_layout, container, false);
ImageView ivImage = (ImageView) itemView.findViewById(R.id.ivImage);
ivImage.setImageBitmap(gallery.get(position));
// Add viewpager_item.xml to ViewPager
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
container.removeView((RelativeLayout) object);
}
}
And I'm using very simple in my code:
ViewPager vpSuggestions = (ViewPager) findViewById(R.id.vpSuggestions);
SuggestionsAdapter sugAdapter = new SuggestionsAdapter(vpSuggestions);
vpSuggestions.setAdapter(sugAdapter);
This is mu xml for slider:
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7">
<TextView
android:gravity="center"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/suggestions"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/vpSuggestions" />
</LinearLayout>
And this is xml for one page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ivImage1"
android:adjustViewBounds="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:src="@drawable/cat_321_sofa" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
android:id="@+id/tvTitle1"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/ivImage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
Because the size of pager is rectangle (small height, huge width) I want to show two images in one page.
The pager now looks like this image:
I want to be like this image:
How can I do that?
Upvotes: 1
Views: 1255
Reputation: 1710
The solution is to override getPageWidth() and divide it with 2
@Override
public float getPageWidth(int position) {
return (super.getPageWidth(position) / 2);
}
Upvotes: 2