标签:
Activity以及Adapter
public class PicDetailActivity extends BaseActivity { public static final String EXTRA_DOC = "doc"; private Document doc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_pic_detail); ImageViewPager viewPager = (ImageViewPager) findViewById(R.id.picDetail); doc = (Document) getIntent().getSerializableExtra(EXTRA_DOC); PicDetailAdapter adapter = new PicDetailAdapter(this); adapter.setItem(doc.getPicAttaches()); viewPager.setAdapter(adapter); //页码显示 final TextView pageIndicator = (TextView) findViewById(R.id.indicator_page); TextView totalIndicator = (TextView) findViewById(R.id.indicator_total); pageIndicator.setText("1"); //页码总数 totalIndicator.setText(String.valueOf(doc.getPicAttaches().size())); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { //滑动时改变当前页码 pageIndicator.setText(String.valueOf(i + 1)); } @Override public void onPageScrollStateChanged(int i) { } }); } public void goBack(View view) { finish(); } //设置Adapter private class PicDetailAdapter extends PagerAdapter { private Context context; private LayoutInflater inflater; private List<Document.PicAttach> items; private List<View> pages; public PicDetailAdapter(Context context) { this.inflater = getLayoutInflater(); this.context = context; } public void setItem(List<Document.PicAttach> items) { this.items = items; //new一个容器,添加view(即为每个page添加图片) pages = new ArrayList<View>(items.size()); for (Document.PicAttach picAttach : items) { String absurl = picAttach.getAbsurl(); String imgdesc = picAttach.getImgdesc(); View view = inflater.inflate(R.layout.pic_detail, null); //图片 ImageView imageView = (ImageView) view.findViewById(R.id.pic_detail); //图片描述 TextView textView = (TextView) view.findViewById(R.id.imgdesc); if (absurl != null) { UrlImageViewHelper.setUrlDrawable(imageView, absurl); } if (imgdesc != null) { textView.setText(URLDecoder.decode(imgdesc)); /*textView.setText("五月飞絮很重。\n" + "一朵柳絮穿过一条街,要用一个上午的时间。\n" + "它们跌跌撞撞,起起落落,微不足道却又自顾自地交缠、拥抱、崩裂,就像尘世里的那些故事,渺小而卑微地轰轰烈烈着,全由当事人的情智予以消化。\n" + "然后穿街过巷,最终被遗忘在时间的角落。\n" + "好像发生过什么,又好像,什么也没有发生过。\n" + "五月飞絮很重。\n" + "一朵柳絮穿过一条街,要用一个上午的时间。\n" + "它们跌跌撞撞,起起落落,微不足道却又自顾自地交缠、拥抱、崩裂,就像尘世里的那些故事,渺小而卑微地轰轰烈烈着,全由当事人的情智予以消化。\n" + "然后穿街过巷,最终被遗忘在时间的角落。\n" + "好像发生过什么,又好像,什么也没有发生过。");*/ } //给Arraylist添加每个页面(view) pages.add(view); view.setTag(items); } } @Override public Object instantiateItem(ViewGroup container, int position) { //初始化Item if (position < pages.size()) { View page = pages.get(position); container.addView(page); return page; } return null; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(pages.get(position)); } public List<Document.PicAttach> getItems() { if (items == null) { items = new ArrayList<Document.PicAttach>(); } return items; } @Override public int getCount() { return getItems().size(); } @Override public boolean isViewFromObject(View view, Object o) { return view == o; } } }
xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/black"> <RelativeLayout android:id="@+id/indicator" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="8dp" android:background="@android:color/black"> <IconTextView android:id="@+id/backIcon" android:text="{fa-angle-left}" android:textSize="30dp" android:textColor="#fff" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerInParent="true" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:clickable="true" android:onClick="goBack" /> <LinearLayout android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/indicator_page" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#fff"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textSize="18sp" android:textColor="#fff"/> <TextView android:id="@+id/indicator_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#fff" /> </LinearLayout> </RelativeLayout> <com.terton.putian.ImageViewPager android:id="@+id/picDetail" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" /> </LinearLayout>
适配器layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/pic_detail" android:layout_width="match_parent" android:layout_height="match_parent" /> <ScrollView android:layout_width="match_parent" android:layout_height="124dp" android:background="#88000000" android:layout_alignParentBottom="true"> <TextView android:id="@+id/imgdesc" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="8dp" android:textColor="#ffffff" android:textSize="16sp" /> </ScrollView> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/aiyobucuoo/p/4599596.html