今日头条 新闻频道展示

思路:用HorizontalScrollView 来对频道进行展示,用ViewPager + FragmentPagerAdapter + Fragment 方式来展示每个栏目下的新闻列表




一:对频道的添加展示




二:viewPager 新闻列表的展示




一:对频道的添加展示
创建了一个新闻类NewsClassify,进行新闻栏目的存储,之后根据栏目的多少动态创建TextView,并且设置其

setBackgroundResource()、setTextAppearance()、setTextColor()等属性,并添加监听


public class MainActivity extends ActionBarActivity {
	
	private HorizontalScrollView mHorizontalScrollView;
	private LinearLayout mRadioGroup_content;
	private LinearLayout ll_more_columns;
	private ViewPager mViewPager;
	
	/** 屏幕宽度 */
	private int mScreenWidth = 0;
	/** Item宽度 */
	private int mItemWidth = 0;
	/*
	 * 临时数据
	 */
	private List<NewsClassify> data = new ArrayList<NewsClassify>();
	
	private List<NewsFragment> fragments = new ArrayList<NewsFragment>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		initView();
		initFragment();
		
	}
	/*
	 * 初始化
	 */
	private void initView(){
		mScreenWidth = BaseTools.getWindowsWidth(this);
		mItemWidth = mScreenWidth / 7;// 一个Item宽度为屏幕的1/7
		
		mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.mHorizontalScrollView);
		mRadioGroup_content = (LinearLayout) findViewById(R.id.mRadioGroup_content);
		ll_more_columns = (LinearLayout) findViewById(R.id.ll_more_columns);
		mViewPager = (ViewPager) findViewById(R.id.mViewPager);
		
		addData();//添加数据
		
		int size = data.size();
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth, LayoutParams.WRAP_CONTENT);
		params.leftMargin = 5;
		params.rightMargin = 5;
		/*
		 * 四个字以上字显示不全的问题
		 */
		for(int i = 0;i < size;i ++){
			final TextView textView = new TextView(this);
			textView.setBackgroundResource(R.drawable.title_selected);
			//通过ColorStateList得到xml中的配置的颜色的。好多需要xml中配置的都要类似这样的映射xml文件
			textView.setTextColor(getResources().getColorStateList(R.drawable.title_color_selected));
			textView.setSingleLine(true);
			textView.setTextAppearance(this, R.style.TextStyle);
			textView.setGravity(Gravity.CENTER);
			textView.setPadding(5, 5, 5, 5);
			textView.setText(data.get(i).getTitle());
			textView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int size = data.size();
					for(int i = 0;i < size;i ++){
						View temp = mRadioGroup_content.getChildAt(i);
						if(temp != v){
							temp.setSelected(false);
						}else{
							temp.setSelected(true);
							mViewPager.setCurrentItem(i);
						}
					}
				}
			});
			mRadioGroup_content.addView(textView, params);
		}
	}
	
	private void selected(int position){
//		for (int i = 0; i < 2; i++) {
			View checkView = mRadioGroup_content.getChildAt(position);
			int k = checkView.getMeasuredWidth();
			int l = checkView.getLeft();
			int i2 = l - k / 2 ;
			
			mHorizontalScrollView.scrollTo(i2, 0);
//		}
		
		
		boolean bool = false;
		int size = data.size();
		for(int i = 0;i < size;i ++){
			View view = mRadioGroup_content.getChildAt(i);
			if(i == position){
				bool = true;
			}else{
				bool = false;
			}
			view.setSelected(bool);
		}
	}
	/*
	 * 初始化Fragment
	 */
	private void initFragment(){
		int size = data.size();
		for(int i = 0;i < size;i ++){
			Bundle bundle = new Bundle();
    		bundle.putString("text", data.get(i).getTitle());
			NewsFragment fragment = new NewsFragment();
			fragment.setArguments(bundle);
			fragments.add(fragment);
		}
		
		NewsFragmentPagerAdapter adapter = new NewsFragmentPagerAdapter(getSupportFragmentManager(), fragments);
		
		mViewPager.setAdapter(adapter);

		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				mViewPager.setCurrentItem(arg0);
				selected(arg0);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	
	private void addData(){
		NewsClassify news = new NewsClassify();
		news.setId(0);
		news.setTitle("头条");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(1);
		news.setTitle("手机");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(2);
		news.setTitle("科技");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(3);
		news.setTitle("数码");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(4);
		news.setTitle("互联网");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(5);
		news.setTitle("娱乐");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(6);
		news.setTitle("智能手机");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(7);
		news.setTitle("财经");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(8);
		news.setTitle("体育");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(9);
		news.setTitle("汽车");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(10);
		news.setTitle("段子");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(11);
		news.setTitle("上海");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(12);
		news.setTitle("图片");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(13);
		news.setTitle("房产");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(14);
		news.setTitle("电台");
		data.add(news);
		
		news = new NewsClassify();
		news.setId(15);
		news.setTitle("原创");
		data.add(news);
	}
	
}

二:viewPager 新闻列表的展示




viewpager 适配器为FragmentPagerAdapter,并且用Fragment进行新闻列表展示,当然也可以用
PagerAdapter 这时就不需要Fragment




谷歌官方认为,ViewPager应该和Fragment一起使用时,此时ViewPager的适配器是FragmentPagerAdapter,
当你实现一个FragmentPagerAdapter,你必须至少覆盖以下方法:


getCount()


getItem()


如果ViewPager没有和Fragment一起,ViewPager的适配器是PagerAdapter,它是基类提供适配器来填充页面ViewPager内部,
当你实现一个PagerAdapter,你必须至少覆盖以下方法:


instantiateItem(ViewGroup, int)


destroyItem(ViewGroup, int, Object)


getCount()


isViewFromObject(View, Object)


class NewsFragmentPagerAdapter::

public class NewsFragmentPagerAdapter extends FragmentPagerAdapter {
	
	private List<NewsFragment> list = null;
	public NewsFragmentPagerAdapter(FragmentManager fm, List<NewsFragment> list) {
		super(fm);
		// TODO Auto-generated constructor stub
		this.list = list;
	}

	@Override
	public Fragment getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

}

class NewsFragment::


public class NewsFragment extends Fragment {
	
	private String text = "";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		text = bundle != null ? bundle.getString("text") : "";
	}
	//新闻列表的展示就是用这个
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_item, null);
		TextView textView = (TextView) view.findViewById(R.id.textView);
		textView.setText(text);
		
		return view;
	}

}


你可能感兴趣的:(今日头条 新闻频道展示)