需要用到的lib包 :解析json gson包,從網路地址解析json數據成String字元串的非同步網路解析工具AsyncHttpClient等下載地址:點擊下載Xlistview 下拉上拉第三方框架 點擊下載側滑菜單的lib 點擊下載 1 package com.lixu.testjsonall;...
需要用到的lib包 :解析json gson包,從網路地址解析json數據成String字元串的非同步網路解析工具AsyncHttpClient等
下載地址:點擊下載
Xlistview 下拉上拉第三方框架 點擊下載
側滑菜單的lib 點擊下載
1 package com.lixu.testjsonall; 2 3 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; 4 import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity; 5 import com.lixu.testjsonall.fragments.Cehua_Fragment; 6 import com.lixu.testjsonall.fragments.Fragment_one; 7 import com.lixu.testjsonall.fragments.Fragment_tiyuxinwen; 8 import com.lixu.testjsonall.fragments.Fragment_yulexinwen; 9 import android.os.Bundle; 10 import android.view.Window; 11 12 // 實現三個Fragment的介面類來實現回調 13 public class MainActivity extends SlidingActivity implements Cehua_Fragment.SetonCehuaFragmentClickListener, 14 Fragment_one.SetonFragmentOneClickListener, Fragment_tiyuxinwen.SetonFragmentTiyuxinwenClickListener, 15 Fragment_yulexinwen.SetonFragmentYulexinwenClickListener { 16 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 requestWindowFeature(Window.FEATURE_NO_TITLE); 21 setContentView(R.layout.activity_main); 22 23 getFragmentManager().beginTransaction().add(R.id.main_framelayout, Fragment_one.newFragment()).commit(); 24 // 設置滑出菜單為demo中轉樣式 25 setBehindContentView(R.layout.demo); 26 // 將中轉demo佈局替換為自定義滑出菜單樣式 27 getFragmentManager().beginTransaction().add(R.id.demo, Cehua_Fragment.newFragment()).commit(); 28 // 獲取菜單滑出菜單實例 29 SlidingMenu slidingMenu = getSlidingMenu(); 30 // 設置滑出菜單的停止寬度 31 slidingMenu.setBehindOffsetRes(R.dimen.weight); 32 33 } 34 35 @Override 36 public void loadxinwen() { 37 getFragmentManager().beginTransaction().replace(R.id.main_framelayout, Fragment_one.newFragment()).commit(); 38 // 添加界面後關閉側滑按鈕框 39 toggle(); 40 } 41 42 @Override 43 public void loadyulexinwen() { 44 getFragmentManager().beginTransaction().replace(R.id.main_framelayout, Fragment_yulexinwen.newFragment()) 45 .commit(); 46 toggle(); 47 } 48 49 @Override 50 public void loadtiyuxinwen() { 51 getFragmentManager().beginTransaction().replace(R.id.main_framelayout, Fragment_tiyuxinwen.newFragment()) 52 .commit(); 53 toggle(); 54 } 55 56 @Override 57 public void back() { 58 toggle(); 59 60 } 61 62 }
1 package com.lixu.testjsonall.fragments; 2 3 import com.lixu.testjsonall.R; 4 import android.app.Activity; 5 import android.app.Fragment; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 12 public class Cehua_Fragment extends Fragment { 13 // 設置回調機制 14 private SetonCehuaFragmentClickListener mSetonCehuaFragmentClickListener; 15 16 public interface SetonCehuaFragmentClickListener { 17 void loadxinwen(); 18 19 void loadyulexinwen(); 20 21 void loadtiyuxinwen(); 22 } 23 24 @Override 25 public void onAttach(Activity activity) { 26 super.onAttach(activity); 27 if (activity instanceof SetonCehuaFragmentClickListener) { 28 mSetonCehuaFragmentClickListener = (SetonCehuaFragmentClickListener) activity; 29 } else { 30 throw new RuntimeException(activity.toString() + " must implement 介面"); 31 } 32 } 33 34 @Override 35 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 36 View view = inflater.inflate(R.layout.cehua_menu, null); 37 return view; 38 } 39 40 @Override 41 public void onActivityCreated(Bundle savedInstanceState) { 42 getView().findViewById(R.id.monixinweijiem).setOnClickListener(new OnClickListener() { 43 44 @Override 45 public void onClick(View v) { 46 mSetonCehuaFragmentClickListener.loadxinwen(); 47 } 48 }); 49 getView().findViewById(R.id.yulexinwen).setOnClickListener(new OnClickListener() { 50 51 @Override 52 public void onClick(View v) { 53 mSetonCehuaFragmentClickListener.loadyulexinwen(); 54 } 55 }); 56 getView().findViewById(R.id.tiyuxinwen).setOnClickListener(new OnClickListener() { 57 58 @Override 59 public void onClick(View v) { 60 mSetonCehuaFragmentClickListener.loadtiyuxinwen(); 61 } 62 }); 63 64 super.onActivityCreated(savedInstanceState); 65 } 66 67 public static Cehua_Fragment newFragment() { 68 Cehua_Fragment f = new Cehua_Fragment(); 69 return f; 70 } 71 }
1 package com.lixu.testjsonall.fragments; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import com.google.gson.Gson; 7 import com.lixu.testjsonall.R; 8 import com.lixu.testjsonall.json_info.Info; 9 import com.lixu.testjsonall.json_info.Infoall; 10 import com.lixu.testjsonall.json_info.MerchantKey; 11 import com.lixu.testjsonall.untils.Untils; 12 import com.loopj.android.http.AsyncHttpClient; 13 import com.loopj.android.http.TextHttpResponseHandler; 14 import com.scxh.slider.library.SliderLayout; 15 import com.scxh.slider.library.SliderTypes.BaseSliderView; 16 import com.scxh.slider.library.SliderTypes.TextSliderView; 17 import com.squareup.picasso.Picasso; 18 import android.app.Activity; 19 import android.app.Fragment; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.view.ViewGroup; 26 import android.widget.ArrayAdapter; 27 import android.widget.ImageView; 28 import android.widget.ProgressBar; 29 import android.widget.TextView; 30 import android.widget.Toast; 31 import cz.msebera.android.httpclient.Header; 32 import me.maxwin.view.XListView; 33 import me.maxwin.view.XListView.IXListViewListener; 34 35 public class Fragment_one extends Fragment { 36 private String net_url = "http://192.168.1.124:8080/json/around"; 37 private MyAdapter mMyAdapter; 38 private ProgressBar mProgressBar; 39 private SliderLayout mSliderLayout; 40 private XListView lv; 41 // 兩種佈局指針 42 private static final int TYPE_ONE = 0; 43 private static final int TYPE_TWO = 1; 44 45 // 設置回調機制 46 private SetonFragmentOneClickListener mSetonFragmentOneClickListener; 47 48 public interface SetonFragmentOneClickListener { 49 void back(); 50 51 } 52 53 @Override 54 public void onAttach(Activity activity) { 55 super.onAttach(activity); 56 if (activity instanceof SetonFragmentOneClickListener) { 57 mSetonFragmentOneClickListener = (SetonFragmentOneClickListener) activity; 58 } else { 59 throw new RuntimeException(activity.toString() + " must implement 介面"); 60 } 61 } 62 63 public static Fragment newFragment() { 64 Fragment_one f = new Fragment_one(); 65 return f; 66 } 67 68 @Override 69 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 70 View view = inflater.inflate(R.layout.fragment_one, null); 71 return view; 72 } 73 74 @Override 75 public void onActivityCreated(Bundle savedInstanceState) { 76 lv = (XListView) getView().findViewById(R.id.xlv); 77 mProgressBar = (ProgressBar) getView().findViewById(R.id.progressbar); 78 // 找到返回圖片按鈕 通過回調機制點擊設置返回 79 getView().findViewById(R.id.fanhui).setOnClickListener(new OnClickListener() { 80 81 @Override 82 public void onClick(View v) { 83 mSetonFragmentOneClickListener.back(); 84 85 } 86 }); 87 88 mMyAdapter = new MyAdapter(getActivity(), -1); 89 90 View view = LayoutInflater.from(getActivity()).inflate(R.layout.listhead, null); 91 mSliderLayout = (SliderLayout) view.findViewById(R.id.sliderlayout); 92 // 將圖片坐標點移動到佈局右下角 93 mSliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Right_Bottom); 94 // 添加到listview頭部 95 lv.addHeaderView(view); 96 97 // 遍歷hashmap的方法 98 HashMap<String, String> map = Untils.getData(); 99 for (String n : map.keySet()) { 100 String key = n; 101 String txt = map.get(key); 102 TextSliderView tsv = new TextSliderView(getActivity()); 103 tsv.description(key); 104 tsv.image(txt); 105 106 // 將滾動圖片根據邊框大小居中按正常圖片剪切顯示 107 tsv.setScaleType(BaseSliderView.ScaleType.CenterCrop); 108 mSliderLayout.addSlider(tsv); 109 } 110 111 // 數據載入前顯示進度轉動條 112 lv.setEmptyView(mProgressBar); 113 lv.setAdapter(mMyAdapter); 114 // 初始獲取數據 115 GetAsynchttpdata(); 116 // 設置上拉刷新 117 lv.setPullLoadEnable(true); 118 lv.setXListViewListener(new IXListViewListener() { 119 120 @Override 121 public void onRefresh() { 122 // 上拉獲取數據 123 GetAsynchttpdata(); 124 // 設置刷新時間 125 lv.setRefreshTime(Untils.formatTimeInMillis(System.currentTimeMillis())); 126 } 127 128 @Override 129 public void onLoadMore() { 130 // 下拉獲取數據 131 GetAsynchttpdata(); 132 } 133 }); 134 super.onActivityCreated(savedInstanceState); 135 } 136 137 // 第三方獲取網路數據類 138 private void GetAsynchttpdata() { 139 AsyncHttpClient ahc = new AsyncHttpClient(); 140 ahc.get(net_url, new TextHttpResponseHandler() { 141 // 通過網路地址解析Json數據成String類型 142 @Override 143 public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) { 144 Toast.makeText(getActivity(), "錯誤!", 0).show(); 145 146 } 147 148 @Override 149 public void onSuccess(int arg0, Header[] arg1, String arg2) { 150 // 從字元串中解析json文件 151 Gson gson = new Gson(); 152 Infoall infoall = gson.fromJson(arg2, Infoall.class); 153 Info info = infoall.getInfo(); 154 List<MerchantKey> mMerchantKey = info.getMerchantKey(); 155 mMyAdapter.setList(mMerchantKey); 156 // 刷新完畢後停止上拉和下拉刷新圖標顯示 157 lv.stopRefresh(); 158 lv.stopLoadMore(); 159 160 } 161 }); 162 163 } 164 165 private class MyAdapter extends ArrayAdapter { 166 167 private LayoutInflater flater; 168 private List<MerchantKey> data = new ArrayList<MerchantKey>(); 169 private Context context; 170 171 public MyAdapter(Context context, int resource) { 172 super(context, resource); 173 this.context = context; 174 flater = LayoutInflater.from(context); 175 } 176 177 public void setList(List<MerchantKey> data) { 178 this.data = data; 179 mMyAdapter.notifyDataSetChanged(); 180 } 181 182 // 第一種佈局 183 public View getViewOne(int position, View convertView, ViewGroup parent) { 184 185 Viewdata mviewdata = null; 186 if (convertView == null) { 187 // 一級優化,view可以復用緩存 188 mviewdata = new Viewdata(); 189 convertView = flater.inflate(R.layout.list, null); 190 mviewdata.biaoti = (TextView) convertView.findViewById(R.id.list_name_txt); 191 mviewdata.biaoti2 = (TextView) convertView.findViewById(R.id.list_coupe_txt); 192 mviewdata.dizhi = (TextView) convertView.findViewById(R.id.list_location_txt); 193 mviewdata.juli = (TextView) convertView.findViewById(R.id.list_distance_txt); 194 mviewdata.jpg = (ImageView) convertView.findViewById(R.id.list_icon_img); 195 mviewdata.tuan = (ImageView) convertView.findViewById(R.id.list_group_img); 196 mviewdata.quan = (ImageView) convertView.findViewById(R.id.list_ticket_img); 197 mviewdata.ka = (ImageView) convertView.findViewById(R.id.list_card_img); 198 convertView.setTag(mviewdata); 199 } 200 // 二級優化 ,只找到一次控制項,不會每次都通過R文件找到控制項 201 mviewdata = (Viewdata) convertView.getTag(); 202 mviewdata.biaoti.setText(data.get(position).getName()); 203 mviewdata.biaoti2.setText(data.get(position).getCoupon()); 204 mviewdata.dizhi.setText(data.get(position).getLocation()); 205 mviewdata.juli.setText(data.get(position).getDistance()); 206 Picasso.with(context).load(data.get(position).getPicUrl()).into(mviewdata.jpg); 207 208 if (data.get(position).getGroupType().equals("YES")) { 209 mviewdata.tuan.setVisibility(View.VISIBLE); 210 } else { 211 mviewdata.tuan.setVisibility(View.GONE); 212 } 213 if (data.get(position).getCardType().equals("YES")) { 214 mviewdata.ka.setVisibility(View.VISIBLE); 215 } else { 216 mviewdata.ka.setVisibility(View.GONE); 217 } 218 if (data.get(position).getCouponType().equals("YES")) { 219 mviewdata.quan.setVisibility(View.VISIBLE); 220 } else { 221 mviewdata.quan.setVisibility(View.GONE); 222 } 223 224 return convertView; 225 226 } 227 228 // 第二種佈局 229 public View getViewTwo(int position, View convertView, ViewGroup parent) { 230 231 Viewdata2 mviewdata2 = null; 232 if (convertView == null) { 233 // 一級優化,view可以復用緩存 234 mviewdata2 = new Viewdata2(); 235 convertView = flater.inflate(R.layout.list2, null); 236 mviewdata2.biaoti2 = (TextView) convertView.findViewById(R.id.list_name_txt2); 237 mviewdata2.biaoti22 = (TextView) convertView.findViewById(R.id.list_coupe_txt2); 238 mviewdata2.juli2 = (TextView) convertView.findViewById(R.id.list_distance2_txt); 239 mviewdata2.jpg2 = (ImageView) convertView.findViewById(R.id.list_icon_img2); 240 convertView.setTag(mviewdata2); 241 } 242 // 二級優化 ,只找到一次控制項,不會每次都通過R文件找到控制項 243 mviewdata2 = (Viewdata2) convertView.getTag(); 244 mviewdata2.biaoti2.setText(data.get(position).getName()); 245 mviewdata2.biaoti22.setText(data.get(position).getCoupon()); 246 mviewdata2.juli2.setText(data.get(position).getDistance()); 247 Picasso.with(context).load(data.get(position).getPicUrl()).into(mviewdata2.jpg2); 248 249 return convertView; 250 251 } 252 253 // 控制項類 254 private class Viewdata { 255 TextView biaoti; 256 TextView biaoti2; 257 TextView dizhi; 258 TextView juli; 259 ImageView jpg; 260 ImageView tuan; 261 ImageView quan; 262 ImageView ka; 263 264 } 265 266 // 第二個佈局的控制項類 267 private class Viewdata2 { 268 TextView biaoti2; 269 TextView biaoti22; 270 TextView juli2; 271 ImageView jpg2; 272 273 } 274 275 @Override 276 public View getView(int position, View convertView, ViewGroup parent) { 277 if (getItemViewType(position) == TYPE_ONE) { 278 return getViewOne(position, convertView, parent); 279 } else { 280 return getViewTwo(position, convertView, parent); 281 } 282 } 283 284 @Override 285 public int getItemViewType(int position) { 286 // 設置對大小寫不敏感 --equalsIgnoreCase 287 if (data.get(position).getCouponType().equalsIgnoreCase("YES")) { 288 return TYPE_ONE; 289 290 } else { 291 return TYPE_TWO; 292 } 293 294 } 295 296 // 獲取返回view的兩種類型 不寫這個方法容易報錯誤。。 297 @Override 298 public int getViewTypeCount() { 299 return 2; 300 } 301 302 @Override 303 public int getCount() { 304 305 return data.size(); 306 } 307 308 } 309 310 // 程式退出時銷毀 311 @Override 312 public void onDestroy() { 313 super.onDestroy(); 314 if (mSliderLayout != null) { 315 mSliderLayout.stopAutoCycle(); 316 mSliderLayout = null; 317 } 318 } 319 320 }
1 package com.lixu.testjsonall.fragments; 2 3 import com.lixu.testjsonall.R; 4 import android.app.Activity; 5 import android.app.Fragment; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 12 public class Fragment_tiyuxinwen extends Fragment { 13 // 設置回調機制 14 private SetonFragmentTiyuxinwenClickListener mSetonFragmentTiyuxinwenClickListener; 15 16 public interface SetonFragmentTiyuxinwenClickListener { 17 void back(); 18 19 } 20 21 @Override 22 public void onAttach(Activity activity) { 23 super.onAttach(activity); 24 if (activity instanceof SetonFragmentTiyuxinwenClickListener) { 25 mSetonFragmentTiyuxinwenClickListener = (SetonFragmentTiyuxinwenClickListener) activity; 26 } else { 27 throw new RuntimeException(activity.toString() + " must implement 介面"); 28 } 29 } 30 31 public static Fragment newFragment() { 32 Fragment_tiyuxinwen f = new Fragment_tiyuxinwen(); 33 return f; 34 } 35 36 @Override 37 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 38 return inflater.inflate(R.layout.yulexinwen, null); 39 } 40 41 @Override 42 public void onActivityCreated(Bundle savedInstanceState) { 43 super.onActivityCreated(savedInstanceState); 44 getView().findViewById(R.id.fanhui).setOnClickListener(new OnClickLis