关闭

恒博新闻动态+标题一、标题二

标签: android
264人阅读 评论(0) 收藏 举报
分类:
<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" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

    <Button
        android:id="@+id/bt_enter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="进入"
        android:visibility="gone" />

</RelativeLayout>
<?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="horizontal" >
    <ImageView 
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    <TextView 
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        />
    <TextView 
        android:id="@+id/uptime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="更新时间"
        />
    </LinearLayout>

</LinearLayout>
<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"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/classname1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="标题一"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/classname2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
             android:text="标题二"
            android:textSize="24sp" />
    </LinearLayout>
    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</LinearLayout>

实体类

package com.bwie.test.bean;

public class Data1 {
    public String cid;
    public String classname;
}


public class Data2 {

    public List<Info> info;

    public class Info{
        public String id;
        public String picurl;
        public String title;
        public String uptime;
    }
}

public class Data3 {

    public List<Info> info;

    public class Info{
        public String id;
        public String picurl;
        public String title;
        public String uptime;
    }
}

public class MyImage {
    public String src;
}

activity

public class MainActivity extends Activity {

    private ViewPager vp_main;
    private Button bt_enter;

    private String path = "http://172.17.29.120/localuser/ljy/ndhx/pic.json";
    private String host = "http://172.17.29.120/localuser/ljy/ndhx/";

    /**
     * 图片 http://172.17.29.120/localuser/ljy/ndhx/pic.json
     * 
     * 栏目导航接口文件 http://172.17.29.120/localuser/ljy/ndhx/nav.json
     * 
     * 关于我们接口文件 http://172.17.29.120/localuser/ljy/ndhx/about.json
     * 
     * 培训动态接口文件 http://172.17.29.120/localuser/ljy/ndhx/peixundongtai.json
     */

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

        // 查找控件
        vp_main = (ViewPager) findViewById(R.id.vp_main);
        bt_enter = (Button) findViewById(R.id.bt_enter);

