ViewPager和PhotoViewAttacher的小错误

时间:2015-04-08 22:55:01

标签: android xml imageview android-viewpager photo

ViewPager和PhotoViewAttacher的小错误,第二张及以下图片首先显示在顶部,然后单击中心支架。如何制作这张照片是在中心?

TestPager.java

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;
import uk.co.senab.photoview.PhotoViewAttacher;


public class TestPager extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_pager);
    CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this);
    ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCustomPagerAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class CustomPagerAdapter extends PagerAdapter {
    Picasso mPicasso;
    PhotoViewAttacher mAttacher;

    int[] mResources = {
            R.drawable.first,
            R.drawable.second,
            R.drawable.third
    };


    Context mContext;
    LayoutInflater mLayoutInflater;

    public CustomPagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater)       mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mResources.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item,  container, false);
        ImageView imageView = (ImageView)  itemView.findViewById(R.id.imageViewPager);
         mPicasso.with(TestPager.this).load(mResources[position]).into(imageView);
        container.addView(itemView);
        mAttacher = new PhotoViewAttacher(imageView);
        return itemView;
    }

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

}

layout_pager.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/relativeLayout">


    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

</RelativeLayout>

pager_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:id="@+id/imageViewPager" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我使用UniversalImageLoader而不是Picasso并遇到了同样的问题。 我的解决方案是在异步加载回调中新建attacher

就我而言,代码是:

imageLoader.displayImage(url, imgView, new ImageLoadingListener() {
    @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            // Make imageview interactive
            attacher = new PhotoViewAttacher(imgView);
        }
     });

对于使用Picasso的情况,它可能类似于:

mPicasso.with(TestPager.this).load(mResources[position]).into(imageView, new Callback() {
    @Override
    public void onSuccess() {
        mAttacher = new PhotoViewAttacher(imageView)
    }

    @Override
    public void onError() {
    }
});