1. 程式人生 > >PullToRefreshListview 重新整理資料(只加載text文件 沒有圖片)

PullToRefreshListview 重新整理資料(只加載text文件 沒有圖片)

第一步

匯入Android-PullToRefresh中的library包中的資料

//mainactivity函式

package com.example.pullshuaixn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class MainActivity extends AppCompatActivity {

    private PullToRefreshListView ptrListView;
    private int mPage=1;
    private ContentAdapter mAdapter;


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

        ptrListView = findViewById( R.id.content);
        mAdapter = new ContentAdapter( this );
        ptrListView.setAdapter( mAdapter );

        //同時支援重新整理  載入
        ptrListView.setMode( PullToRefreshBase.Mode.BOTH );
        ptrListView.setOnRefreshListener( new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPage = 1;
                initData();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                initData();
            }
        } );
        initData();
    }
    public String url= "http://api.expoon.com/AppNews/getNewsList/type/1/p/";

    private  void initData(){
        NetUtil.getNetUtil().requsetData( url + mPage, Bean.class, new NetUtil.CallBak<Bean>() {
            @Override
            public void onsuccess(Bean bean) {
                if (mPage == 1){
                    mAdapter.setDataBeans( bean.getData() );
                }else {
                    mAdapter.addString( bean.getData() );
                }
                mPage++;
                ptrListView.onRefreshComplete();
            }
        } );
    }
}

//Bean類

ALT + S 解析資料

//adapter資料

package com.example.pullshuaixn;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ContentAdapter extends BaseAdapter {

    private Context context;
    private List<Bean.DataBean> dataBeans;

    public ContentAdapter(Context context) {
        this.context = context;
        dataBeans = new ArrayList<>(  );
    }

    public void setDataBeans(List<Bean.DataBean> mBeans) {
        //this.dataBeans = dataBeans;
        dataBeans.clear();
        if (dataBeans != null){
            dataBeans.addAll( mBeans );
        }
        notifyDataSetChanged();
    }

    public void  addString(List<Bean.DataBean> mBeans){
        if (dataBeans != null){
            dataBeans.addAll( mBeans );
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataBeans.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            holder = new ViewHolder(convertView);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(dataBeans.get( position ).getNews_title());
        return convertView;
    }
    class ViewHolder{
        TextView textView;
        protected  View convertView;
        public ViewHolder(View convertView) {
            textView = convertView.findViewById( R.id.text_ID );
            convertView.setTag( this );
        }
    }
}

//子佈局

根據需求自定義子佈局

//main 主要佈局

	<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


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

</LinearLayout>

//工具類

package com.example.pullshuaixn;

import android.annotation.SuppressLint;
import android.os.AsyncTask;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.security.auth.callback.Callback;

public class NetUtil {

    private static NetUtil netUtil;

    public  NetUtil(){

    }

    public static NetUtil getNetUtil(){
        if (netUtil == null){
            netUtil = new NetUtil();
        }
        return  netUtil;
    }

    public interface  CallBak<T>{
        void  onsuccess(T t);
    }

    @SuppressLint("StaticFieldLeak")
    public  void requsetData(String strurl, final Class clazz, final CallBak callback){
        new AsyncTask<String, Void, Object>() {
            @Override
            protected Object doInBackground(String... strings) {
                return requsetData( strings[0],clazz );
            }

            @Override
            protected void onPostExecute(Object o) {
                callback.onsuccess( o );
            }
        }.execute( strurl );
    }


    private <T>T requsetData(String string, Class clazz) {
        return (T) new Gson().fromJson( requsetData(string),clazz );
    }

    private  String requsetData(String str){
        try {
            URL url = new URL(str);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(5000);
            int responseCode = urlConnection.getResponseCode();
            if(responseCode==200){
                String s = streamTostring(urlConnection.getInputStream());
                return s;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String streamTostring(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder=new StringBuilder();
        for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
            builder.append(tmp);
        }
        return builder.toString();
    }
}