Android 图片滑动练习

练习:放入三张图片,使其滑动显示,并在最后一张跳转到另一个界面。

布局:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".ViewPagerActivity">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp"
        android:background="#161616"
        >
    </android.support.v4.view.ViewPager>
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<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="100dp"
    android:layout_height="100dp"
    android:id="@+id/iv"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    />
<TextView
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    android:gravity="center"
    android:text="测试"
    />
</LinearLayout>
单个图片效果:图片在上,文字在下。
                如图:
        
代码:

package com.example.administrator.jreduch05;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends Activity {
    private ViewPager vp;
    private List<ImageView> imgList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        vp=(ViewPager) findViewById(R.id.vp);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if(position==2){
                    Intent intent=new Intent(ViewPagerActivity.this,zuoe81_2Activity.class);
                    startActivity(intent);
                    finish();              
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
                
            }
        });
        imgList=new ArrayList<>();
        //将图片放到List        ImageView img=new ImageView(this);
        img.setImageResource(R.mipmap.renrou);
        imgList.add(img);

        img= new ImageView(this);
        img.setImageResource(R.mipmap.zhaopian);
        imgList.add(img);

        img= new ImageView(this);
        img.setImageResource(R.mipmap.jietu1);
        imgList.add(img);

        vp.setAdapter(new MyViewPagerAdapter(imgList));

    }
    public class  MyViewPagerAdapter extends PagerAdapter{
        private List<ImageView> imgList;
        public MyViewPagerAdapter(List<ImageView> imgList){
            this.imgList=imgList;
        }
        @Override
        public int getCount() {
            return imgList.size();
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //ViewPager里放图片
            container.addView(imgList.get(position));
            return imgList.get(position);

        }

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



上一篇:rpm安装mysql图文


下一篇:SQL查看表字段信息如:字段名、字段类型、字段精度、字段大小、索引、主键等