关闭

[安卓]新闻客户端(五) 主页面之slidingMenu & fragment(3)

标签: 安卓
270人阅读 评论(0) 收藏 举报
分类:

续前文

新闻子栏目,上面的这个布局,需要一个viewpager,但是由于一些首位页滑动bug问题,这里给他写成一个类来实现,前面已经有两个类似情况了,同时由于要传递参数,要重写一个传递属性的构造函数,关于bug,有一篇详述

listview上下拖动,这里需要带动上面的viewpager,本来可以放到scrollview里面,但是由于上面还有一个下拉菜单,所以需要把viewpager抽出来,当做listview的第一项。

上面的布局包括一个viewpager,一个textview和一个indicator,把他当做header,使用addheader添加


public class TopNewsViewPager extends ViewPager{

	public TopNewsViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public TopNewsViewPager(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

}
然后tabDetailPager的布局当成了一整个list,前面也提到了,listview有一个属性 cachecolorhint,点击时背景色的设置,默认是黑色

<?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" >
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:cacheColorHint="#FFF"
        android:id="@+id/lv_tab_detail_news"/>
</LinearLayout>

他实际上是两部分了

组合布局里的点引用了indicator里的控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
        android:layout_height="200dp">
    <com.example.mynewsapp.view.TopNewsViewPager
      android:id="@+id/vp_tab_detail_topnews"
      android:layout_width="match_parent"
      android:layout_height="200dp"> 
    </com.example.mynewsapp.view.TopNewsViewPager> 
    
   	<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#a000"
        android:layout_alignParentBottom="true"
        android:padding="5dp">
     	  <TextView
	          android:layout_width="fill_parent"
	          android:layout_height="wrap_content"
	          android:id="@+id/tv_tabdetial_topnewstitle"
	          android:textColor="#FFF"/>          
          <com.viewpagerindicator.CirclePageIndicator
	          android:id="@+id/topnews_indicator"
	          android:padding="10dip"
	          android:layout_height="wrap_content"
	          android:layout_width="wrap_content"
	          android:layout_alignParentRight="true"/> 	  
   	</RelativeLayout>
</RelativeLayout>

另一个是下面的list

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<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"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/image_demo" />
    <TextView
        android:id="@+id/tv_newsitme_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_newsitme_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/tv_newsitme_title"
        android:text="2015-03-16 16:20"
         android:layout_alignBottom="@id/iv_pic"
        android:textColor="@android:color/darker_gray"
        android:textSize="16sp" />
</RelativeLayout>


回到tab里来,这里的解析同样需要一个bean来规定格式

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 + "]";
	}
}

public class TabDetailPager extends BaseMenuDetailPager {
	private String mUrl;
	private NewsTabData newtabdata;
	private ViewPager vp_tab_detail_topnews; //上面的头条新闻(使用大图来显示)
    private ListView  lv_tab_detail_news; //新闻列表
    private TabData mTabDetailData;  //这个页签的详细数据(包含该分类的全部新闻)
	public ArrayList<TopNewsData> mTopNewsList;	
	private TextView tv_tabdetial_topnewstitle; //放topnews新闻标题的控件
    private CirclePageIndicator topnews_indicator;   
    private ArrayList<TabNewsData> listnewsData;
    
	public TabDetailPager(Activity activity) {
		super(activity);
		// TODO Auto-generated constructor stub
	}
	public TabDetailPager(Activity mActivity, NewsTabData newsTabData) {
		// TODO Auto-generated constructor stub
		super(mActivity);
		newtabdata = newsTabData;
		mUrl = GlobalContants.SERVER_URL+ newsTabData.url;
		System.out.println("TabDetailPager.TabDetailPager()"+mUrl);
	}
	@Override
	public View initViews() {
		View v= View.inflate(mActivity, R.layout.tab_detail_pager, null);
        View head = View.inflate(mActivity, R.layout.list_news_head, null);
		
		vp_tab_detail_topnews = (ViewPager) head.findViewById(R.id.vp_tab_detail_topnews);
		tv_tabdetial_topnewstitle = (TextView) head.findViewById(R.id.tv_tabdetial_topnewstitle);
		topnews_indicator = (CirclePageIndicator) head.findViewById(R.id.topnews_indicator);
			
		lv_tab_detail_news = (ListView) v.findViewById(R.id.lv_tab_detail_news);
        lv_tab_detail_news.addHeaderView(head);		
			
		return v;
	}
	@Override
	public void initData() {
		// TODO Auto-generated method stub
		super.initData();
		//text.setText(newtabdata.title); //应该放在这里
		getDataFromServer();
	}

