json数据
页签详情页数据
public class TabData {
public int retcode;
public TabDetail data;//不是数组的话类型就是这个
public class TabDetail {
public String title;
public String more;
public ArrayList<TabNewsData> news;
public ArrayList<TopNewsData> topnews;
@Override
public String toString() {
return "TabDetail [title=" + title + ", news=" + news
+ ", topnews=" + topnews + "]";
}
}
/**
* 新闻列表对象
*/
public class TabNewsData {
public String id;
public String listimage;
public String pubdate;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "TabNewsData [title=" + title + "]";
}
}
/**
* 头条新闻
*/
public class TopNewsData {
public String id;
public String topimage;
public String pubdate;
public String title;
public String type;
public String url;
@Override
public String toString() {
return "TopNewsData [title=" + title + "]";
}
}
@Override
public String toString() {
return "TabData [data=" + data + "]";
}
}
页签详情页
知识点:
- 使用xutils加载网络图片,xutils的configDefaultLoadingImage()方法,设置默认图片 ,还有
BitmapFun 、ImageDownloader也能加载网络图片 - xutils的注解,不需要fandviewbyid了,但是前提是需要注入事件ViewUtils.inject(this, view);
- 可以给listview加个头,那样头也跟着滑动了,头上还能加头,lvList.addHeaderView(headerView);
- indicate的使用
1.list_header_topnews.xml:头条新闻
由于indicate自带的下面的几个点很难看,可以根据自己的需求修改
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"//注意命名空间也需要拷过来
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.itheima.zhbj52.view.TopNewsViewPager
android:id="@+id/vp_news"
android:layout_width="match_parent"
android:layout_height="200dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#a000"
android:padding="3dp" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="16sp" />
<com.viewpagerindicator.CirclePageIndicator//下面的几个点
android:id="@+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dip"
app:fillColor="#f00"//点选中的颜色
app:pageColor="@android:color/darker_gray"//点默认的颜色
app:radius="3dp"//大小
app:strokeWidth="0dp" />//点外面的黑圆圈
</RelativeLayout>
</RelativeLayout>
2.list_news_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="110dp"
android:layout_height="70dp"
android:scaleType="fitXY"
android:padding="1dp"
android:background="@android:color/darker_gray"//padding了一个dp,背景是灰色的,图片外就显示出了一个边框
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/image_demo" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/iv_pic"
android:text="新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题新闻标题"
android:textColor="#000"
android:maxLines="2"
android:ellipsize="end"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/tv_title"
android:text="2015-03-16 16:20"
android:layout_alignBottom="@id/iv_pic"
android:textColor="@android:color/darker_gray"
android:textSize="16sp" />
</RelativeLayout>
3、tab_detail_pager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.itheima.zhbj52.view.RefreshListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:cacheColorHint="#fff"
android:layout_weight="1" >
</com.itheima.zhbj52.view.RefreshListView>
</LinearLayout>
4.页签详情页
public class TabDetailPager extends BaseMenuDetailPager implements
OnPageChangeListener {
NewsTabData mTabData;// 新闻页签数据
// private TextView tvText;
private String mUrl;// 访问页签的网络地址
private TabData mTabDetailData;// 页签详情页数据
@ViewInject(R.id.vp_news)
private ViewPager mViewPager;// 头条新闻的pager
@ViewInject(R.id.tv_title)
private TextView tvTitle;// 头条新闻的标题
private ArrayList<TopNewsData> mTopNewsList;// 头条新闻数据集合
@ViewInject(R.id.indicator)
private CirclePageIndicator mIndicator;// 头条新闻位置指示器
@ViewInject(R.id.lv_list)
private RefreshListView lvList;// 新闻列表
private ArrayList<TabNewsData> mNewsList; // 新闻数据集合
private NewsAdapter mNewsAdapter;// 给list设置的adapter
public TabDetailPager(Activity activity, NewsTabData newsTabData) {
super(activity);
mTabData = newsTabData;//new的时候把数据和连接地址也创建了
mUrl = GlobalContants.SERVER_URL + mTabData.url;
}
@Override
public View initViews() {
View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);//下拉刷新
// 加载头布局
View headerView = View.inflate(mActivity, R.layout.list_header_topnews,
null);
ViewUtils.inject(this, view);
ViewUtils.inject(this, headerView);
// 将头条新闻以头布局的形式加给listview
lvList.addHeaderView(headerView);
return view;
}
@Override
public void initData() {
getDataFromServer();
}
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = (String) responseInfo.result;
System.out.println("页签详情页返回结果:" + result);
parseData(result);
}
@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();
mTabDetailData = gson.fromJson(result, TabData.class);
System.out.println("页签详情解析:" + mTabDetailData);
mTopNewsList = mTabDetailData.data.topnews;
mNewsList = mTabDetailData.data.news;
if (mTopNewsList != null) {
mViewPager.setAdapter(new TopNewsAdapter());
mIndicator.setViewPager(mViewPager);
mIndicator.setSnap(true);// 支持快照显示,即不是慢慢的滑过去,而是跳过去
mIndicator.setOnPageChangeListener(this);
mIndicator.onPageSelected(0);// 让指示器重新定位到第一个点
tvTitle.setText(mTopNewsList.get(0).title);//首先让他显示个字
}
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
lvList.setAdapter(mNewsAdapter);
}
}
/**
* 头条新闻适配器
*
* @author Kevin
*
*/
class TopNewsAdapter extends PagerAdapter {
private BitmapUtils utils;
public TopNewsAdapter() {
utils = new BitmapUtils(mActivity);
utils.configDefaultLoadingImage(R.drawable.topnews_item_default);// 设置默认图片
}
@Override
public int getCount() {
return mTabDetailData.data.topnews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView image = new ImageView(mActivity);
image.setScaleType(ScaleType.FIT_XY);// 基于控件大小填充图片
TopNewsData topNewsData = mTopNewsList.get(position);
utils.display(image, topNewsData.topimage);// 传递imagView对象和图片地址
container.addView(image);
System.out.println("instantiateItem....." + position);
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
/**
* 新闻列表的适配器
*
* @author Kevin
*
*/
class NewsAdapter extends BaseAdapter {
private BitmapUtils utils;
public NewsAdapter() {
utils = new BitmapUtils(mActivity);
utils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
}
@Override
public int getCount() {
return mNewsList.size();
}
@Override
public TabNewsData getItem(int position) {
return mNewsList.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_news_item,
null);
holder = new ViewHolder();
holder.ivPic = (ImageView) convertView
.findViewById(R.id.iv_pic);
holder.tvTitle = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tvDate = (TextView) convertView
.findViewById(R.id.tv_date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
TabNewsData item = getItem(position);
holder.tvTitle.setText(item.title);
holder.tvDate.setText(item.pubdate);
utils.display(holder.ivPic, item.listimage);
return convertView;
}
}
static class ViewHolder {
public TextView tvTitle;
public TextView tvDate;
public ImageView ivPic;
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
TopNewsData topNewsData = mTopNewsList.get(arg0);
tvTitle.setText(topNewsData.title);
}
}