Android App Classic Main interface frame two: Imitation netease news client, CSDN client (Fragment viewpager)

Source: Internet
Author: User

Another main interface style is the NetEase news, Phoenix News and the New Sina blog (read version) as the representative. Using Viewpager+fragment, which is not the general view in the Viewpager adapter. But fragment. So the adapter can not inherit Pageradapter, but to inherit Fragmentpageradapter, this is in the Android.support.v4.app.FragmentPagerAdapter package. A bit of a wonderful thing is, fragmentpageradapter only in this bag, in the android.app.* this bag below it.

You will find that you can only use android.support.v4.app.* to pack the following items.

The Fragmentmanager in the two bags are not universal, and the fragment provided in the two packages are not quite the same. If you inherit fragment under Android.app.*, you cannot write the constructor again. Can only be used with the default. There is this restriction in V4 's bag.

Is NetEase news, Phoenix News, Sina Blog:



About The Imitation NetEase News client code has been very much, I mainly based on the open source of this csdnclient production, preparation step by step. This is a Daniel's work on the Oschina, the reference link in 5 steps to achieve.

I looked at the code for it. are dyed together. For example, there is no additional three packages required to complete this navigation. And his resources are together.

So prepare yourself to play, and by the way record the problems in development.

The first step: The top navigation bar

That is, "NetEase news" Four characters of this column.

Layout file Head_title_panel.xml:

<?

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=" wrap_content "android:background=" @color/light_blue "android:orientation=" Horizont Al "> <imageview android:id=" @+id/headicon "android:layout_width=" Wrap_content "Android:layo ut_height= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_marginleft= "8DP" and roid:layout_marginright= "4DP" android:src= "@drawable/biz_navigation_tab_news_pressed"/> <imageview Android:id= "@+id/headdivider" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "center_vertical" android:layout_marginleft= "4DP" android:layout_marginright= "4DP" android:src= "@drawable/base_action_bar_back_divider"/> <textview android:id= "@+id/headtv" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_marginleft= "4DP" android:layout_weight= "1" android:text= "csdn info" android:textcolor= "@color/white" Android:text Size= "21SP" android:textstyle= "bold" > </TextView></LinearLayout>

for the convenience of future operation, I mapped it into a Headtitlepanel.java file, and I can see that this kind of writing is a little different from the definition of the upper and lower panel.

Package Org.yanzi.ui;import Org.yanzi.csdnproject.r;import Android.content.context;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.imageview;import Android.widget.relativelayout;import Android.widget.textview;public Class Headtitlepanel extends Relativelayout {private Context mcontext;private TextView mheadtv;private ImageView Mheadicon; Private ImageView Mheaddivider;public Headtitlepanel (context context, AttributeSet Attrs) {Super (context, attrs);// TODO auto-generated Constructor Stubmcontext = context; View parent = Layoutinflater.from (Mcontext). Inflate (R.layout.head_title_panel, this, true); Mheadtv = (TextView) Parent.findviewbyid (R.ID.HEADTV);}}

Step Two: Viewpager's navigation bar

This originally I was prepared to encapsulate a, online also practical RadioButton package, considering it this navigation bar or not fixed length can slide, time reason temporarily not encapsulated, using open source Android-viewpagerindicator-master.zip This package, this person's GitHub link: Https://github.com/JakeWharton will change the name of the library directory viewpagerindicator_library in. There are several kinds of indicator in this, we mainly use tabpageindicator this.

Step three: The layout of mainactivity:

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:backg Round= "#eee" tools:context= ". Mainactivity "> <org.yanzi.ui.headtitlepanel android:id=" @+id/head_title_panel "Android:layout_widt H= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true"/> <com. Viewpagerindicator. Tabpageindicator android:id= "@+id/page_indicator" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:layout_below= "@id/head_title_panel" android:background= "@color/transparentblue "/> <android.support.v4.view.viewpager android:id=" @+id/view_pager "android:layout_width=" Match_pa Rent "android:layout_height=" match_parent "android:layout_below=" @id/page_indicator "/></rElativelayout> 
Mainactivity.java

Package Org.yanzi.csdnproject;import Org.yanzi.viewpager.adapter.tabadapter;import Com.viewpagerindicator.tabpageindicator;import Android.os.bundle;import Android.app.activity;import Android.app.fragmentmanager;import Android.view.menu;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 mpageindicator;private Viewpager mviewpager;private Fragmentpageradapter fragpageradapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Initui (); fragpageradapter = new TabAdapter ( Getsupportfragmentmanager ()); Mviewpager.setadapter (Fragpageradapter); Mpageindicator.setviewpager (MViewPager, 0) ;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main,menu); return true;} private void Initui () {mpageindicator = (tabpageindicator) Findviewbyid (r.id.page_indicator); Mviewpager = (ViewPager) Findviewbyid (R.id.view_pager);}}