	private void getDataFromServer() {
		// TODO Auto-generated method stub
		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("获取到的json数据" + result);
				parseData(result, false);
			}
		
			@Override
			public void onFailure(HttpException error, String msg) {
				Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
				error.printStackTrace();			 
			}
		});
	}
	
	private void parseData(String result, boolean b) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		mTabDetailData = gson.fromJson(result, TabData.class);
		System.out.println(" 解析到的数据为:" + mTabDetailData);
				
		mTopNewsList = mTabDetailData.data.topnews;
		
		vp_tab_detail_topnews.setAdapter(new TopNewsAdapter());
		vp_tab_detail_topnews.setOnPageChangeListener(new MyOnPageChangeListener());
	
		topnews_indicator.setViewPager(vp_tab_detail_topnews);
		topnews_indicator.setOnPageChangeListener(new MyOnPageChangeListener());
		topnews_indicator.setSnap(true);
		
	    //拿到数据之后,现将第一个topnews的title设置到控件上。即默认显示第一条topnews
		String tilte0 = mTopNewsList.get(0).title;
		tv_tabdetial_topnewstitle.setText(tilte0);
		
		//下面的listview也要设置adapter去填充数据		
		listnewsData =mTabDetailData.data.news;
		MyListViewAdapter listViewAdapter = new MyListViewAdapter();
		lv_tab_detail_news.setAdapter(listViewAdapter);
	}
		
	/**
	 * topnews的填充adapter
	 */
	class TopNewsAdapter extends PagerAdapter {
		private BitmapUtils utils;
		public TopNewsAdapter() {
			utils = new BitmapUtils(mActivity);
			utils.configDefaultLoadingImage(R.drawable.topnews_item_default);
		}

		@Override
		public int getCount() {
			System.out.println("TabDetailPager.TopNewsAdapter.getCount()"+mTabDetailData.data.topnews.size());
			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);// 直接去下载,然后将下载的图片放到该view中
			container.addView(image);
			//image.setOnTouchListener(new TopNewsTouchListener());// 
			return image;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}
	
	class MyOnPageChangeListener implements OnPageChangeListener{
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub		
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			System.out
					.println("TabDetailPager.MyOnPageChangeListener.onPageSelected()");
			String tilte = mTopNewsList.get(arg0).title;
			tv_tabdetial_topnewstitle.setText(tilte);
		}	
	}


	class MyListViewAdapter extends BaseAdapter{
		private BitmapUtils bitmapUtils;
		public MyListViewAdapter() {
			bitmapUtils = new BitmapUtils(mActivity);
		    bitmapUtils.configDefaultLoadFailedImage(R.drawable.pic_item_list_default);
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listnewsData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View item_view =null;
			Holder holder = new Holder();	 
			if (convertView==null) {
				  item_view = View .inflate(mActivity, R.layout.list_news_item, null);
				  holder.iv_newpic = (ImageView) item_view.findViewById(R.id.iv_pic);
				  holder.tv_newsitme_title = (TextView) item_view.findViewById(R.id.tv_newsitme_title);
				  holder.tv_newsitme_date = (TextView) item_view.findViewById(R.id.tv_newsitme_date);
 
				  item_view.setTag(holder);				
			}else {
				item_view=convertView;				
				holder = (Holder) item_view.getTag();			 
			}			
			holder.tv_newsitme_title.setText(listnewsData.get(position).title);
			holder.tv_newsitme_date.setText(listnewsData.get(position).pubdate);
			bitmapUtils.display(holder.iv_newpic, listnewsData.get(position).listimage);
			return item_view;
		}			
	}	
	class Holder{
		ImageView iv_newpic ;
		TextView tv_newsitme_title ;
		TextView tv_newsitme_date;	 		
	}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36556次
    • 积分:1071
    • 等级:
    • 排名:千里之外
    • 原创:70篇
    • 转载:13篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论