ListView、GridView显示组图,处理机制相同

<?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="match_parent"
android:layout_margin="10dp"
android:background="@drawable/pic_list_item_bg"
android:orientation="vertical" > <ImageView
android:id="@+id/iv_icon"
android:layout_width="match_parent"
android:layout_height="160dp"
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="8dp"
android:singleLine="true"
android:text="标题"
android:textColor="#000"
android:textSize="18sp" />
</LinearLayout> </LinearLayout>

list_item_photo.xml

<?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="match_parent"
android:orientation="vertical" > <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#fff"
android:divider="@null" >
</ListView> <GridView
android:id="@+id/gv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:visibility="gone" >
</GridView>
</FrameLayout> </LinearLayout>

pager_menu_detail_photo.xml

import java.util.ArrayList;

/**
* 组图对象封装
*
*/
public class PhotoBean {
public int retcode;
public PhotoData data; public class PhotoData {
public ArrayList<PhotoNewsData> news;
} public class PhotoNewsData {
public String id;
public String listimage;
public String pubdate;
public String title;
public String url;
}
}

PhotoBean

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.zhsh66.R;
import com.itheima.zhsh66.base.BaseMenuDetailPager;
import com.itheima.zhsh66.domain.PhotoBean;
import com.itheima.zhsh66.domain.PhotoBean.PhotoNewsData;
import com.itheima.zhsh66.global.Constants;
import com.itheima.zhsh66.utils.CacheUtils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
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;
import com.lidroid.xutils.view.annotation.ViewInject; /**
* 菜单详情页-组图
*
*/
public class PhotosMenuDetailPager extends BaseMenuDetailPager implements
OnClickListener { @ViewInject(R.id.lv_list)
private ListView lvList;
@ViewInject(R.id.gv_list)
private GridView gvList; private ArrayList<PhotoNewsData> mPhotoList; private boolean isList = true;// 当前界面状态 private ImageButton btnDisplay; public PhotosMenuDetailPager(Activity activity, ImageButton btnDisplay) {
super(activity);
this.btnDisplay = btnDisplay;
btnDisplay.setOnClickListener(this);
} @Override
public View initView() {
View view = View.inflate(mActivity, R.layout.pager_menu_detail_photo,
null);
ViewUtils.inject(this, view);
return view;
} @Override
public void initData() {
String cache = CacheUtils.getCache(Constants.PHOTOS_URL, mActivity);
if (!TextUtils.isEmpty(cache)) {
processResult(cache);
} getDataFromServer();
} private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, Constants.PHOTOS_URL,
new RequestCallBack<String>() { @Override
public void onSuccess(ResponseInfo<String> responseInfo) {
processResult(responseInfo.result); CacheUtils.setCache(Constants.PHOTOS_URL,
responseInfo.result, mActivity);
} @Override
public void onFailure(HttpException error, String msg) {
error.printStackTrace();
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
}
});
} protected void processResult(String result) {
Gson gson = new Gson();
PhotoBean photoBean = gson.fromJson(result, PhotoBean.class);
mPhotoList = photoBean.data.news; lvList.setAdapter(new PhotoAdapter());
gvList.setAdapter(new PhotoAdapter());
} class PhotoAdapter extends BaseAdapter { private BitmapUtils mBitmapUtils; public PhotoAdapter() {
mBitmapUtils = new BitmapUtils(mActivity);
mBitmapUtils.configDefaultLoadingImage(R.drawable.news_pic_default);
} @Override
public int getCount() {
return mPhotoList.size();
} @Override
public PhotoNewsData 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 = null;
if (convertView == null) {
convertView = View.inflate(mActivity, R.layout.list_item_photo,
null);
holder = new ViewHolder();
holder.tvTitle = (TextView) convertView
.findViewById(R.id.tv_title);
holder.ivIcon = (ImageView) convertView
.findViewById(R.id.iv_icon); convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
} PhotoNewsData item = getItem(position); holder.tvTitle.setText(item.title);
mBitmapUtils.display(holder.ivIcon, item.listimage); return convertView;
} } static class ViewHolder {
public TextView tvTitle;
public ImageView ivIcon;
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_display:
// 如果当前是列表, 要切换成grid, 如果是grid,就切换成列表
if (isList) {
isList = false; lvList.setVisibility(View.GONE);
gvList.setVisibility(View.VISIBLE); btnDisplay.setImageResource(R.drawable.icon_pic_list_type);
} else {
isList = true; lvList.setVisibility(View.VISIBLE);
gvList.setVisibility(View.GONE); btnDisplay.setImageResource(R.drawable.icon_pic_grid_type);
} break; default:
break;
}
}
}

PhotosMenuDetailPager

共用一个Adapter

