【问题标题】:Minor bug with ViewPager and PhotoViewAttacherViewPager 和 PhotoViewAttacher 的小错误
【发布时间】:2015-04-08 22:55:01
【问题描述】:

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>

【问题讨论】:

    标签: android xml imageview android-viewpager photo


    【解决方案1】:

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

    在我的例子中,代码是:

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

    对于您使用毕加索的情况,可能是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多