Эх сурвалжийг харах

1.修改Android Apex Mobile Home和History,完成数据加载以及下拉刷新触底加载。

Pen Li 8 жил өмнө
parent
commit
9120106f65

+ 157 - 26
Apex Mobile/app/src/main/java/com/usai/apex/mainframe/HistoryFragment.java

@@ -1,12 +1,15 @@
 package com.usai.apex.mainframe;
 
 
+import android.app.Activity;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 
 import com.usai.apex.R;
+import com.usai.apex.Result.PullRefreshListView;
+import com.usai.util.Network;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -32,6 +35,29 @@ public class HistoryFragment extends TrackingListFragment {
 //        setupAdapter();
 //        customize_style();
 
+        refresh_footer = View.inflate(mContext, R.layout.refresh_footer, null);
+        mPullRefreshListView.setRefreshFooter(refresh_footer);
+        mPullRefreshListView.setOnRefreshListener(new PullRefreshListView.OnRefreshListener() {
+            @Override
+            public void onHeaderStateChange(int state, int offset) {
+
+            }
+
+            @Override
+            public void onFooterStateChange(int state, int offset) {
+                if (state == PullRefreshListView.REFRESHING) {
+                    if (!isLoading) {
+                        loadContent(2);
+                    }
+                }
+            }
+
+            @Override
+            public void shouldPullDownToRefresh(boolean should) {
+
+            }
+        });
+
         // this.getListView().setBackgroundColor(Color.WHITE);
         requestdata();
 
@@ -39,34 +65,36 @@ public class HistoryFragment extends TrackingListFragment {
     private void requestdata()
     {
 
-        JSONObject jsobj = loadfakecontent(R.raw.fake_container_list);
-        // if (searchresult.get_fieldscount() == 0)
-        // {
-        // JSONObject objfields = jsobj.getJSONObject("fields");
-        // if (objfields != null)
-        // searchresult.init_fields(objfields.toString());
-        // }
-
+        loadContent(0);
 
-        try {
-            JSONArray datalist = jsobj.getJSONArray("container_list");
-            int count = datalist.length();
-            searchresult.add_records(datalist.toString(), count);
-
-        } catch (JSONException e) {
-            e.printStackTrace();
-        }
-
-//        int count = jsobj.getInt("total");
+//        JSONObject jsobj = loadfakecontent(R.raw.fake_container_list);
+//        // if (searchresult.get_fieldscount() == 0)
+//        // {
+//        // JSONObject objfields = jsobj.getJSONObject("fields");
+//        // if (objfields != null)
+//        // searchresult.init_fields(objfields.toString());
+//        // }
+//
+//
+//        try {
+//            JSONArray datalist = jsobj.getJSONArray("container_list");
+//            int count = datalist.length();
+//            searchresult.add_records(datalist.toString(), count);
 //
-//        if (count < limit)
-//            bfinish = true;
-//        JSONObject objrecords = jsobj.getJSONObject("records");
-//        if (objrecords != null)
-//            searchresult.add_records(objrecords.toString(), count);
-//        errorcode = Network.RESULT_TRUE;
-
-        adapter.notifyDataSetChanged();
+//        } catch (JSONException e) {
+//            e.printStackTrace();
+//        }
+//
+////        int count = jsobj.getInt("total");
+////
+////        if (count < limit)
+////            bfinish = true;
+////        JSONObject objrecords = jsobj.getJSONObject("records");
+////        if (objrecords != null)
+////            searchresult.add_records(objrecords.toString(), count);
+////        errorcode = Network.RESULT_TRUE;
+//
+//        adapter.notifyDataSetChanged();
     }
     @Override
     protected View initHeaderView()
@@ -77,4 +105,107 @@ public class HistoryFragment extends TrackingListFragment {
 
     }
 
+    @Override
+    protected void requestData() {
+
+        loadContent(1);
+    }
+
+    private static final int load_limit = 10;
+    private void loadContent(final int option) {
+
+        if (isLoading) {
+            if (mSwipeRefresh.isRefreshing()) {
+                mSwipeRefresh.setRefreshing(false);
+            }
+            return;
+        }
+
+        int offset = 0;
+        switch (option) {
+            case 0:
+            case 1: {
+                offset = 0;
+            }
+            break;
+            case 2: {
+                offset = searchresult.get_count();
+            }
+            break;
+        }
+
+        final Bundle params = new Bundle();
+        params.putInt("offset",offset);
+        params.putInt("limit",load_limit);
+
+        isLoading = true;
+        showProgressDialog(null,"Loading...");
+
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+
+                final JSONObject jsobj = Network.request_history(params);
+
+                ((Activity)mContext).runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+
+                        mPullRefreshListView.completeRefresh();
+                        progressDialog.dismiss();
+                        if (mSwipeRefresh.isRefreshing()) {
+                            mSwipeRefresh.setRefreshing(false);
+                        }
+
+                        if (jsobj != null) {
+
+                            try {
+                                int result = jsobj.getInt("result");
+                                if (result == Network.RESULT_TRUE) {
+
+                                    JSONArray datalist = jsobj.getJSONArray("container_list");
+                                    int count = datalist.length();
+
+                                    switch (option) {
+                                        case 0:
+                                        case 1: {
+                                            searchresult.clear();
+                                        }
+                                        break;
+                                        case 2: {
+
+                                        }
+                                        break;
+                                    }
+
+                                    searchresult.add_records(datalist.toString(), count);
+
+                                    adapter.notifyDataSetChanged();
+
+                                    if (count < load_limit) {
+                                        showAlert("No More Data");
+                                    }
+
+                                } else {
+                                    String msg = jsobj.getString("err_msg");
+                                    showAlert(msg);
+                                }
+                            } catch (JSONException e) {
+                                e.printStackTrace();
+                                showAlert("Sorry,there is some wrong");
+                            }
+
+                        } else {
+                            showAlert("Sorry,there is some wrong");
+                        }
+                        isLoading = false;
+                    }
+                });
+            }
+        }).start();
+
+    }
+
+
+
 }

