[转载]Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架 – Hongyang – 博客频道 – CSDN.NET.
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23513993
本来准备下载个CSDN的客户端放手机上,没事可以浏览浏览资讯,下载了官方的之后,发现并不能很好的使用。恰好搜到一个大神自己写的csdn的app,下载安装了一下,感觉很不错,也很流畅,基本满足了我们 日常浏览的需求。
app效果图:
我会在博客中完整的介绍这个项目的制作,第一篇当然是整个项目的整体结构了。
效果图:
1、头部的布局文件,这个很简单:
<!--?xml version="1.0" encoding="utf-8"?-->
就显示一个图标和标题。
2、主布局文件:
一个TabPageIndicator和一个ViewPager。
3、主Activity
package com.zhy.csdndemo; import com.viewpagerindicator.TabPageIndicator; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { private TabPageIndicator mIndicator ; private ViewPager mViewPager ; private FragmentPagerAdapter mAdapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPager = (ViewPager) findViewById(R.id.id_pager); mAdapter = new TabAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mAdapter); mIndicator.setViewPager(mViewPager, 0); } }
TabAdapter.java
package com.zhy.csdndemo; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class TabAdapter extends FragmentPagerAdapter { public static final String[] TITLES = new String[] { "业界", "移动", "研发", "程序员杂志", "云计算" }; public TabAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { MainFragment fragment = new MainFragment(arg0); return fragment; } @Override public CharSequence getPageTitle(int position) { return TITLES[position % TITLES.length]; } @Override public int getCount() { return TITLES.length; } }
MainFragment.java
package com.zhy.csdndemo; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("ValidFragment") public class MainFragment extends Fragment { private int newsType = 0; public MainFragment(int newsType) { this.newsType = newsType; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_item_fragment_main, null); TextView tip = (TextView) view.findViewById(R.id.id_tip); tip.setText(TabAdapter.TITLES[newsType]); return view; } }
4、在styles.xml中自定义Theme
在AndroidManifest中注册使用:
<!--?xml version="1.0" encoding="utf-8"?-->
总体还是很简单的,但是效果很不错啊,现在不是流行Fragment么~好了 ,如果这篇文章对你有帮助,赞一个~