平滑的ViewPager

时间:2014-11-10 13:57:54

标签: android android-fragments view android-viewpager

我使用FragmentStatePagerAdapter实现了一个ViewPager,它运行正常。但是页面更改过渡看起来有点慢。移动到下一页时有点滞后。

我在500 * 500的每页上一次加载大约5张图像,这些图像是使用Picasso下载的(所以我猜它是在一个单独的线程上)

除此之外,我的主要线程没有繁重的工作,因此我不能完成任何问题。

需要一些帮助才能让它更顺畅,更快一些。

我的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (getArguments() != null) {

        isThumbLoaded = true;

        mType = getArguments().getInt(MyGirdCrushesActivity.CLICKED_WHAT,
                HomeActivity.TYPE_HOME);
    }

    rootView = inflater.inflate(R.layout.fragment_profile_view, container,
            false);

    return rootView;

}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    // on create
    mActivity = (HomeActivity) getActivity();

    ProgressBar progress = new ProgressBar(mActivity);

    // on create view
    main_relative = (View) rootView.findViewById(R.id.main_relative);

    newUsersProfile = (FacesModel) getArguments().getSerializable(
            NetworkController.MODEL_KEY);

    networkController = new NetworkController(getActivity());

    newImageLoader = new ImageLoader(getActivity(), true, false, false);

    img_profile_pic = (ImageView) rootView
            .findViewById(R.id.img_profile_pic);


    imageLoader = new ImageLoader(getActivity(), false, false, false);

    // if (NetworkController.isConnectingToInternet(getActivity())) {
    arraProfileImage = (List<ProfileImagesModel>) Arrays
            .asList(newUsersProfile.getLstProfileImages());

    img_profile_pic.setOnClickListener(ProfileViewsFragment.this);

    view_profile_details = (View) rootView
            .findViewById(R.id.view_profile_details);

    rela_profile_details = (LinearLayout) view_profile_details
            .findViewById(R.id.rela_profile_details);
    rela_profile_details.setOnClickListener(ProfileViewsFragment.this);

    layout_mutual_friends = (RelativeLayout) rootView
            .findViewById(R.id.layout_mutual_friends);

    layout_mutual_friends.setOnClickListener(ProfileViewsFragment.this);

    txt_profile_person_name = (TextView) view_profile_details
            .findViewById(R.id.txt_profile_person_name);
    txt_profile_person_age = (TextView) view_profile_details
            .findViewById(R.id.txt_profile_person_age);
    txt_profile_person_location = (TextView) view_profile_details
            .findViewById(R.id.txt_profile_person_location);

    txtMutualCount = (TextView) view_profile_details
            .findViewById(R.id.txtMutualCount);

    setUserData();

    gallery_profile_images = (LinearLayout) rootView
            .findViewById(R.id.gallery_profile_images);

    View cell;

    for (int i = 0; i < arraProfileImage.size(); i++) {
        pos = i;
        // System.out.println("Inside For loop");

        cell = getActivity().getLayoutInflater().inflate(
                R.layout.layout_list_item, null);

        final RoundedImageView imageThumbnail = (RoundedImageView) cell
                .findViewById(R.id.img_profile_gallery);

        imageThumbnail.setId(i);

        imgThumbnails.add(imageThumbnail);

        imageThumbnail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Clears previous selection
                clearSelections();

                mSelectionUrl = arraProfileImage
                        .get(imageThumbnail.getId()).getImageUrl4();

                v.setSelected(true);

                img_profile_pic.setTag(arraProfileImage.get(
                        imageThumbnail.getId()).getImageUrl4());


                MyApp
                        .getPicasso()
                        .load(arraProfileImage.get(imageThumbnail.getId())
                                .getImageUrl4())
                        .placeholder(Commons.getDefaultImageSrc(mActivity))
                        .into(img_profile_pic);


            }
        });


                       MyApp.getPicasso()
                .load(arraProfileImage.get(i).getImageUrl4())
                .placeholder(Commons.getDefaultImageSrcTrans(mActivity))
                .into(imageThumbnail);



        imageThumbnail.setTag(arraProfileImage.get(i).getImageUrl4());

        if (arraProfileImage.get(i).isProfileImage()) {
            imageThumbnail.setSelected(true);
            mProfilePic = arraProfileImage.get(i).getImageUrl4();

            MyApp.getPicasso()
                    .load(arraProfileImage.get(i).getImageUrl4())
                    .placeholder(Commons.getDefaultImageSrc(mActivity))
                    .into(img_profile_pic);



        }


        img_profile_pic.setTag(arraProfileImage.get(i).getImageUrl4());



        gallery_profile_images.addView(cell);
    }


    }


}

我的适配器:

 @Override
 public Fragment getItem(int position) {

    if (type == HomeActivity.TYPE_HOME) {
        if (position > arrNewUsersProfiles.size() - 1) {
            BlankFaceFragment blank = new BlankFaceFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(MyGirdCrushesActivity.CLICKED_WHAT, type);
            blank.setArguments(bundle);
            return blank;
        } else {
            ProfileViewsFragment fragment = new ProfileViewsFragment();
            // fragment.setmActivity(mActivity);
            Bundle args2 = new Bundle();
            System.out.println("RJ POSITION " + position);

            args2.putInt(MyGirdCrushesActivity.CLICKED_WHAT, type);
            args2.putSerializable(NetworkController.MODEL_KEY,
                    arrNewUsersProfiles.get(position));

            fragment.setArguments(args2);
            return fragment;
        }
    } 

    }

}

请知道我是否需要更多代码。

1 个答案:

答案 0 :(得分:0)

您是否尝试过实施ViewHolder模式?

此示例显示如何在ListView上实现模式,但该概念也适用于ViewPager。

http://developer.android.com/training/improving-layouts/smooth-scrolling.html