智慧北京开发第三天(下)

头条新闻标题设置 & 使用ViewpagerIndicator实现头条新闻位置指示器

tab_detail_pager.xml



    

        

        

            
            
        
    
    

    
TabDetailPager.java
package com.xbmu.wisdombj.base;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
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;
import com.viewpagerindicator.CirclePageIndicator;
import com.xbmu.wisdombj.R;
import com.xbmu.wisdombj.bean.NewsData.NewsTabData;
import com.xbmu.wisdombj.bean.TabData;
import com.xbmu.wisdombj.bean.TabData.TopNewsData;
import com.xbmu.wisdombj.global.GlobalVariables;

import java.util.ArrayList;


/**
 * 页签详情页,表示每一个页签的对象
 * Created by Administrator on 2016/2/7 0007.
 */
public class TabDetailPager extends BaseNewsDetailPager implements ViewPager.OnPageChangeListener{

    private NewsTabData mTabData;//新闻的 11个子标签标题

    @ViewInject(R.id.vp_tab_top)
    private ViewPager vpTabPagerTop;

    @ViewInject(R.id.lv_tab_list)
    private ListView lvTabList;

    @ViewInject(R.id.tv_news_title)
    private TextView tvNewsTitle;

    @ViewInject(R.id.indicator)
    private CirclePageIndicator indicator;

    private String mUrl;
    private TabData tabData;//页签数据
    private ArrayList topNewsDatas;//头条新闻数据集合
    private TopNewsData topNewsData;//头条新闻数据

