1.PhotoActivity.java的代码
package com.ynhere.forestharmfulpest.module.photoView;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.ynhere.forestharmfulpest.R;
import com.ynhere.forestharmfulpest.utils.StatusBarUtil;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* 照片查看界面
*/
public class PhotoActivity extends AppCompatActivity {
public static final String IMG_LIST_FLAG = "img_list_flag";
public static final String IMG_LIST_POSITION_FLAG = "image_position";
@BindView(R.id.viewPager)
ViewPager viewPager;
@BindView(R.id.hint)
TextView hint;
private int imgCount = 0;
private ViewPager.OnPageChangeListener listener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
hint.setText((position + 1) + "/" + imgCount);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
ButterKnife.bind(this);
StatusBarUtil.setColor(this, Color.BLACK);
initData();
}
private void initData() {
ArrayList<String> images = getIntent().getStringArrayListExtra(IMG_LIST_FLAG);
int position = getIntent().getIntExtra(IMG_LIST_POSITION_FLAG, 0);
PhotoPagerAdapter photoPagerAdapter = new PhotoPagerAdapter(this, images);
viewPager.setAdapter(photoPagerAdapter);
viewPager.addOnPageChangeListener(listener);
imgCount = images.size();
hint.setText(1 + "/" + imgCount);
viewPager.setCurrentItem(position);
}
@OnClick({R.id.back})
public void click(View v) {
switch (v.getId()) {
case R.id.back:
overridePendingTransition(R.anim.out, R.anim.in);
finish();
break;
}
}
}
2.PhotoViewPager.java的代码
package com.ynhere.forestharmfulpest.module.photoView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* 照片列表
* 解决PhotoView Bug
* https://github.com/chrisbanes/PhotoView
*/
public class PhotoViewPager extends android.support.v4.view.ViewPager {
public PhotoViewPager(Context context) {
super(context);
}
public PhotoViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
try {
return super.onTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
return false;
}
}
3.点击图片显示大图 怎么点击adapter里的照片 监听item里的image
com.github.chrisbanes.photoview.PhotoView;
PhotoView的依赖 加载在suplib下,就是对的。加载在app下