例子中用于解析Json的Gson请自己Google下载
主Activity:
package COM.Example.Main;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import COM.Example.Main.R;import COM.Example.Main.stringGetJson.User;import android.app.Activity;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.TextView;public class stringListActivity extends ListActivity {private MyAdapter mMyAdapter;private LinkedList<User> users = null;// 当前Activity中的ListViewListView listView = null;int lastItem = 0;LinearLayout loadingLayout = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.foodlistactivity);listView = getListView();setLoadingLayout();new readTask().execute(null);}private final class MyAdapter extends ArrayAdapter<User> {public MyAdapter(Activity activity, List<User> newsList) {super(activity, 0, newsList);}private Map<Integer, View> viewMap = new HashMap<Integer, View>();@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;View rowView = this.viewMap.get(position);if (rowView == null) {User user = users.get(position);LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater();holder = new ViewHolder();rowView = inflater.inflate(R.layout.foodlistactivity_item, null);holder.mNameText = (TextView) rowView.findViewById(R.id.foodItemUsername);holder.mPhoto = (ImageView) rowView.findViewById(R.id.foodItemPic);rowView.setTag(holder);holder.mNameText.setText(user.Name);if (!holder.mPhoto.isDrawingCacheEnabled()) {holder.mPhoto.setTag(user.Pic);new downImageTask().execute(holder.mPhoto);holder.mPhoto.setDrawingCacheEnabled(true);}viewMap.put(position, rowView);} else {holder = (ViewHolder) rowView.getTag();}return rowView;}public class ViewHolder {public TextView mNameText;public ImageView mPhoto;}}public void setLoadingLayout() {LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.HORIZONTAL);ProgressBar progressBar = new ProgressBar(this); progressBar.setPadding(0, 0, 15, 0); layout.addView(progressBar); TextView textView = new TextView(this); textView.setText("加载中..."); textView.setGravity(Gravity.CENTER_VERTICAL); layout.addView(textView); layout.setGravity(Gravity.CENTER); loadingLayout = new LinearLayout(this); loadingLayout.addView(layout); loadingLayout.setGravity(Gravity.CENTER); } private class scrollListener implements OnScrollListener { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 当listView中没有数据,或者数据超过100条,或者不是10的整数倍(即数据不足)时隐藏“更多”并取消onScroll事件的绑定 if (mMyAdapter.getCount() >= 100 || mMyAdapter.getCount() % 10 > 0 || mMyAdapter.getCount() == 0) { listView.removeFooterView(loadingLayout); listView.setOnScrollListener(null); } lastItem = firstVisibleItem + visibleItemCount - 1;// 这里减一是因为有FootView } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (lastItem == mMyAdapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE) { // 进行翻页操作 new scrollTask().execute(null); } } } public class readTask extends AsyncTask<Object, Void, Void> { @Override protected Void doInBackground(Object... arg0) { users = new stringGetJson().getJson(); return null; } @Override protected void onPostExecute(Void result) { listView.addFooterView(loadingLayout); mMyAdapter = new MyAdapter(stringListActivity.this, users); setListAdapter(mMyAdapter); listView.setOnScrollListener(new scrollListener()); } } public class scrollTask extends AsyncTask<Object, Void, Void> { @Override protected Void doInBackground(Object... arg0) { for (Iterator iterator = new stringGetJson().getJson() .iterator(); iterator.hasNext();) { User user = (User) iterator.next(); users.add(user); } return null; } @Override protected void onPostExecute(Void result) { mMyAdapter.notifyDataSetChanged(); } } }
Json数据来源(这里的例子中把json字符串直接写在代码中了,实际项目中需要改成从网络读取,例子中已经使用了多线程):
package COM.Example.Main;import java.lang.reflect.Type;import java.util.Iterator;import java.util.LinkedList;import android.database.MatrixCursor;import android.widget.ListView;import android.widget.TextView;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public class stringGetJson {// 设置临时ID,在项目中将被现实的id取代private int id = 0;public stringGetJson() {super();}//通过getJson方法从Json字符串获取User的列表public LinkedList<User> getJson() {String jsonData = "[{\"Name\":\"xinuxForJson1\",\"Pic\":\"http://.Example.FunctionUtility;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class Download {public static Bitmap getBitmapFromUrl(String imgUrl) {URL url;Bitmap bitmap = null;try {url = new URL(imgUrl);InputStream is = url.openConnection().getInputStream();BufferedInputStream bis = new BufferedInputStream(is);bitmap = BitmapFactory.decodeStream(bis);bis.close();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bitmap;}}
以上所述是小编给大家分享的Android程序开发ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)的相关知识,希望对大家有所帮助。