热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

网络数据请求XListView展示

<?xmlversion"1.0"encoding"utf-8"?><LinearLayoutxmlns:android"
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_match_parent"
android:layout_match_parent"
android:orientation="vertical"
tools:cOntext="com.example.e.yuekaolianxi.MainActivity">

<RadioGroup
android:id="@+id/radioGroup"
android:layout_match_parent"
android:layout_wrap_content"
android:padding="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio01"
android:layout_0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_wrap_content"
android:button="@null"
android:textSize="20sp"
android:text="本社介绍"
/>
<RadioButton
android:id="@+id/radio02"
android:layout_0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_wrap_content"
android:button="@null"
android:textSize="20sp"
android:text="履行职责"
/>
<RadioButton
android:id="@+id/radio03"
android:layout_0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_wrap_content"
android:button="@null"
android:textSize="20sp"
android:text="自身建设"
/>
RadioGroup
>
<LinearLayout
android:layout_match_parent"
android:layout_wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<View
android:layout_0dp"
android:layout_2dp"
android:layout_weight="1"
android:id="@+id/v"
android:background="#FF0000"
>View>
LinearLayout>
<android.support.v4.view.ViewPager
android:layout_match_parent"
android:layout_match_parent"
android:layout_marginTop="5dp"
android:id="@+id/viewPager"
>android.support.v4.view.ViewPager>
LinearLayout>


xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_match_parent"
android:layout_match_parent">
<me.maxwin.view.XListView
android:layout_match_parent"
android:layout_match_parent"
android:id="@+id/xListView">me.maxwin.view.XListView
>

LinearLayout>


xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_match_parent"    android:layout_match_parent">    <LinearLayout        android:layout_match_parent"        android:layout_wrap_content"        android:layout_centerInParent="true"        android:orientation="horizontal">        <ImageView            android:id="@+id/image"            android:layout_120dp"            android:layout_100dp"            android:padding="5dp" />        <LinearLayout            android:layout_wrap_content"            android:layout_wrap_content"            android:orientation="vertical">            <TextView                android:id="@+id/title"                android:layout_wrap_content"                android:layout_wrap_content"                android:padding="5dp"                android:text="深港澳台千里连线,嘉年华会今夏入川"                android:textColor="#FF0000"                android:textSize="15dp" />            <TextView                android:id="@+id/miaoshu"                android:layout_wrap_content"                android:layout_wrap_content"                android:padding="5dp"                android:text="6月17—20日,“2016成都深港澳台嘉年华会”(简称嘉年华会)将在成都世纪城国际会展中心举办。其主办方励展华博借力旗"                android:textSize="12dp" />        LinearLayout>    LinearLayout>RelativeLayout>

package com.example.e.xlist;
import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;/** * Created by e on 2017/5/26. */public class Utils { public static String parmrs(String path) { try { URL url = new URL(path); HttpURLConnection httpURLCOnnection= (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); if (httpURLConnection.getResponseCode() == 200) { InputStream in = httpURLConnection.getInputStream(); StringBuffer sb = new StringBuffer(); int len = -1; byte[] by = new byte[1024 * 4]; while ((len = in.read(by)) != -1) { sb.append(new String(by, 0, len, "utf-8")); } in.close(); httpURLConnection.disconnect(); return sb.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; }}


package com.example.e.xlist;
import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.RadioButton;import android.widget.RadioGroup;import java.util.Arrays;import java.util.List;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private ViewPager vp; private List list; private RadioGroup rg; private MyAdapter adapter; private RadioButton rb1; private RadioButton rb2; private RadioButton rb3; private View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.viewPager); rg = (RadioGroup) findViewById(R.id.radioGroup); rb1 = (RadioButton) findViewById(R.id.radio01); rb2 = (RadioButton) findViewById(R.id.radio02); rb3 = (RadioButton) findViewById(R.id.radio03); view = findViewById(R.id.v); indata(); vp.setOffscreenPageLimit(3); adapter = new MyAdapter(getSupportFragmentManager()); vp.setAdapter(adapter); rg.setOnCheckedChangeListener(this); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { int i = view.getWidth(); if (positionOffsetPixels != 0) { view.setX(i * position + positionOffsetPixels / 3); } } @Override public void onPageSelected(int position) { switch (position) { case 0: rb1.setChecked(true); break; case 1: rb2.setChecked(true); break; case 2: rb3.setChecked(true); break; default: break; } } @Override public void onPageScrollStateChanged(int state) { } }); } private void indata() { list = Arrays.asList(new Fragment1(), new Fragment2(), new Fragment3()); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int num = 0; switch (checkedId) { case R.id.radio01: num = 0; break; case R.id.radio02: num = 1; break; case R.id.radio03: num = 2; break; default: break; } if (vp.getCurrentItem()!=num){ vp.setCurrentItem(num); } } class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }}


package com.example.e.xlist;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import com.google.gson.Gson;import java.util.ArrayList;import java.util.List;import me.maxwin.view.XListView;/** * Created by e on 2017/5/28. */public class Fragment1 extends Fragment implements XListView.IXListViewListener {    private XListView listView;    private String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";    private List list;    private String s;    private MyAdapter adapter;    Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            s = msg.obj.toString();            Gson gson = new Gson();            Bean bean = gson.fromJson(s, Bean.class);            list.addAll(bean.getData());            adapter.notifyDataSetChanged();        }    };    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment1_layout, container, false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        list = new ArrayList<>();        listView = (XListView) getView().findViewById(R.id.xListView);        listView.setPullLoadEnable(true);        listView.setXListViewListener(this);        new Thread() {            @Override            public void run() {                String s = Utils.parmrs(path);                Message msg = Message.obtain();                msg.obj = s;                mHandler.sendMessage(msg);            }        }.start();        adapter = new MyAdapter();        listView.setAdapter(adapter);    }    @Override    public void onRefresh() {        mHandler.postDelayed(new Runnable() {            @Override            public void run() {                Gson gson=new Gson();                Bean bean=gson.fromJson(s,Bean.class);                list.addAll(bean.getData());                adapter.notifyDataSetChanged();                stopLoader();            }        }, 2000);    }    @Override    public void onLoadMore() {        mHandler.postDelayed(new Runnable() {            @Override            public void run() {                Gson gson=new Gson();                Bean bean=gson.fromJson(s,Bean.class);                list.addAll(bean.getData());                adapter.notifyDataSetChanged();                stopLoader();            }        }, 2000);    }    private void stopLoader() {        listView.stopRefresh();        listView.stopLoadMore();    }    class MyAdapter extends BaseAdapter {        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int position) {            return list.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            Holder holder;            if (cOnvertView==null){                holder=new Holder();                cOnvertView=View.inflate(getActivity(),R.layout.item1_layout,null);                holder.tv1= (TextView) convertView.findViewById(R.id.title);                holder.tv2= (TextView) convertView.findViewById(R.id.miaoshu);                holder.image= (ImageView) convertView.findViewById(R.id.image);                convertView.setTag(holder);            }else{                holder= (Holder) convertView.getTag();            }            Bean.DataBean bean=list.get(position);            holder.tv1.setText(bean.getNews_title());            holder.tv2.setText(bean.getNews_summary());            Glide.with(getActivity()).load(bean.getPic_url()).into(holder.image);            return convertView;        }    }    class Holder{        TextView tv1,tv2;        ImageView image;    }}


 

推荐阅读
author-avatar
若v0丷會飛的獨角獸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有