12、json、GridView、缓存
1.解析json数据:
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;
}
}
2.布局:
<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"
android:visibility="gone" />
</FrameLayout>
使用同一个子布局,同一个adapter
<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>
3.菜单详情页—组图
public static final String PHOTOS_URL = SERVER_URL
+ "/photos/photos_1.json";// 获取组图信息的接口
public class PhotoMenuDetailPager extends BaseMenuDetailPager {
private ListView lvPhoto;
private GridView gvPhoto;
private ArrayList<PhotoInfo> mPhotoList;
private PhotoAdapter mAdapter;
private ImageButton btnPhoto;
public PhotoMenuDetailPager(Activity activity, ImageButton btnPhoto) {
super(activity);this.btnPhoto = btnPhoto;
//这样就把imagebutton传了过来,好方法。在构造函数里传
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);
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);
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);
mPhotoList = data.data.news;// 获取组图列表集合
if (mPhotoList != null) {
mAdapter = new PhotoAdapter();
lvPhoto.setAdapter(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);
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);
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);
}
}
}
4.其他
在basepager里添加这样一个按钮,并findviewbyid出来,因为菜单是新闻中心里才有的,组图是菜单里的一项
<ImageButton
android:id="@+id/btn_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/icon_pic_grid_type"
android:visibility="gone" />
新闻中心页面setCurrentMenuDetailPager中
if (pager instanceof PhotoMenuDetailPager) {
btnPhoto.setVisibility(View.VISIBLE);
} else {
btnPhoto.setVisibility(View.GONE);
}