package com.bawie.com.shanggeyueyuekao; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.GridView; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.ILoadingLayout; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.List; public class Main2Activity extends AppCompatActivity { private int cid; private String url="http://120.27.23.105/product/getProductCatagory?cid="+cid; private PullToRefreshListView pulltoF; private Bean2 bean2; private List<Bean2.DataBean> data = new ArrayList<>(); private GridView grid_view; private TextView htext; private int operType=1; private int pageIndex=cid; private Hmyadapter hmyadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); cid = intent.getIntExtra("cid", this.cid); //list_view = findViewById(R.id.list_view); grid_view = findViewById(R.id.grid_view); pulltoF = findViewById(R.id.pulltoF); htext = findViewById(R.id.htext); initplv(); getlist(); } private void initplv() { pulltoF.setMode(PullToRefreshListView.Mode.BOTH); ILoadingLayout startLabels = pulltoF.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新");//第一次下拉时显示的文字 startLabels.setRefreshingLabel("正在拉");//下拉的过程中显示的文字 startLabels.setReleaseLabel("放开刷新");//不能在下拉时,显示的文字 ILoadingLayout endLabels = pulltoF.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); pulltoF.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { operType =1; url="http://120.27.23.105/product/getProductCatagory?cid="+cid; getlist(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { operType =2; url="http://120.27.23.105/product/getProductCatagory?cid="+cid; getlist(); } }); } private void getlist() { if(NetStateUtil.isConn(this)){ ListMyTask listMyTask=new ListMyTask(new ListMyTask.ListIcall() { @Override public void ListUpdatejson(String strjson) { Gson gson=new Gson(); Bean2 bean2 = gson.fromJson(strjson, Bean2.class); if(operType==1){ data.clear(); } data.addAll(bean2.getData()); setAdapter(); pulltoF.onRefreshComplete(); } }); listMyTask.execute("http://120.27.23.105/product/getProductCatagory?cid="+cid); }else{ NetStateUtil.showNoNetWorkDlg(this); } } private void setAdapter() { if(hmyadapter==null){ hmyadapter = new Hmyadapter(this,data); pulltoF.setAdapter(hmyadapter); }else{ hmyadapter.notifyDataSetChanged(); } }}
==========================================================================================================================================================================================================
package com.bawie.com.shanggeyueyuekao; import android.os.AsyncTask; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /** * Created by Administrator on 2018/1/12 0012. */ public class ListMyTask extends AsyncTask<String,Void,String> { private ListIcall listIcall; String str=""; public ListMyTask(ListIcall listIcall) { this.listIcall = listIcall; } @Override protected String doInBackground(String... strings) { try { URL url=new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); urlConnection.setRequestMethod("GET"); int responseCode = urlConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = urlConnection.getInputStream(); str = ListStreamToString.listStreamToString(inputStream,"utf-8"); } } catch (Exception e) { e.printStackTrace(); } return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); listIcall.ListUpdatejson(s); } public interface ListIcall{ void ListUpdatejson(String strjson); } }==========================================================================================================================================================================================================布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" xmlns:ptr="http://schemas.android.com/apk/res-auto" tools:context="com.bawie.com.shanggeyueyuekao.Main2Activity"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pulltoF" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" > </com.handmark.pulltorefresh.library.PullToRefreshListView> </RelativeLayout>
09-15
09-15