获取网络图片+ViewPager+自动轮播

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kd953163660/article/details/51419277

第一部分,布局

<LinearLayout 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:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/main_vp"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="@string/hello_world" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/image_one"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image_two"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/image_three"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image_four"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="首页" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="我的" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="下载" />
    </LinearLayout>

</LinearLayout>

第二部分 实体类创建

第三部分 工具类编写

public class NetWorkUtils {
    List<Bitmap> vpImg=new ArrayList<Bitmap>();

    public static MeiRiXue getNet(){
        String path="http://www.meirixue.com/api.php?c=index&a=index";
        //打开一个链接
        HttpClient client=new DefaultHttpClient();
        //请求方式
        HttpGet get=new HttpGet(path);
        try {
            HttpResponse response=client.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode==200) {
                String str=EntityUtils.toString(response.getEntity());
                Type type=new TypeToken<MeiRiXue>(){}.getType();
                Gson gson=new Gson();
                MeiRiXue meiRiXue=gson.fromJson(str, type);

                //拿到viewpager资源
                return meiRiXue;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }

        return null;
    }
}

第四部分 主线程UI编写

public class MainActivity extends Activity {

    private ViewPager vp;
    private ImageView image_one;
    private ImageView image_two;
    private ImageView image_three;
    private ImageView image_four;
    private BitmapUtils utils;
    private MeiRiXue mrx;
    private List<View> vList;

    int i=1;
    Handler h=new Handler(){
        public void handleMessage(android.os.Message msg) {
            vp.setCurrentItem(i%vList.size());
            i++;
            h.sendEmptyMessageDelayed(1, 2000);
        };
    };

    Handler handler=new Handler(){

        public void handleMessage(android.os.Message msg) {
            utils.display(image_one,mrx.getData().getHotcourse().get(0).getImg());
            utils.display(image_two,mrx.getData().getHotcourse().get(1).getImg());
            utils.display(image_three,mrx.getData().getHotcourse().get(2).getImg());
            utils.display(image_four,mrx.getData().getHotcourse().get(3).getImg());

            vList =getView();
            System.out.println("vList="+vList.size());
            vp.setAdapter(new PagerAdapter() {

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                    // TODO Auto-generated method stub
                    return arg0==arg1;
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return vList.size();
                }

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

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    View view=vList.get(position);
                    container.addView(view);
                    return view;
                }
            });
            h.sendEmptyMessage(0x123);
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.main_vp);
        image_one = (ImageView) findViewById(R.id.image_one);
        image_two = (ImageView) findViewById(R.id.image_two);
        image_three = (ImageView) findViewById(R.id.image_three);
        image_four = (ImageView) findViewById(R.id.image_four);

        utils = new BitmapUtils(MainActivity.this);
        getMeiRiXue();
    }

    private void getMeiRiXue() {
        new Thread(){

            public void run() {
                System.out.println("启动线程");
                mrx = NetWorkUtils.getNet();
                if (mrx!=null) {
                    handler.sendEmptyMessage(0x123);
                }
            };
        }.start();
    }

    public List<View> getView(){
        List<View> lv=new ArrayList<View>();
        List<Adlist> adlist=mrx.getData().getAdlist();
        for (Adlist adlist2 : adlist) {
            View inflate=View.inflate(MainActivity.this, R.layout.new_fragment, null);
            ImageView img=(ImageView) inflate.findViewById(R.id.fragment_image);
            utils.display(img, adlist2.getImg());
            lv.add(inflate);
            System.out.println("------------");
            adlist2.getImg();
        }
        return lv;
    }

}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页