    public TabDetailPager(Activity mActivity, NewsTabData newsTabDatas) {
        super(mActivity);
        this.mTabData = newsTabDatas;
        mUrl = GlobalVariables.BASE_URL + newsTabDatas.url;
    }

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.tab_detail_pager,null);
        ViewUtils.inject(this,view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        getDataFromServer();

    }

    /**
     * 从服务器获取数据
     */
    public void getDataFromServer() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.GET, mUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result;
                System.out.print("从网络获取的子页签详情数据:" + result);

                //解析数据
                paserData(result);
            }

            @Override
            public void onFailure(HttpException e, String msg) {
                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        });
    }

    /**
     * 解析数据
     * @param result
     */
    private void paserData(String result) {
        Gson gson = new Gson();
        tabData = gson.fromJson(result, TabData.class);
        topNewsDatas = tabData.data.topnews;
        if(topNewsDatas!=null){
            vpTabPagerTop.setAdapter(new NewsTopAdapter());
            indicator.setOnPageChangeListener(this);
            indicator.setSnap(true);//支持快照显示
            indicator.onPageSelected(0);//让指示器重新定位到第一个点
            indicator.setViewPager(vpTabPagerTop);
            
            tvNewsTitle.setText(topNewsDatas.get(0).title);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNewsTitle.setText(topNewsDatas.get(position).title);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 新闻头条适配器
     */
    private class NewsTopAdapter extends PagerAdapter{
        private BitmapUtils bitmapUtils;
        public NewsTopAdapter() {
            bitmapUtils = new BitmapUtils(mActivity);
            bitmapUtils.configDefaultLoadingImage(R.drawable.topnews_item_default);//设置默认图片
        }

        @Override
        public int getCount() {
            return topNewsDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(mActivity);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);//基于控件大小填充图片
            topNewsData = topNewsDatas.get(position);
            bitmapUtils.display(imageView, topNewsData.topimage);//传递imageView对象和图片地址
            container.addView(imageView);

            System.out.println("topNews:" + position);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }
}
运行效果:
智慧北京开发第三天(下)_第1张图片

新闻列表展示 & item布局编写

new_list_item.xml


    
    
    
效果图:
智慧北京开发第三天(下)_第2张图片
tab_detail_pager.xml



    
    
new_top_ads.xml



    

    

        

        
    

TabDetailPager.java
package com.xbmu.wisdombj.base;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
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;
import com.viewpagerindicator.CirclePageIndicator;
import com.xbmu.wisdombj.R;
import com.xbmu.wisdombj.bean.NewsData.NewsTabData;
import com.xbmu.wisdombj.bean.TabData;
import com.xbmu.wisdombj.bean.TabData.TabNewsData;
import com.xbmu.wisdombj.bean.TabData.TopNewsData;
import com.xbmu.wisdombj.global.GlobalVariables;
import java.util.ArrayList;


/**
 * 页签详情页,表示每一个页签的对象
 * Created by Administrator on 2016/2/7 0007.
 */
public class TabDetailPager extends BaseNewsDetailPager implements ViewPager.OnPageChangeListener {

    private NewsTabData mTabData;//新闻的 11个子标签标题

    @ViewInject(R.id.vp_tab_top)
    private ViewPager vpTabPagerTop;

    @ViewInject(R.id.lv_tab_list)
    private ListView lvTabList;

    @ViewInject(R.id.tv_news_title)
    private TextView tvNewsTitle;

    @ViewInject(R.id.indicator)
    private CirclePageIndicator indicator;

    private String mUrl;
    private TabData tabData;//页签数据
    private ArrayList topNewsDatas;//头条新闻数据集合
    private TopNewsData topNewsData;//头条新闻数据
    private ArrayList tabNewsDatas;//新闻列表对象
    private View newTopView;//新闻顶部广告条

    public TabDetailPager(Activity mActivity, NewsTabData newsTabDatas) {
        super(mActivity);
        this.mTabData = newsTabDatas;
        mUrl = GlobalVariables.BASE_URL + newsTabDatas.url;
    }

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
        newTopView = View.inflate(mActivity, R.layout.new_top_ads, null);
        ViewUtils.inject(this, view);
        ViewUtils.inject(this, newTopView);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        getDataFromServer();

    }

    /**
     * 从服务器获取数据
     */
    public void getDataFromServer() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.GET, mUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = responseInfo.result;
                System.out.print("从网络获取的子页签详情数据:" + result);

                //解析数据
                paserData(result);
            }

            @Override
            public void onFailure(HttpException e, String msg) {
                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        });
    }

    /**
     * 解析数据
     *
     * @param result
     */
    private void paserData(String result) {
        Gson gson = new Gson();
        tabData = gson.fromJson(result, TabData.class);
        topNewsDatas = tabData.data.topnews;
        if (topNewsDatas != null) {
            vpTabPagerTop.setAdapter(new NewsTopAdapter());
            indicator.setOnPageChangeListener(this);
            indicator.setSnap(true);//支持快照显示
            indicator.onPageSelected(0);//让指示器重新定位到第一个点
            indicator.setViewPager(vpTabPagerTop);

            tvNewsTitle.setText(topNewsDatas.get(0).title);
        }
        tabNewsDatas = tabData.data.news;
        if (tabNewsDatas != null) {
            lvTabList.addHeaderView(newTopView);
            lvTabList.setAdapter(new NewsItemAdapter());
        }
    }

    private class NewsItemAdapter extends BaseAdapter {

        private  BitmapUtils bitmapUtils;

        public NewsItemAdapter() {
            bitmapUtils = new BitmapUtils(mActivity);
        }

        @Override
        public int getCount() {
            return tabNewsDatas.size();
        }

        @Override
        public TabNewsData getItem(int position) {
            return tabNewsDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                convertView = View.inflate(mActivity,R.layout.new_list_item,null);
                viewHolder = new ViewHolder();
                viewHolder.ivNewIcon = (ImageView) convertView.findViewById(R.id.iv_pic);
                viewHolder.tvNewTitle = (TextView) convertView.findViewById(R.id.tv_title);
                viewHolder.tvNewDate = (TextView) convertView.findViewById(R.id.tv_date);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tvNewTitle.setText(getItem(position).title);
            viewHolder.tvNewDate.setText(getItem(position).pubdate);
            bitmapUtils.display(viewHolder.ivNewIcon,getItem(position).listimage);

            return convertView;
        }
    }
    static class ViewHolder{
        ImageView ivNewIcon;
        TextView tvNewTitle;
        TextView tvNewDate;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNewsTitle.setText(topNewsDatas.get(position).title);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 新闻头条适配器
     */
    private class NewsTopAdapter extends PagerAdapter {
        private BitmapUtils bitmapUtils;

        public NewsTopAdapter() {
            bitmapUtils = new BitmapUtils(mActivity);
            bitmapUtils.configDefaultLoadingImage(R.drawable.topnews_item_default);//设置默认图片
        }

        @Override
        public int getCount() {
            return topNewsDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(mActivity);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);//基于控件大小填充图片
            topNewsData = topNewsDatas.get(position);
            bitmapUtils.display(imageView, topNewsData.topimage);//传递imageView对象和图片地址
            container.addView(imageView);

            System.out.println("topNews:" + position);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
运行效果:
智慧北京开发第三天(下)_第3张图片

全部代码:
http://download.csdn.net/detail/btt2013/9429457



































你可能感兴趣的:(Android项目)