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

第一部分,布局

<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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AndroidViewPager是一种常用的布局容器,用于在屏幕上滑动显示多个页面。在实现轮播效果时,可以通过以下步骤来实现: 1. 首先,在XML布局文件中添加ViewPager组件。可以设置该组件的宽度和高度,以及其他属性,如指示器dots等。 2. 创建一个适配器类(如PagerAdapter),用于管理ViewPager中的页面。适配器需要重写一些方法,如获取页面数量、创建页面和销毁页面等。 3. 在创建页面的方法中,可以使用LayoutInflater.inflate()方法来加载布局文件,然后将其添加到ViewPager中。可以根据需求自定义每个页面的布局。 4. 在Activity或Fragment中,通过findViewById()方法获取ViewPager组件的实例,并设置适配器。例如: ViewPager viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(adapter); 5. 如果需要实现轮播效果,可以通过设置ViewPager的setCurrentItem()方法来切换页面。可以使用Handler类来实现定时切换页面的效果。例如: Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { int currentItem = viewPager.getCurrentItem(); if (currentItem < adapter.getCount() - 1) { currentItem++; } else { currentItem = 0; } viewPager.setCurrentItem(currentItem); handler.postDelayed(this, 3000); // 间隔3秒切换页面 } }; handler.postDelayed(runnable, 3000); 以上就是使用AndroidViewPager实现轮播效果的简要步骤。通过设置适配器和定时切换页面,可以让多个页面在屏幕上自动滑动显示,从而实现轮播效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值