我正在尝试使用MultiViewPager实现viewpager
并使用google的zoomoutpageTransformer作为效果,目的是实现类似于此图像的效果:
但是我每次都无法在屏幕中间显示单个页面时,就无法在左右显示这两个页面!
这是我的查看寻呼机代码:
mViewPager.setPageMargin(
-64);
mAdapter = new ViewPagerAdapter(getActivity(), mClasses);
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
mViewPager.setAdapter(mAdapter);
这是我在xml中的viewpager:
<ir.phzrobin.Utils.MultiViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="20dp"
app:matchChildWidth="@+id/textview_class_title" >
</ir.phzrobin.Utils.MultiViewPager>
这是我的viewpager页面布局:
<TextView
android:id="@+id/textview_class_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="fsafjabf"
android:textAlignment="gravity"
android:textColor="@color/white"
android:textSize="17sp" />
<TextView
android:id="@+id/textview_class_teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview_class_title"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="fsafjabf"
android:textAlignment="gravity"
android:textColor="@color/white"
android:textSize="17sp" />
<ImageView
android:id="@+id/button_options"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:layout_marginRight="2dp"
android:background="@drawable/overflow" />
</RelativeLayout>
我试过的是
this
以及here的multiviewpager开发人员的回答
有人可以帮我实现目标吗?
谢谢
这是查看寻呼机适配器:
public class ViewPagerAdapter extends PagerAdapter {
// ImageLoader imageLoader = ImageLoader.getInstance();
// DisplayImageOptions options;
// private ImageLoadingListener imageListener;
Context activity;
List<Classes> items;
public ViewPagerAdapter(Context activity, List<Classes> items) {
this.activity = activity;
this.items = items;
}
public void setItems(List<Classes> items) {
this.items = items;
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final int pos = position;
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_viewpager, container, false);
TextView mClassTitle = (TextView) view
.findViewById(R.id.textview_class_title);
mClassTitle.setText("عنوان درس: " + items.get(pos).getSubject());
mClassTitle.setTypeface(AppFont.GetAppFont());
((TextView) view.findViewById(R.id.textview_class_teacher))
.setText("نام استاد: " + items.get(pos).getMasterName());
((TextView) view.findViewById(R.id.textview_class_teacher))
.setTypeface(AppFont.GetAppFont());
RelativeLayout mLayout = (RelativeLayout) view
.findViewById(R.id.rl_viewpager);
mLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent _intent = new Intent(ContextVal.GetAppContext(),
ClassNotes.class);
_intent.putExtra("CLASSID",
String.valueOf(items.get(pos).getID())); // set selected
// item id
// into
// intent
// activity
// to use in
// Classnotes
// Activity!
activity.startActivity(_intent); // open notes activity
}
});
ImageView mOverFlow = (ImageView) view.findViewById(R.id.button_options);
mOverFlow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PopupMenu popup = new PopupMenu(ContextVal.GetAppContext(), v);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() != null)
{//edit item
Intent _ClassWindow = new Intent(ContextVal.GetAppContext(),
AddNewClass.class);
_ClassWindow.putExtra("CLASSID", String.valueOf(items.get(pos).getID()));
activity.startActivity(_ClassWindow); }
else{//delete item
AlertDialog.Builder builder = new AlertDialog.Builder(
ContextVal.GetAppContext());
builder.setTitle("حذف کلاس");
builder.setMessage("آیا مطمعن هستید که میخواهید این درس را حذف کنید ؟");
builder.setPositiveButton("بله", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Log.e("onClick", "YES");
DatabaseHandler _db = new DatabaseHandler(ContextVal
.GetAppContext());
_db.deleteClass(items.get(pos));
_db.deleteClassNotes(items.get(pos).getID());
ContextVal.mViewFragment.RefreshViewPager(); // Refresh Items !
// and remove
// deleted item from
// user's eyes lol
dialog.dismiss();
}
});
builder.setNegativeButton("خیر", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
Log.e("onClick", "NO");
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show(); }
return true;
}
});
popup.show();
}
});
container.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
}
最佳答案
在GitHub上的MultiViewPager示例中查看此示例 fragment 页面布局:
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}">
<FrameLayout
android:id="@+id/vg_cover"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
tools:ignore="UselessParent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/bg_page"
android:scaleType="centerInside"
android:src="@drawable/im_pixplicity"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center" />
</FrameLayout>
</RelativeLayout>
有一个
RelativeLayout
,其宽度设置为match_parent
,而一个内部FrameLayout
的显式宽度为200dp。内部FrameLayout
中的内容最终是 fragment 页面。您的布局具有
app:matchChildWidth="@+id/textview_class_title"
,但是不像内部ViewGroup
那样是FrameLayout
。您需要重写您的 fragment 页面布局,以便您拥有一个内部
ViewGroup
,其中包含所有TextView
。另外,请勿显式调用
setPageMargin()
,MultiViewPager
会根据所有测量值在内部设置页边距。如果仍然无法解决此问题,建议您从MultiViewPager的示例应用程序开始,并对其进行修改,直到获得所需的布局结果为止。