        //第一次进入导航页 
        SharedPreferences sp=getSharedPreferences("navigation", 0);
        boolean flag=sp.getBoolean("flag", true);
        Editor edit=sp.edit();
        if (flag) {
            new MyAsyncTask().execute(path);
            edit.putBoolean("flag", false);
        }else{
            //否则进入主页
            Intent intent=new Intent(this, HomeActivity.class);
            startActivity(intent);
            finish();
        }
        edit.commit();
    }

    /**
     * 异步类
     * @author 蓝调
     *
     */
    class MyAsyncTask extends AsyncTask<String,Void, MyImage[]>{

        @Override
        protected MyImage[] doInBackground(String... params) {
            MyImage [] images=getData(params[0]);
            return images;
        }
        //获取网络数据
        private MyImage[] getData(String string) {
            try {
                HttpClient client=new DefaultHttpClient();
                HttpGet get=new HttpGet(string);
                HttpResponse response=client.execute(get);
                if (response.getStatusLine().getStatusCode()==200) {
                    InputStream is=response.getEntity().getContent();
                    int len;
                    byte [] arr=new byte[1024];
                    StringBuffer sb=new StringBuffer();
                    while ((len=is.read(arr))!=-1) {
                        sb.append(new String(arr,0,len));
                    }
                    String json=sb.toString();
                    System.out.println(json);
                    Gson gson=new Gson();
                    MyImage[] images=gson.fromJson(json, MyImage[].class);
                    return images;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(final MyImage[] images) {
            super.onPostExecute(images);

            //为ViewPager设置适配器
            vp_main.setAdapter(new PagerAdapter() {

                @Override
                public boolean isViewFromObject(View arg0, Object arg1) {
                    return arg0==arg1;
                }

                @Override
                public int getCount() {
                    return images.length;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    ImageView iv=new ImageView(MainActivity.this);
                    iv.setScaleType(ScaleType.FIT_XY);
                    BitmapUtils bitmapUtils=new BitmapUtils(MainActivity.this);
                    bitmapUtils.display(iv, host+images[position].src);
                    container.addView(iv);
                    return iv;
                }

                @Override
                public void destroyItem(ViewGroup container, int position,
                        Object object) {
                    container.removeView((View) object);
                }
            });
            //设置监听
            vp_main.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int arg0) {
                    if (arg0==2) {
                        bt_enter.setVisibility(View.VISIBLE);
                    }
                    //点击跳转到主页面
                    bt_enter.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
        }
    }

}
public class HomeActivity extends Activity {

    private String path1 = "http://172.17.29.120/localuser/ljy/ndhx/nav.json";
    private String path2 = "http://172.17.29.120/localuser/ljy/ndhx/about.json";
    private String path3 = "http://172.17.29.120/localuser/ljy/ndhx/peixundongtai.json";

    private ListView lv;
    private TextView classname1;
    private TextView classname2;

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

        lv = (ListView) findViewById(R.id.lv);
        classname1 = (TextView) findViewById(R.id.classname1);
        classname2 = (TextView) findViewById(R.id.classname2);

        // 请求导航接口
        new MyAsyncTask1().execute(path1);
        //为导航项设置监听
        classname1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new MyAsyncTask2().execute(path2);
            }
        });
        classname2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new MyAsyncTask3().execute(path3);
            }
        });
        new MyAsyncTask2().execute(path2);
    }

    class MyAsyncTask1 extends AsyncTask<String, Void, Data1[]> {

        @Override
        protected Data1[] doInBackground(String... params) {
            Data1[] data = getData(params[0]);
            return data;
        }

        @Override
        protected void onPostExecute(Data1[] data) {
            super.onPostExecute(data);
            classname1.setText(data[0].classname);
            classname2.setText(data[1].classname);
        }

        private Data1[] getData(String string) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(string);
                HttpResponse response = client.execute(get);
                if (response.getStatusLine().getStatusCode() == 200) {
                    InputStream is = response.getEntity().getContent();
                    int len;
                    byte[] arr = new byte[1024];
                    StringBuffer buffer = new StringBuffer();
                    while ((len = is.read(arr)) != -1) {
                        buffer.append(new String(arr, 0, len, "gbk"));
                    }
                    String json = buffer.toString();
                    System.out.println("MyAsyncTask1==" + json);
                    Gson gson = new Gson();
                    Data1[] data = gson.fromJson(json, Data1[].class);
                    return data;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    class MyAsyncTask2 extends AsyncTask<String, Void, Data2> {

        private List<Info> info;

        @Override
        protected Data2 doInBackground(String... params) {
            Data2 data2 = getData(params[0]);
            return data2;
        }

        @Override
        protected void onPostExecute(Data2 data) {
            super.onPostExecute(data);
            info = data.info;
            lv.setAdapter(new BaseAdapter() {

                @Override
                public View getView(int position, View convertView,
                        ViewGroup parent) {
                    ViewHolder viewHolder;
                    if (convertView == null) {
                        viewHolder = new ViewHolder();
                        convertView = View.inflate(HomeActivity.this,
                                R.layout.item_lv, null);
                        viewHolder.iv = (ImageView) convertView
                                .findViewById(R.id.iv);
                        viewHolder.title = (TextView) convertView
                                .findViewById(R.id.title);
                        viewHolder.uptime = (TextView) convertView
                                .findViewById(R.id.uptime);
                        convertView.setTag(viewHolder);
                    } else {
                        viewHolder = (ViewHolder) convertView.getTag();
                    }
                    BitmapUtils bitmapUtils = new BitmapUtils(HomeActivity.this);
                    bitmapUtils.display(viewHolder.iv,
                            info.get(position).picurl);
                    viewHolder.uptime.setText(info.get(position).uptime);
                    return convertView;
                }

                @Override
                public long getItemId(int position) {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return null;
                }

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

        private Data2 getData(String string) {
            try {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(string);
                HttpResponse response = client.execute(get);
                if (response.getStatusLine().getStatusCode() == 200) {
                    InputStream is = response.getEntity().getContent();
                    int len;
                    byte[] arr = new byte[1024];
                    StringBuffer buffer = new StringBuffer();
                    while ((len = is.read(arr)) != -1) {
                        buffer.append(new String(arr, 0, len, "gbk"));
                    }
                    String json = buffer.toString();
                    System.out.println("MyAsyncTask===" + json);
                    Gson gson = new Gson();
                    Data2 data = gson.fromJson(json, Data2.class);
                    return data;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }

    class MyAsyncTask3 extends AsyncTask<String, Void, Data3>{

        private List<com.bwie.test.bean.Data3.Info> info2;


        @Override
        protected Data3 doInBackground(String... params) {
            Data3 data=getData(params[0]);
            return data;
        }

        @Override
        protected void onPostExecute(Data3 data) {
            super.onPostExecute(data);
            info2 = data.info;

            lv.setAdapter(new BaseAdapter() {

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    // ListView优化
                    ViewHolder holder = null;
                    if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = View.inflate(HomeActivity.this,
                                R.layout.item_lv, null);
                        holder.iv = (ImageView) convertView
                                .findViewById(R.id.iv);
                        holder.title = (TextView) convertView
                                .findViewById(R.id.title);
                        holder.uptime = (TextView) convertView
                                .findViewById(R.id.uptime);
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
                    BitmapUtils bitmapUtils = new BitmapUtils(HomeActivity.this);
                    bitmapUtils.display(holder.iv, info2.get(position).picurl);
                    holder.title.setText(info2.get(position).title);
                    holder.uptime.setText(info2.get(position).uptime);
                    return convertView;
                }

                @Override
                public long getItemId(int position) {
                    // TODO Auto-generated method stub
                    return 0;
                }

                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return null;
                }

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


        private Data3 getData(String string) {
            try {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(string);
                HttpResponse response = client.execute(get);
                if (response.getStatusLine().getStatusCode() == 200) {
                    InputStream is = response.getEntity().getContent();
                    int len;
                    byte[] arr = new byte[1024];
                    StringBuffer buffer = new StringBuffer();
                    while ((len = is.read(arr)) != -1) {
                        buffer.append(new String(arr, 0, len, "gbk"));
                    }
                    String json = buffer.toString();
                    System.out.println(json);
                    Gson gson = new Gson();
                    // 解析pic.json
                    Data3 data = gson.fromJson(json, Data3.class);
                    return data;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }


    }

    // 优化类
    class ViewHolder {
        ImageView iv;
        TextView title;
        TextView uptime;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4959次
    • 积分:210
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论