@Override public void onClick(View v) { asyncTask = new LoadNewsAsyncTask(); switch (v.getId()) { case R.id.loadmore_btn: asyncTask.execute(CATEGORY_TYPE, mNewsData.size(), false); break; case R.id.titlebar_refresh: asyncTask.execute(CATEGORY_TYPE, 0, true); break; } }
@Override public void onClick(View v) { loadNewsAsyncTask = new LoadNewsAsyncTask(); switch (v.getId()) { case R.id.loadmore_btn: // 获取该栏目下新闻 // getSpeCateNews(mCid,mNewsData,mNewsData.size(),false); // 通知ListView进行更新 // mNewsListAdapter.notifyDataSetChanged(); loadNewsAsyncTask.execute(mCid, mNewsData.size(), false); break; case R.id.titlebar_refresh: loadNewsAsyncTask.execute(mCid, 0, true); break; } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHolder = ((MainActivity) getActivity()).getViewHolder(); mNewsManager = new NewsManager(); loadMoreLayout = getActivity().getLayoutInflater().inflate(R.layout.loadmore, null); mLoadMoreBtn = (Button) loadMoreLayout.findViewById(R.id.loadmore_btn); mHeadView = getActivity().getLayoutInflater().inflate(R.layout.first_image, null); firstImage = (ImageView) mHeadView.findViewById(R.id.firstImage); asyncTask.execute(CATEGORY_TYPE, 0, true); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mInflater = getLayoutInflater(); mNewsData = new ArrayList<HashMap<String, Object>>(); mNewsList = (ListView) findViewById(R.id.newslist); mTitlebarRefresh = (Button) findViewById(R.id.titlebar_refresh); mLoadnewsProgress = (ProgressBar) findViewById(R.id.loadnews_progress); mTitlebarRefresh.setOnClickListener(loadMoreListener); // 把px转换成dip mColumnWidthDip = DensityUtil.px2dip(this, COLUMNWIDTHPX); mFlingVelocityDip = DensityUtil.px2dip(this, FLINGVELOCITYPX); // 获取新闻分类 String[] categoryArray = getResources().getStringArray(R.array.categories); // 把新闻分类保存到List中 final List<HashMap<String, Category>> categories = new ArrayList<HashMap<String, Category>>(); // 分割新闻类型字符串 for (int i = 0; i < categoryArray.length; i++) { String[] temp = categoryArray[i].split("[|]"); if (temp.length == 2) { int cid = StringUtil.String2Int(temp[0]); String title = temp[1]; Category type = new Category(cid, title); HashMap<String, Category> hashMap = new HashMap<String, Category>(); hashMap.put("category_title", type); categories.add(hashMap); } } // 默认选中的新闻分类 mCid = 1; mCatName = "关于我们"; // 创建Adapter,指明映射字段 // SimpleAdapter categoryAdapter = new SimpleAdapter(this, categories, // R.layout.category_title,new String[]{"category_title"}, new int[]{R.id.category_title}); CustomSimpleAdapter categoryAdapter = new CustomSimpleAdapter( this, categories, R.layout.category_title, new String[] {"category_title"}, new int[] {R.id.category_title}); GridView category = new GridView(this); category.setColumnWidth(mColumnWidthDip); // 每个单元格宽度 category.setNumColumns(GridView.AUTO_FIT); // 单元格数目 category.setGravity(Gravity.CENTER); // 设置对其方式 // 设置单元格选择是背景色为透明,这样选择时就不现实黄色背景 // category.setSelector(new ColorDrawable(Color.TRANSPARENT)); int width = mColumnWidthDip * categories.size(); LayoutParams params = new LayoutParams(width, LayoutParams.FILL_PARENT); // 更新category宽度和高度,这样category在一行显示 category.setLayoutParams(params); // 设置适配器 category.setAdapter(categoryAdapter); // 把category加入到容器中 LinearLayout categoryList = (LinearLayout) findViewById(R.id.category_layout); categoryList.addView(category); // 添加单元格点击事件 category.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView categoryTitle; // 恢复每个单元格背景色 for (int i = 0; i < parent.getChildCount(); i++) { categoryTitle = (TextView) (parent.getChildAt(i)); categoryTitle.setBackgroundDrawable(null); categoryTitle.setTextColor(0XFFADB2AD); } // 设置选择单元格的背景色 categoryTitle = (TextView) (parent.getChildAt(position)); categoryTitle.setBackgroundResource(R.drawable.categorybar_item_background); categoryTitle.setTextColor(0XFFFFFFFF); // 获取选中的新闻分类id mCid = categories.get(position).get("category_title").getCid(); mCatName = categories.get(position).get("category_title").getTitle(); // 获取该栏目下新闻 // getSpeCateNews(mCid,mNewsData,0,true); // 通知ListView进行更新 // mNewsListAdapter.notifyDataSetChanged(); loadNewsAsyncTask = new LoadNewsAsyncTask(); loadNewsAsyncTask.execute(mCid, 0, true); } }); // 箭头 final HorizontalScrollView categoryScrollView = (HorizontalScrollView) findViewById(R.id.category_scrollview); Button categoryArrowRight = (Button) findViewById(R.id.category_arrow_right); categoryArrowRight.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { categoryScrollView.fling(mFlingVelocityDip); } }); // 填充ListView,在里边显示新闻列表 /*List<HashMap<String, String>> newsData = new ArrayList<HashMap<String, String>>(); for(int i = 0; i < 10; i++){ HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("newslist_item_title", "中天"); hashMap.put("newslist_item_digest", "中天信息公司成立了"); hashMap.put("newslist_item_source", "来自中天"); hashMap.put("newslist_item_ptime", "2013-03-07"); newsData.add(hashMap); }*/ // 获取指定栏目的新闻列表 getSpeCateNews(mCid, mNewsData, 0, true); mNewsListAdapter = new SimpleAdapter( this, mNewsData, R.layout.newslist_item, new String[] { "newslist_item_title", "newslist_item_digest", "newslist_item_source", "newslist_item_ptime" }, new int[] { R.id.newslist_item_title, R.id.newslist_item_digest, R.id.newslist_item_source, R.id.newslist_item_ptime }); View loadMoreLayout = mInflater.inflate(R.layout.loadmore, null); mNewsList.addFooterView(loadMoreLayout); mNewsList.setAdapter(mNewsListAdapter); mNewsList.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, NewsDetailsActivity.class); // 把需要的信息放到Intent中; intent.putExtra("categoryName", mCatName); intent.putExtra("newsDate", mNewsData); intent.putExtra("position", position); startActivity(intent); } }); mLoadMoreBtn = (Button) findViewById(R.id.loadmore_btn); mLoadMoreBtn.setOnClickListener(loadMoreListener); loadNewsAsyncTask = new LoadNewsAsyncTask(); loadNewsAsyncTask.execute(mCid, 0, true); }