+ 86 - 25
Apex Mobile/app/src/main/java/com/usai/apex/mainframe/HomeFragment.java

@@ -1,12 +1,14 @@
 package com.usai.apex.mainframe;
 
 
+import android.app.Activity;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 
 import com.usai.apex.R;
+import com.usai.util.Network;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -35,37 +37,96 @@ public class HomeFragment extends TrackingListFragment {
         requestdata();
 
     }
-    private void requestdata()
-    {
-
-        JSONObject jsobj = loadfakecontent(R.raw.fake_container_list);
-        // if (searchresult.get_fieldscount() == 0)
-        // {
-        // JSONObject objfields = jsobj.getJSONObject("fields");
-        // if (objfields != null)
-        // searchresult.init_fields(objfields.toString());
-        // }
 
+    @Override
+    protected void requestData() {
 
-        try {
-            JSONArray datalist = jsobj.getJSONArray("container_list");
-            int count = datalist.length();
-            searchresult.add_records(datalist.toString(), count);
+        requestdata();
+    }
 
-        } catch (JSONException e) {
-            e.printStackTrace();
+    private void requestdata()
+    {
+        if (isLoading) {
+            if (mSwipeRefresh.isRefreshing()) {
+                mSwipeRefresh.setRefreshing(false);
+            }
+            return;
         }
+        isLoading = true;
+        showProgressDialog(null,"Loading...");
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+
+                final JSONObject jsobj = Network.request_home();
+
+                ((Activity)mContext).runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+
+                        progressDialog.dismiss();
+                        if (mSwipeRefresh.isRefreshing()) {
+                            mSwipeRefresh.setRefreshing(false);
+                        }
+
+                        if (jsobj != null) {
+
+                            try {
+                                int result = jsobj.getInt("result");
+                                if (result == Network.RESULT_TRUE) {
+
+                                    JSONArray datalist = jsobj.getJSONArray("container_list");
+                                    int count = datalist.length();
+                                    searchresult.add_records(datalist.toString(), count);
+
+                                    adapter.notifyDataSetChanged();
+                                } else {
+                                    String msg = jsobj.getString("err_msg");
+                                    showAlert(msg);
+                                }
+                            } catch (JSONException e) {
+                                e.printStackTrace();
+                                showAlert("Sorry,there is some wrong");
+                            }
+
+                        } else {
+                            showAlert("Sorry,there is some wrong");
+                        }
+                        isLoading = false;
+                    }
+                });
+            }
+        }).start();
 
-//        int count = jsobj.getInt("total");
 //