protected void processResult(String result) {
Gson gson = new Gson();
PhotoBean photoBean = gson.fromJson(result, PhotoBean.class);
mPhotoList = photoBean.data.news;

lvList.setAdapter(new PhotoAdapter());
gvList.setAdapter(new PhotoAdapter());
}

缓存到本地

public void initData() {
String cache = CacheUtils.getCache(Constants.PHOTOS_URL, mActivity);
if (!TextUtils.isEmpty(cache)) {
processResult(cache);
}

getDataFromServer();
}

------------------------------------------------------------------------------

初始化标题按钮(Base )

<?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="match_parent"
android:orientation="vertical" > <include layout="@layout/title_bar" /> <FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout> </LinearLayout>
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView; import com.itheima.zhsh66.MainActivity;
import com.itheima.zhsh66.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; /**
* 五个标签页的基类
*
*/
public abstract class BasePager { public Activity mActivity; // 标签页面的根布局
public View mRootView; public TextView tvTitle;
public ImageButton btnMenu;
// 帧布局容器, 将来要动态向里面添加内容
public FrameLayout flContent; // 组图切换按钮
public ImageButton btnDisplay; public BasePager(Activity activity) {
mActivity = activity;
initView();
} /**
* 初始化布局
*/
public void initView() {
mRootView = View.inflate(mActivity, R.layout.base_pager, null);
tvTitle = (TextView) mRootView.findViewById(R.id.tv_title);
btnMenu = (ImageButton) mRootView.findViewById(R.id.btn_menu);
flContent = (FrameLayout) mRootView.findViewById(R.id.fl_content);
btnDisplay = (ImageButton) mRootView.findViewById(R.id.btn_display); btnMenu.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
toggle();
}
});
} /**
* 侧边栏展开或者收起的方法
*/
private void toggle() {
MainActivity mainUI = (MainActivity) mActivity;
SlidingMenu slidingMenu = mainUI.getSlidingMenu();
slidingMenu.toggle();// 开关(如果状态为开,它就关;如果状态为关,它就开)
} /**
* 初始化数据
*/
public abstract void initData();
}

BasePager

------------------------------------------------------------------------------

对按钮的传递、调用

if (pager instanceof PhotosMenuDetailPager) {

import java.util.ArrayList;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast; import com.google.gson.Gson;
import com.itheima.zhsh66.MainActivity;
import com.itheima.zhsh66.base.BaseMenuDetailPager;
import com.itheima.zhsh66.base.BasePager;
import com.itheima.zhsh66.base.impl.menudetail.InteractMenuDetailPager;
import com.itheima.zhsh66.base.impl.menudetail.NewsMenuDetailPager;
import com.itheima.zhsh66.base.impl.menudetail.PhotosMenuDetailPager;
import com.itheima.zhsh66.base.impl.menudetail.TopicMenuDetailPager;
import com.itheima.zhsh66.domain.NewsMenuData;
import com.itheima.zhsh66.global.Constants;
import com.itheima.zhsh66.utils.CacheUtils;
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 NewsCenterPager extends BasePager { // 菜单详情页集合
private ArrayList<BaseMenuDetailPager> mMenuDetailPagers;
private NewsMenuData mNewsMenuData;// 新闻分类信息网络数据 public NewsCenterPager(Activity activity) {
super(activity);
} @Override
public void initData() {
System.out.println("新闻中心初始化...");
tvTitle.setText("新闻"); // 1.首先先看本地有没有缓存
// 2.有缓存,直接加载缓存
String cache = CacheUtils.getCache(Constants.CATEGORIES_URL, mActivity);
if (!TextUtils.isEmpty(cache)) {
// 有缓存
System.out.println("发现缓存....");
processResult(cache);
} // 即使发现有缓存,仍继续调用网络, 获取最新数据
getDataFromServer();
} /**
* 从服务器获取数据 需要权限: <uses-permission
* android:name="android.permission.INTERNET"/>
*/
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, Constants.CATEGORIES_URL,
new RequestCallBack<String>() { @Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// 请求成功
String result = responseInfo.result;// 获取json字符串
// System.out.println("result:" + result);
processResult(result);
// 写缓存
CacheUtils.setCache(Constants.CATEGORIES_URL, result,
mActivity);
} @Override
public void onFailure(HttpException error, String msg) {
// 请求失败
error.printStackTrace();
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
}
});
} /**
* 解析json数据
*
* @param result
*/
protected void processResult(String result) {
// gson->json
Gson gson = new Gson();
mNewsMenuData = gson.fromJson(result, NewsMenuData.class);
System.out.println("解析结果:" + mNewsMenuData); // 获取侧边栏对象
MainActivity mainUI = (MainActivity) mActivity;
LeftMenuFragment leftMenuFragment = mainUI.getLeftMenuFragment();
// 将网络数据设置给侧边栏
leftMenuFragment.setData(mNewsMenuData.data); // 初始化4个菜单详情页
mMenuDetailPagers = new ArrayList<BaseMenuDetailPager>();
mMenuDetailPagers.add(new NewsMenuDetailPager(mActivity,
mNewsMenuData.data.get(0).children));
mMenuDetailPagers.add(new TopicMenuDetailPager(mActivity));
mMenuDetailPagers.add(new PhotosMenuDetailPager(mActivity, btnDisplay));
mMenuDetailPagers.add(new InteractMenuDetailPager(mActivity)); // 菜单详情页-新闻作为初始页面
setCurrentMenuDetailPager(0);
} // 给新闻中心页面的FrameLayout填充布局
protected void setCurrentMenuDetailPager(int position) {
BaseMenuDetailPager pager = mMenuDetailPagers.get(position);
// 移除之前所有的view对象, 清理屏幕
flContent.removeAllViews();
flContent.addView(pager.mRootView);
pager.initData();// 初始化数据 // 更改标题
tvTitle.setText(mNewsMenuData.data.get(position).title); // 组图页面需要显示切换按钮
if (pager instanceof PhotosMenuDetailPager) {
btnDisplay.setVisibility(View.VISIBLE);
} else {
btnDisplay.setVisibility(View.GONE);
}
} }