Surprisingly simple, more simple than simply using fragment, the reason is just need to plug fragment into the adapter is OK. The adapter has done a fragment switch for us. What we can do is to infer if there is already a new fragment in the adapter.

The following points need to be noted:

1. In Styles.xml it defines the style:

    <style name= "MyTheme" parent= "Appbasetheme" > <item name= "Vpitabpageindicatorstyle" > @style/mywidget. tabpageindicator</item> <item name= "Android:windowbackground" > @drawable/init_pic</item> &L T;item name= "Android:windownotitle" >true</item> <item name= "Android:animationduration" >5000</ item> <item name= "Android:windowcontentoverlay" > @null </item> </style> <style name= "My  Widget.tabpageindicator "parent=" Widget > <item name= "android:gravity" >center</item> <item Name= "Android:background" > @drawable/vpi__tab_indicator</item> <item name= "Android:paddingleft" > 22dip</item> <item name= "android:paddingright" >22dip</item> <item name= "android:padding Top ">8dp</item> <item name=" Android:paddingbottom ">8dp</item> <item name=" Android:te Xtappearance "> @style/mytextapPearance. tabpageindicator</item> <item name= "android:textsize" >16sp</item> <item name= "android:m        Axlines ">1</item> </style> <style name=" Mytextappearance.tabpageindicator "parent=" Widgets "> <item name= "Android:textstyle" >bold</item> <item name= "Android:textcolor" > @color/black</it Em> </style>

This is dependent on the packets that are in the lead.

2, it used the Android:windowbackground property, so the app opens the moment there will be a picture pop-up, and then set the mainactivity layout background for the android:background= "#eee", and replaced the picture. Suppose you don't set android:background= "#eee" will always see this picture does not disappear.

3, because of the opening reason, mainactivity only can inherit from Fragmentactivity.

Fourth Step: Mainfragment.java, this class inherits fragment. And it's under the android.support.v4.app.Fragment.

package Org.yanzi.fragment;import Org.yanzi.csdnproject.r;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;public class Mainfragment extends Fragment {private int mnewstype = 0; @Overridepublic View oncrea Teview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO auto-generated method StubView V = Inflater.inflate (R.layout.tab_item_fragment_main, NULL); TextView tip = (TextView) V.findviewbyid (R.id.id_tip); Bundle B = getarguments (); String title = b.getstring ("TITLES"); Tip.settext (title); return v;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.onactivitycreated (savedinstancestate);}}  
is to make a layout, and then get the parameters and show it.

Fifth Step: Viewpager Adapter Tabadapter.java

package Org.yanzi.viewpager.adapter;import Org.yanzi.constant.constant;import Org.yanzi.fragment.mainfragment;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;public class Tabadapter extends Fragmentpageradapter {public TabAdapter ( Fragmentmanager FM) {super (FM);//TODO auto-generated constructor stub} @Overridepublic Fragment GetItem (int position) {/ /TODO auto-generated method Stubmainfragment fragment = new Mainfragment (); Bundle B = new bundle (); String title = constant.titles[position];b.putstring ("TITLES", title); Fragment.setarguments (b); return fragment;} @Overridepublic charsequence getpagetitle (int position) {//TODO auto-generated method Stubreturn constant.titles[ Position];} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn Constant.TITLES.length;}}  

The main is to rewrite the GetItem, here to instantiate fragment, at the same time to set the number of passes.

In fact, it can be passed

Fragmentmanager
follow the tag to find out if the fragment exists, and then instantiate it.

Other than that. Getpagetitle This interface must be overridden. In fact, the implementation of the Tabpageindicator.java class can be found, it needs to pass in a viewpager, and then get the adapter. Get the title from the adapter. As a result of using this open source package, can no longer use the Viewpager Setonpagechangelistener interface, only with Mpageindicator.setonpagechangelistener (listener) to listen.

Complete, source code link: Link: http://pan.baidu.com/s/1bn4EFbp password:dx4s

Effects such as the following:






Android App Classic Main interface frame two: Imitation netease news client, CSDN client (Fragment viewpager)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.