我有一个ViewPager,它正在运行。它在每个页面上显示一个图像和文本。
这是我的适配器:
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);
}
}
我在代码中使用非常简单:
ViewPager vpSuggestions = (ViewPager) findViewById(R.id.vpSuggestions);
SuggestionsAdapter sugAdapter = new SuggestionsAdapter(vpSuggestions);
vpSuggestions.setAdapter(sugAdapter);
这是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>
这是一页的xml:
<?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>
因为寻呼机的大小是矩形(高度小,宽度很大),我想在一个页面中显示两个图像。
寻呼机现在看起来像这样的图像:
我希望像这样的形象:
我该怎么做?
答案 0 :(得分:2)
解决方案是覆盖getPageWidth()并将其除以2
@Override
public float getPageWidth(int position) {
return (super.getPageWidth(position) / 2);
}