NewsCenterPager

android 项目学习随笔十七(ListView、GridView显示组图)的更多相关文章

  1. android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)

    1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为        2,为了使LISTVIEW的ITEM在点 ...

  2. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...

  3. android 项目学习随笔十一(ListView下拉刷新提示)

    1. 设置mHeaderView.setPadding TOPPADING为负值,隐藏刷新提示头布局 在onTouchEvent事件中进行头布局显示隐藏切换 import java.text.Simp ...

  4. android 项目学习随笔九(ListView加头布局)

    1.缓冲背景色 <ListView android:id="@+id/lv_list" android:layout_width="match_parent&quo ...

  5. android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)

    语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...

  6. android 项目学习随笔十八(三级缓存)

    xUtils的BitmapUtils模块用的就是三级缓存,在项目中尽量还是应用BitmapUtils 三级缓存(机制) import com.itheima.zhsh66.R; import andr ...

  7. android 项目学习随笔二十(屏幕适配)

    1.图片适配 放入相同名称的资源文件,机器根据不同分辨率找相近的资源 240*320 ldpi 320*480 mdpi 480*800 hdpi 720*1280 xhdpi 2.布局适配 在不同的 ...

  8. android 项目学习随笔十六( 广告轮播条播放)

    广告轮播条播放 if (mHandler == null) {//在此初始化mHandler , 保证消息不重复发送 mHandler = new Handler() { public void ha ...

  9. android 项目学习随笔十四(WebView)

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

随机推荐

  1. 【转】Android的材料设计兼容库(Design Support Library)

    转自:http://www.jcodecraeer.com/a/anzhuokaifa/developer/2015/0531/2958.html?mType=Group Android的材料设计兼容 ...

  2. android apk简单反编译

    1. 查看或者提取资源文件:     使用谷歌官方工具apktool,命令行如下: apktool d xxx.apk xxx_decode d代表反编译,xxx.apk为你要反编译的apk,xxx_ ...

  3. hexo 搭建博客

    使用hexo搭建网站.记录一下. hexo搭建方法: https://wsgzao.github.io/post/hexo-guide/ http://jacob110.github.io/2015/ ...

  4. iOS 面试题及答案

    1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...

  5. 搭建和使用Docker私有仓库

    需要注意的是,从Docker Pool下载的镜像文件,与官方镜像文件是完全一致的. 安装Docker之后,可以是使用官方提供的registry镜像来搭建一套本地私有仓库环境:  docker run  ...

  6. 30天,O2O速成攻略【8.29杭州站】

    活动概况 时间:2015年8月29日13:30-16:30 地点:123茶楼(杭州上城区青年路27号2楼) 主办:APICloud.UPYUN.一起火 网址:www.apicloud.com 费用:免 ...

  7. Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .

    String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...

  8. WordPress 性能检测与速度优化

    来源:SayBlog.Me [摘要]在如何提升WordPress性能与速度方面多花一些时间是值得的,本文下面为大家就如何提升WordPress运行效率列出了几点建议以供参考. 你的WordPress博 ...

  9. bootstrap学习之二-组件

    一.bootstrap字体图标 以span的形式出现,通常可以用于一个button或者其他元素的内文本, <span class="glyphicon glyphicon-sort-b ...

  10. linux:什么是linux

    1>.linux是一套作业系统(linux就是核心与呼叫这两层),每一种作业系统都是在他专门的硬体机器上面运行的:linux是一个Open Source的作业系统,具有可移植性 2>.li ...