我正在进行画廊活动,但我遇到了一些麻烦。我似乎无法使按钮(在viewpager的xml之外)保持静态。滑动时会创建看似重复的按钮,即使它位于viewpager的xml之外。我附上了一张捕捉到这个问题的图片。
<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="eqlogic.annswingsandthings.Activity_Gallery">
<android.support.v4.view.ViewPager
android:id="@+id/gallery_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<Button
android:layout_width="64dp"
android:layout_height="64dp"
android:id="@+id/send_photo_button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@android:drawable/ic_menu_camera" />
</RelativeLayout>
public class GalleryPagerAdapter extends FragmentPagerAdapter {
static Context m_Context;
public GalleryPagerAdapter
(FragmentManager fm, Context context)
{
super(fm);
m_Context = context;
}
@Override
public Fragment getItem(int i)
{
return GalleryBackgroundFragment.NewInstance(i);
}
@Override
public int getCount() {
if(GalleryDataHandler.m_Bitmaps != null)
return GalleryDataHandler.m_Bitmaps.size();
else return 0;
}
}
public class GalleryBackgroundFragment extends Fragment {
BitmapDrawable m_Bitmap;
public static GalleryBackgroundFragment NewInstance(int i)
{
try
{
GalleryBackgroundFragment gallery = new GalleryBackgroundFragment();
gallery.m_Bitmap = new BitmapDrawable
(Activity_Gallery.m_LocalActivity.getResources(),
GalleryDataHandler.m_Bitmaps.get(i));
gallery.setRetainInstance(true);
return gallery;
}
catch(Exception e)
{ Log.d("Error:", e.toString()); e.printStackTrace(); return null; }
}
@Override
public View onCreateView
(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_gallery, container, false);
view.setBackground(m_Bitmap);
return view;
}
}