加入依賴:imageloader(圖片) gson(網絡)
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.google.code.gson:gson:2.2.4'
------------------------------------------------------------------
佈局:java
<com.bawei.jane.mxlistview.view.XListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/xlv"/>
============================================================android
主要方法:json
import android.os.AsyncTask; import android.os.Bundle; 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.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bawei.jane.mxlistview.view.XListView; import com.example.shenhuiran_0103.Goods; import com.example.shenhuiran_0103.NetWordUtils; import com.example.shenhuiran_0103.R; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; public class Main4Fragment extends Fragment implements XListView.IXListViewListener { //http://api.expoon.com/AppNews/getNewsList/type/1 String url = "http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603"; String baseUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/"; int page; List<Goods.DataBean> list = new ArrayList<>(); //大集合 private MAdapter mAdapter; private ImageLoader imageLoaderInstances; private XListView xListView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.fragment_d, null); xListView = view.findViewById(R.id.xlv); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); imageLoaderInstances = ImageLoader.getInstance(); mAdapter = new MAdapter(); xListView.setAdapter(mAdapter); xListView.setPullLoadEnable(true); //容許上拉加載更多; xListView.setXListViewListener(this); //設置(上下拉的)監聽 設置(上下拉的)監聽 設置(上下拉的)監聽 getNetData(0); } //請求網絡 private void getNetData(int page) { new MAsyncTask().execute(baseUrl+page); } //下拉刷新新數據; @Override public void onRefresh() { list.clear(); getNetData(0); } //上拉加載更多 @Override public void onLoadMore() { //大集合中添加新數據 page++; getNetData(page); //更新適配器 } private class MAdapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View viewItem = View.inflate(getActivity(), R.layout.listview, null); TextView textView = (TextView) viewItem.findViewById(R.id.textView); ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView); textView.setText(list.get(i).getNews_title()); imageLoaderInstances.displayImage(list.get(i).getPic_url(), imageView); return viewItem; } } private class MAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... strings) { return NetWordUtils.getNetjson(strings[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Goods goods = gson.fromJson(s, Goods.class); List<Goods.DataBean> dataTemp = goods.getData(); list.addAll(dataTemp); //更新適配器 mAdapter.notifyDataSetChanged(); uicomplete();//中止刷新 } } //上下拉刷新頭或者刷新底部自動返回功能 public void uicomplete(){ xListView.stopRefresh(); xListView.stopLoadMore(); xListView.setRefreshTime("剛剛"+System.currentTimeMillis()); } }
-------------------------------------------
R.layout.listview:
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/imageView"/> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/textView" android:text="TextView"/> </LinearLayout>