package com.itheima.zhbj52.base.menudetail; import java.util.ArrayList; import android.app.Activity; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.itheima.zhbj52.R; import com.itheima.zhbj52.base.BaseMenuDetailPager; import com.itheima.zhbj52.domain.PhotosData; import com.itheima.zhbj52.domain.PhotosData.PhotoInfo; import com.itheima.zhbj52.domain.PhotosData.PhotosInfo; import com.itheima.zhbj52.global.GlobalContants; import com.itheima.zhbj52.utils.CacheUtils; import com.lidroid.xutils.BitmapUtils; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; /** * 新闻中心-组图 */ public class PhotoMenuDetailPager extends BaseMenuDetailPager { /*public abstract class BaseMenuDetailPager { public Activity mActivity; public View mRootView;// 根布局对象 public BaseMenuDetailPager(Activity activity) { mActivity = activity; mRootView = initViews(); } public abstract View initViews(); public void initData() { } }*/ private ListView lvPhoto; private GridView gvPhoto; private ArrayList<PhotoInfo> mPhotoList; private PhotoAdapter mAdapter; private ImageButton btnPhoto; public PhotoMenuDetailPager(Activity activity, ImageButton btnPhoto) {//btnPhoto对象通过构造函数传过来。 super(activity); this.btnPhoto = btnPhoto; btnPhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { changeDisplay(); } }); } @Override public View initViews() { View view = View.inflate(mActivity, R.layout.menu_photo_pager, null); //menu_photo_pager.xml /*<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/lv_photo" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#fff" 背景色为白色 android:divider="@null" /> 没有分割线 <GridView android:id="@+id/gv_photo" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" 设置为2列 android:visibility="gone" /> 默认隐藏。点击可以切换用ListView或者GridView。 </FrameLayout>*/ lvPhoto = (ListView) view.findViewById(R.id.lv_photo); gvPhoto = (GridView) view.findViewById(R.id.gv_photo); return view; } @Override public void initData() { String cache = CacheUtils .getCache(GlobalContants.PHOTOS_URL, mActivity); //PHOTOS_URL = "http://10.0.2.2:8080/zhbj/photos/photos_1.json"; if (!TextUtils.isEmpty(cache)) { } getDataFromServer(); } private void getDataFromServer() { HttpUtils utils = new HttpUtils(); utils.send(HttpMethod.GET, GlobalContants.PHOTOS_URL, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { String result = (String) responseInfo.result; parseData(result); // 设置缓存 CacheUtils.setCache(GlobalContants.PHOTOS_URL, result, mActivity); } @Override public void onFailure(HttpException error, String msg) { Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT) .show(); error.printStackTrace(); } }); } protected void parseData(String result) { Gson gson = new Gson(); PhotosData data = gson.fromJson(result, PhotosData.class); /*public class PhotosData { public int retcode; public PhotosInfo data; public class PhotosInfo { public String title; public ArrayList<PhotoInfo> news; } public class PhotoInfo { public String id; public String listimage; public String pubdate; public String title; public String type; public String url; } }*/ mPhotoList = data.data.news;// 获取组图列表集合 if (mPhotoList != null) { mAdapter = new PhotoAdapter(); lvPhoto.setAdapter(mAdapter);//使用同一个mAdapter gvPhoto.setAdapter(mAdapter); } } class PhotoAdapter extends BaseAdapter { private BitmapUtils utils; public PhotoAdapter() { utils = new BitmapUtils(mActivity); utils.configDefaultLoadingImage(R.drawable.news_pic_default);//默认加载图片 } @Override public int getCount() { return mPhotoList.size(); } @Override public PhotoInfo getItem(int position) { return mPhotoList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(mActivity, R.layout.list_photo_item,null); /*<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/pic_list_item_bg" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_pic" android:layout_width="match_parent" android:layout_height="180dp" android:scaleType="centerCrop" android:src="@drawable/news_pic_default" /> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="标题" android:singleLine="true" android:textColor="#000" android:textSize="22sp" /> </LinearLayout> </LinearLayout>*/ holder = new ViewHolder(); holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title); holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } PhotoInfo item = getItem(position); holder.tvTitle.setText(item.title); utils.display(holder.ivPic, item.listimage);//加载图片,默认BitmapUtils对图片提供了缓存。 return convertView; } } static class ViewHolder { public TextView tvTitle; public ImageView ivPic; } private boolean isListDisplay = true;// 是否是列表展示 /** * 切换展现方式 */ private void changeDisplay() { if (isListDisplay) { isListDisplay = false; lvPhoto.setVisibility(View.GONE); gvPhoto.setVisibility(View.VISIBLE); btnPhoto.setImageResource(R.drawable.icon_pic_list_type); } else { isListDisplay = true; lvPhoto.setVisibility(View.VISIBLE); gvPhoto.setVisibility(View.GONE); btnPhoto.setImageResource(R.drawable.icon_pic_grid_type); } } }
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5068481.html,如需转载请自行联系原作者