-//        if (count < limit)
-//            bfinish = true;
-//        JSONObject objrecords = jsobj.getJSONObject("records");
-//        if (objrecords != null)
-//            searchresult.add_records(objrecords.toString(), count);
-//        errorcode = Network.RESULT_TRUE;
-
-        adapter.notifyDataSetChanged();
+//        JSONObject jsobj = loadfakecontent(R.raw.fake_container_list);
+//        // if (searchresult.get_fieldscount() == 0)
+//        // {
+//        // JSONObject objfields = jsobj.getJSONObject("fields");
+//        // if (objfields != null)
+//        // searchresult.init_fields(objfields.toString());
+//        // }
+//
+//
+//        try {
+//            JSONArray datalist = jsobj.getJSONArray("container_list");
+//            int count = datalist.length();
+//            searchresult.add_records(datalist.toString(), count);
+//
+//        } catch (JSONException e) {
+//            e.printStackTrace();
+//        }
+//
+////        int count = jsobj.getInt("total");
+////
+////        if (count < limit)
+////            bfinish = true;
+////        JSONObject objrecords = jsobj.getJSONObject("records");
+////        if (objrecords != null)
+////            searchresult.add_records(objrecords.toString(), count);
+////        errorcode = Network.RESULT_TRUE;
+//
+//        adapter.notifyDataSetChanged();
     }
 
     @Override

+ 141 - 0
Apex Mobile/app/src/main/java/com/usai/apex/mainframe/TrackingListFragment.java

@@ -1,18 +1,27 @@
 package com.usai.apex.mainframe;
 
 
+import android.app.ProgressDialog;
 import android.content.Context;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.ListFragment;
+import android.support.v4.widget.SwipeRefreshLayout;
+import android.support.v7.app.AlertDialog;
+import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.ListView;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import com.usai.apex.R;
+import com.usai.apex.Result.PullRefreshListView;
 import com.usai.util.commonUtil;
 
 import org.apache.http.util.EncodingUtils;
@@ -122,6 +131,10 @@ public class TrackingListFragment extends ListFragment {
 
             return datalist;
         }
