Проблема с ViewPager и PhotoViewAttacher
Добавлено: 09 апр 2015, 19:39
Есть небольшая проблема с ViewPager и библиотекой PhotoViewAttacher, когда открывается ViewPager первая картинка находится так как нужно - по центру, а далее когда начинаешь листать картинки влево, то вначале она оказывается вверху, пока не кликнешь по ней, тогда она центрируется. Если делать это без PhotoViewAttacher, то все нормально все картинки как положено в центре. Как исправить что бы картинки с PhotoViewAttacher всегда были по центру вначале.
TestPager.java
layout_pager.xml
pager_item.xml
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);
}
}
}Код: Выделить всё
<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>Код: Выделить всё
<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>