Viewpager幻灯片下一页滞后

时间:2015-05-04 13:32:32

标签: android android-fragments android-viewpager

我有可点击项目的网格视图。当我点击一个项目时,gridview变得隐藏,我显示了一个ViewPager。 在我的情况下,这个ViewPager应该像幻灯片一样工作。

所以我尝试在此处关注Google示例:http://developer.android.com/training/animation/screen-slide.html

如果我在gridview项目点击上打开一个新活动,则viewpager运行良好(如Google示例)。但是,如果我打开隐藏网格视图的ViewPager,幻灯片动画就会变得滞后。

显然,我已尝试使用相同的图形/位图等打开viewpager。

不幸的是,我的应用程序需要在同一个活动中工作,我无法打开一个新的。

ViewPager有一些限制吗?或者我应该注意一些特别的事情?

1 个答案:

答案 0 :(得分:0)

非常简单。

  

不幸的是,我的应用程序需要在同一个活动中工作,我无法打开一个新的。

我认为当用户点击某个项目的网格视图(可能包含图像)时,视图寻呼机会显示图像,用户可以将ViewPager滑动到下一个图像,依此类推。

在这种情况下,请使用Dialog Fragment。

ViewPager图片的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<ImageView
    android:id="@+id/image_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:scaleType="fitCenter" />

</RelativeLayout>

对话片段代码:

public class SlideshowDialogFragment extends DialogFragment {
private String TAG = SlideshowDialogFragment.class.getSimpleName();
private ArrayList<Image> images;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private TextView lblCount, lblTitle, lblDate;
private int selectedPosition = 0;

static SlideshowDialogFragment newInstance() {
    SlideshowDialogFragment f = new SlideshowDialogFragment();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
    viewPager = (ViewPager) v.findViewById(R.id.viewpager);
    lblCount = (TextView) v.findViewById(R.id.lbl_count);
    lblTitle = (TextView) v.findViewById(R.id.title);
    lblDate = (TextView) v.findViewById(R.id.date);

    images = (ArrayList<Image>) getArguments().getSerializable("images");
    selectedPosition = getArguments().getInt("position");

    Log.e(TAG, "position: " + selectedPosition);
    Log.e(TAG, "images size: " + images.size());

    myViewPagerAdapter = new MyViewPagerAdapter();
    viewPager.setAdapter(myViewPagerAdapter);
    viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

    setCurrentItem(selectedPosition);

    return v;
}

private void setCurrentItem(int position) {
    viewPager.setCurrentItem(position, false);
    displayMetaInfo(selectedPosition);
}

//  page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        displayMetaInfo(position);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }
};

private void displayMetaInfo(int position) {
    lblCount.setText((position + 1) + " of " + images.size());

    Image image = images.get(position);
    lblTitle.setText(image.getName());
    lblDate.setText(image.getTimestamp());
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}

//  adapter
public class MyViewPagerAdapter extends PagerAdapter {

    private LayoutInflater layoutInflater;

    public MyViewPagerAdapter() {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);

        ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);

        Image image = images.get(position);

        Glide.with(getActivity()).load(image.getLarge())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageViewPreview);

        container.addView(view);

        return view;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == ((View) obj);
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
}

通过以下代码发送图片链接及其说明/标题:

 Bundle bundle = new Bundle();
    bundle.putSerializable("images", images);
    bundle.putInt("position", position);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
    newFragment.setArguments(bundle);
    newFragment.show(ft, "slideshow");

正在使用的数组列表:

 private ArrayList<Image> images = new ArrayList<>();
 Image image = new Image();
 image.setName("Image Title");
 image.setLarge("ImageUrl");