+
+        public void clear() {
+            datalist.clear();
+        }
     }
     private class TrackingAdapter extends BaseAdapter
     {
@@ -250,6 +263,9 @@ public class TrackingListFragment extends ListFragment {
     {
         super.onActivityCreated(savedInstanceState);
 
+        progressDialog = new ProgressDialog(mContext);
+        progressDialog.setCancelable(false);
+
         setupAdapter();
         customize_style();
 
@@ -340,4 +356,129 @@ public class TrackingListFragment extends ListFragment {
     {
         return null;
     }
+
+    ProgressDialog progressDialog;
+    boolean isLoading = false;//表示是否正处于加载状态
+    Context mContext;
+    SwipeRefreshLayout mSwipeRefresh;
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        mContext = context;
+    }
+
+    protected void showProgressDialog(String title, String msg) {
+        if (!getUserVisibleHint()) {
+            return;
+        }
+
+        progressDialog.setTitle(title);
+        progressDialog.setMessage(msg);
+        progressDialog.show();
+    }
+
+    public void showAlert(String msg) {
+
+        if (!getUserVisibleHint()) {
+            return;
+        }
+
+        if (msg == null || msg.isEmpty()) {
+            return;
+        }
+
+        new AlertDialog.Builder(mContext)
+                .setMessage(msg)
+                .show();
+    }
+
+    static final int INTERNAL_EMPTY_ID = 0x00ff0001;
+    static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002;
+    static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003;
+
+    protected PullRefreshListView mPullRefreshListView;
+    protected View refresh_footer;
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        final Context context = getContext();
+
+        FrameLayout root = new FrameLayout(context);
+
+        // ------------------------------------------------------------------
+
+        LinearLayout pframe = new LinearLayout(context);
+        pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
+        pframe.setOrientation(LinearLayout.VERTICAL);
+        pframe.setVisibility(View.GONE);
+        pframe.setGravity(Gravity.CENTER);
+
+        ProgressBar progress = new ProgressBar(context, null,
+                android.R.attr.progressBarStyleLarge);
+        pframe.addView(progress, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
+
+        root.addView(pframe, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+        // ------------------------------------------------------------------
+
+        FrameLayout lframe = new FrameLayout(context);
+        lframe.setId(INTERNAL_LIST_CONTAINER_ID);
+
+        TextView tv = new TextView(context);
+        tv.setId(INTERNAL_EMPTY_ID);
+        tv.setGravity(Gravity.CENTER);
+        lframe.addView(tv, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+
+        // Swipe
+        SwipeRefreshLayout swipeRefreshLayout = new SwipeRefreshLayout(context);
+        mSwipeRefresh = swipeRefreshLayout;
+        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
+            @Override
+            public void onRefresh() {
+                requestData();
+            }
+        });
+
+        PullRefreshListView lv = new PullRefreshListView(context);
+        mPullRefreshListView = lv;
+        lv.setId(android.R.id.list);
+        lv.setDrawSelectorOnTop(false);
+
+        swipeRefreshLayout.addView(lv, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+        lframe.addView(swipeRefreshLayout, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+        root.addView(lframe, new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+        // ------------------------------------------------------------------
+
+        root.setLayoutParams(new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
+
+        return root;
+    }
+
+    @Override
+    public void setUserVisibleHint(boolean isVisibleToUser) {
+        super.setUserVisibleHint(isVisibleToUser);
+
+        if (isVisibleToUser) {
+
+        } else {
+
+        }
+    }
+
+    protected void requestData() {
+
+    }
 }

+ 71 - 4
Apex Mobile/app/src/main/java/com/usai/util/Network.java

@@ -93,7 +93,7 @@ public class Network
 	public static int			AP_MESSAGE_NONE					= 6;
 	//
 	public static final int		RESULT_FALSE					= 0;
-	public static final int		RESULT_TRUE						= -1;
+	public static final int		RESULT_TRUE						= 2;
 	public static final int		RESULT_NET_ERROR				= -3;
 	public static final int		RESULT_NET_NOTAVAILABLE			= -4;
 	public static final int		RESULT_ERROR					= -5;
@@ -103,6 +103,9 @@ public class Network
 	public static final int		RESULT_SESSION_EXPIRED			= -13;
 	public static final int		RESULT_VER_LOW					= -15;
 
+	public static String		URL_HOME				= "https://ra.apexshipping.com/main_new.php";
+	public static String		URL_HISTORY				= "https://ra.apexshipping.com/main_new.php";
+
 	public static String		URL_UPDATE_AUTH					= "https://ra.apexshipping.com/login.php";
 	public static String		URL_REQUEST_COUNT				= "https://ra.apexshipping.com/main_new.php";
 	public static String		URL_REQUEST_RECORDS				= "https://ra.apexshipping.com/main.php";
@@ -1573,17 +1576,81 @@ public class Network
 		params.putString("action","handset_search");
 
 		String jstr = getJson(Network.URL_REQUEST_COUNT, params);
-		if (jstr == null || jstr.length() <= 0)
-		{
-			return null;
+		try {
+
+			if (jstr == null || jstr.length() <= 0)
+			{
+				JSONObject resultJson = new JSONObject();
+				resultJson.put("err_msg","Net Error. Cannot connect to server for now. Please retry later.");
+				resultJson.put("result",RESULT_NET_ERROR);
+				return resultJson;
+			}
+
+			JSONObject jsobj = new JSONObject(jstr);
+			return jsobj;
+
+		} catch (JSONException e) {
+			e.printStackTrace();
 		}
 
+		return null;
+	}
+
+
+	public static JSONObject request_home() {
+
+		Bundle params = new Bundle();
+		params.putString("action","hand_new_home_list");
+
+		String jstr = getJson(URL_HOME,params);
+		Log.d("Network", "request_home: " + jstr);
 		try {
+
+			if (jstr == null || jstr.length() <= 0)
+			{
+				JSONObject resultJson = new JSONObject();
+				resultJson.put("err_msg","Net Error. Cannot connect to server for now. Please retry later.");
+				resultJson.put("result",RESULT_NET_ERROR);
+				return resultJson;
+			}
+
 			JSONObject jsobj = new JSONObject(jstr);
 			return jsobj;
+
 		} catch (JSONException e) {
 			e.printStackTrace();
 		}
+
 		return null;
 	}
+
+	public static JSONObject request_history(Bundle params) {
+
+		if (params == null) {
+			params = new Bundle();
+		}
+		params.putString("action","hand_new_home_list_all");
+
+		String jstr = getJson(URL_HISTORY,params);
+		Log.d("Network", "request_home: " + jstr);
+		try {
+
+			if (jstr == null || jstr.length() <= 0)
+			{
+				JSONObject resultJson = new JSONObject();
+				resultJson.put("err_msg","Net Error. Cannot connect to server for now. Please retry later.");
+				resultJson.put("result",RESULT_NET_ERROR);
+				return resultJson;
+			}
+
+			JSONObject jsobj = new JSONObject(jstr);
+			return jsobj;
+
+		} catch (JSONException e) {
+			e.printStackTrace();
+		}
+
+		return null;
+	}
+
 }