1. 程式人生 > >XListView解析資料及PullToRefresh解析資料 及多條目載入

XListView解析資料及PullToRefresh解析資料 及多條目載入

package com.example.week3.Fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.ListView;

import com.example.mylibrary.me.maxwin.view.XListView;
import com.example.week3.Bean.MyData;
import com.example.week3.R;
import com.example.week3.Utils.Utils;
import com.example.week3.adapter.MyBaseAdapter;
import com.google.gson.Gson;

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

public class Three extends Fragment implements XListView.IXListViewListener{
private String mUrl = “

http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
private List<MyData.DataBean> mList = new ArrayList<>();
private MyBaseAdapter baseAdapter;
private XListView xListView;
private int start=0;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View inflate = View.inflate(getActivity(), R.layout.three, null);
    initView(inflate);
    return inflate;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    baseAdapter = new MyBaseAdapter(getActivity(), mList);
    xListView.setAdapter(baseAdapter);
    new MyAsync().execute(mUrl+start);
}

private void initView(View inflate) {
    xListView = (XListView) inflate.findViewById(R.id.lv);


    xListView.setPullLoadEnable(true);
    xListView.setXListViewListener(this);


}



class MyAsync extends AsyncTask<String, Void, List<MyData.DataBean>> {

    @Override
    protected List<MyData.DataBean> doInBackground(String... strings) {
        try {
            String str = Utils.getStr(strings[0]);
            Gson gson = new Gson();
            MyData myData = gson.fromJson(str, MyData.class);
            return myData.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<MyData.DataBean> dataBeans) {
        super.onPostExecute(dataBeans);
        mList.addAll(dataBeans);
        baseAdapter.setmList(mList);
        stop();
    }
}

private void stop() {
    xListView.stopRefresh();
    xListView.stopLoadMore();
    xListView.setRefreshTime("剛剛");
}
//上拉重新整理
@Override
public void onRefresh() {
   mList.clear();
   start=1;
   new MyAsync().execute(mUrl+start);
}

//下拉載入
@Override
public void onLoadMore() {

 new MyAsync().execute(mUrl+start);
}

}
PullToRefresh解析資料
package com.example.week3.Fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
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.ListView;

import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.example.week3.Bean.MyData;
import com.example.week3.R;
import com.example.week3.Utils.Utils;
import com.example.week3.adapter.MyBaseAdapter;
import com.google.gson.Gson;

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

public class Four extends Fragment {
private PullToRefreshListView Pull_To_Re;
private String mUrl=“http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
private List<MyData.DataBean> data=new ArrayList<>();
private int start=0;
private MyBaseAdapter baseAdapter;
private View inflate;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    inflate = View.inflate(getActivity(), R.layout.four, null);
    initView(inflate);
     baseAdapter = new MyBaseAdapter(getActivity(), data);
     Pull_To_Re.setAdapter(baseAdapter);
    new Async().execute(mUrl+start);
    setListener();
    return inflate;
}

private void initView(View inflate) {
    Pull_To_Re = (PullToRefreshListView) inflate.findViewById(R.id.Pull_To_Re);
    //設定上下滑動
    Pull_To_Re.setMode(PullToRefreshBase.Mode.BOTH);
    baseAdapter = new MyBaseAdapter(getActivity(), data);
    Pull_To_Re.setAdapter(baseAdapter);

}
//開始寫邏輯程式碼

class Async extends AsyncTask<String,Void,List<MyData.DataBean>>{

  @Override
  protected List<MyData.DataBean> doInBackground(String... strings) {
      try {
          String str = Utils.getStr(strings[0]);
          Gson gson = new Gson();
          MyData myData = gson.fromJson(str, MyData.class);
          return  myData.getData();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }

    @Override
    protected void onPostExecute(List<MyData.DataBean> dataBeans) {
        super.onPostExecute(dataBeans);
        data.addAll(dataBeans);
       baseAdapter.setmList(data);
    }
}
//設定監聽事件
public void setListener(){
    Pull_To_Re.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
           start=1;
           data.clear();
          new Async().execute(mUrl);
          Pull_To_Re.postDelayed(new Runnable() {
              @Override
              public void run() {
                  Pull_To_Re.onRefreshComplete();
              }
          },1000);
        }
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
             start++;
            new Async().execute(mUrl);
            Pull_To_Re.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Pull_To_Re.onRefreshComplete();
                }
            },1000);
        }
    });
}

}
ListView多條目載入
package com.example.listviewitem;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

private ListView lv;

/* private String murl=“https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=5”;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//設定資料
List list = new ArrayList<>();
for (int i = 0;i < 20;++i){

        list.add("你是個餓貨"+i);
    }

    MyAdapter myAdapter = new MyAdapter(this,list);
    lv.setAdapter(myAdapter);
}


private void initView() {
    lv = (ListView) findViewById(R.id.lv);
}


class MyAdapter extends BaseAdapter{

    private static final int TYPE_1=0;
    private static final int TYPE_2=1;
    private List<String> list;
    private Context context;


    public MyAdapter(Context context, List<String> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public int getItemViewType(int position) {
        if (position%2==0){
            return  TYPE_1;
        }
        else {
            return TYPE_2;
        }

    }

//制定載入的條目型別個數
@Override
public int getViewTypeCount() {
return 2;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        ViewHolder2 viewHolder2=null;
        int type = getItemViewType(position);
        switch (type){
            case TYPE_1:
                if (convertView==null){
                    viewHolder = new ViewHolder();
                    convertView = View.inflate(context, R.layout.list1, null);
                    viewHolder.tvcontext1=convertView.findViewById(R.id.text);
                    convertView.setTag(viewHolder);
                }
                else{
                    viewHolder= (ViewHolder) convertView.getTag();
                }
                viewHolder.tvcontext1.setText(list.get(position));
                break;

            case  TYPE_2:

                if (convertView==null){
                    viewHolder2 = new ViewHolder2();
                    convertView = View.inflate(context, R.layout.list2, null);
                    viewHolder2.tvcontext2=convertView.findViewById(R.id.text2);
                    convertView.setTag(viewHolder2);
                }
                else{
                    viewHolder2= (ViewHolder2) convertView.getTag();
                }
                viewHolder2.tvcontext2.setText(list.get(position));
                break;
        }


        return convertView;
    }
    class  ViewHolder{
        TextView tvcontext1;
    }
    class  ViewHolder2{
        TextView tvcontext2;
    }
}

}