pulltorefishlistview 联网解析

pulllistview

<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/plv"></com.handmark.pulltorefresh.library.PullToRefreshListView>

list_item

<ImageView
    android:id="@+id/imageView"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@mipmap/ic_launcher"
    />
<TextView
    android:id="@+id/itemtextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="测试数据"
    android:textSize="20sp"        />

MyApp

public void onCreate() {
    super.onCreate();    //初始化组件
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
    ImageLoader.getInstance().init(configuration);}

shouFrag

private ViewPager viewpager;
private TabLayout tablayout;
private String[] titles;
private ArrayList<TextView> tittlesView;
private ArrayList<Fragment> flist = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.shou_frag, container, false);
    viewpager = view.findViewById(R.id.viewPager);
    tablayout = view.findViewById(R.id.tablayout);
    titles = new String[]{"美女写真","新闻资讯","军事科技"};
    flist.add(new Fragment01());
    flist.add(new Fragment02());
    flist.add(new Fragment03());
    viewpager.setAdapter(new MyAdapter(getChildFragmentManager()));
    tablayout.setupWithViewPager(viewpager);
    return view;
}

@Override
public void onClick(View v) {
    int position = v.getId(); //id是数组中的下标;
    viewpager.setCurrentItem(position); //viewpager 自动换页;
}
private class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return flist.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

Fragment01

private PullToRefreshListView plv;
private MyAdapter myAdapter;
String url = "http://api.tianapi.com/meinv/?key=3304a8bc9414f97e30928b80163cf098&num=10";
ArrayList<Girl.NewslistBean> glist = new ArrayList<Girl.NewslistBean>();
private ImageLoader imageLoaderInstances;
private int page;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if(msg.what==1){
            String result = (String) msg.obj;
            Gson gson = new Gson();
            //Shuju shuju = gson.fromJson(result, Shuju.class);
            Girl girl = gson.fromJson(result, Girl.class);
            List<Girl.NewslistBean> newslist = girl.getNewslist();
            //List<Shuju.DataBean> data = shuju.getData();
            glist.addAll(newslist);
            myAdapter.notifyDataSetChanged();
            //mAdapter.notifyDataSetChanged();
            plv.onRefreshComplete();
        }
    }
};

public static Fragment getInstance(String title) {
    Fragment01 mFragment = new Fragment01();
    Bundle bundle = new Bundle();
    bundle.putString("title", title);
    mFragment.setArguments(bundle);
    return mFragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.pull_list, container, false);
    plv = view.findViewById(R.id.plv);

    plv.setMode(PullToRefreshBase.Mode.BOTH);
    imageLoaderInstances = ImageLoader.getInstance();
    myAdapter = new MyAdapter();
    plv.setAdapter(myAdapter);
    plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        /*
            下拉刷新
         */
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
            page = 0;
            glist.clear();
            initdata(page);
        }
        /*
            上拉加载
         */
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
            page++;
            initdata(page);
        }
    });
    boolean conn = NetStateUtils.isConn(getContext());
    if(conn){
        //有网
        Toast.makeText(getActivity(),"有网",Toast.LENGTH_LONG).show();
        initdata(0);
    }else{
        //无网
        Toast.makeText(getActivity(),"无网",Toast.LENGTH_LONG).show();
    }
    return view;
}
/*
    网络判断
 */
private void initdata(int page) {
    final String s = url+page;
    new Thread(){
        @Override
        public void run() {
            super.run();
            String string = new NetUtill().getString(s);
            Message message = new Message();
            message.obj = string;
            message.what = 1;
            handler.sendMessage(message);
        }
    }.start();
}

private class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return glist.size();
    }

    @Override
    public Object getItem(int position) {
        return glist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh ;
        if (convertView == null) {
            convertView = View.inflate(getActivity(), R.layout.list_item, null);
            vh = new ViewHolder();
            vh.tv = (TextView) convertView.findViewById(R.id.itemtextView);
            vh.iv = (ImageView)convertView.findViewById(R.id.imageView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //设置数据
        String news_title = glist.get(position).getTitle();
        ImageLoader instance = ImageLoader.getInstance();
        instance.displayImage(glist.get(position).getPicUrl(),vh.iv);
        vh.tv.setText(news_title);
        return convertView;
    }
}

class ViewHolder {
    public ImageView iv;
    TextView tv;
}

联网工具类

NetUtill

public String getString(String string) {
    //1.Httpurlconnection进行联网请求
    try {
        try {
            URL url = new URL(string);
            HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200){
                //可以开始请求
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String temps="";
                StringBuilder stringBuilder = new StringBuilder();
                while ((temps =bufferedReader.readLine())!=null){
                    stringBuilder.append(temps);
                }
                return stringBuilder.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

NetStateUtil

public static boolean isConn(Context context){
    // 定义一个网络
    boolean bisConnFlag = false;
    // 网络连接管理类
    ConnectivityManager connManger = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo network = connManger.getActiveNetworkInfo();
    //  判断网络状态
    if(network!=null){
        bisConnFlag = network.isAvailable();
    }
    return bisConnFlag;
}
public static  int getNetType(Context context) {
    //定义无网络
    int mState = -1;//无网络
    // 网络连接管理类
    ConnectivityManager connManger = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connManger.getActiveNetworkInfo();
    //判断网络状态
    if (networkInfo == null) {
        return mState;
    }
    //获取网络状态
    int type = networkInfo.getType();
    //判断WIFI网络
    if (type == ConnectivityManager.TYPE_WIFI) {
        mState = 1;
        return mState;
    } else if (type == ConnectivityManager.TYPE_MOBILE) {
        //判断手机网络
        mState = 0;
        return mState;
    }
    return mState;
}

shou_frag

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tablayout"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值