[Android] News client main interface section

Source: Internet
Author: User

When we use activity plus fragment , each interface has to create a fragment, each Fragment inside all to rewrite onCreate (),oncreateview (), onactivitycreated (), method, we create a new base class basefragment to override these methods

Basefragment needs to define an abstract method Initview (), which is used to initialize the View object, which requires subclasses to implement, our parent class Layoutinflater objects in .

Basefragment needs to define an abstract method initdata (), which initializes the data and requires subclasses to implement

 Packagecom.qingguow.news.fragment;ImportAndroid.content.Context;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Basic class *@authorTaoshihan **/ Public Abstract classBasefragmentextendsFragment { Publiccontext Context; @Override Public voidonactivitycreated (@Nullable Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. onactivitycreated (savedinstancestate);    InitData (savedinstancestate); } @Override Public voidonCreate (@Nullable Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate); Context=getactivity (); } @Override @Nullable PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancest ATE) {View View=Initview (Inflater); returnview; }    /*** Initialize View object *@paramInflater *@return     */     Public AbstractView Initview (Layoutinflater inflater); /*** Initialize data *@paramBundle *@return     */     Public Abstract voidInitData (Bundle savedinstancestate);}

implement Banner Carousel in the main Fragment

Add a node to the fragment_index.xml layout file <android.support.v4.view.viewpager/>

Gets the viewpager object, via the Findviewbyid () method

Call the Setadapter () method of the viewpager Object , set the adapter, parameters:pageradapter Object

Implement abstract methods using anonymous inner classes,GetCount (),isviewfromobject (),instantiateitem ( ) , Destroyitem ()

Override the GetCount () method to return the number of carousel

Override the isviewfromobject () method, return a Boolean value,

Override the Instantiateitem () method to add the current view object to the viewgroup object. Returns the current object

Override the Destroyitem () method to deletethe View of the specified position (position) from the current container

 Packagecom.qingguow.news.fragment;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;ImportCOM.QINGGUOW.NEWS.R;/*** Home *@authorTaoshihan **/ Public classIndexfragmentextendsbasefragment {PrivateList<view>banners; @Override PublicView Initview (Layoutinflater inflater) {View View=inflater.inflate (R.layout.fragment_index,NULL); //Banner Partbanners=NewArraylist<>(); TextView TV=NewTextView (context); Tv.settext (Test);        BANNERS.ADD (TV); TextView TV2=NewTextView (context); Tv2.settext ("Test 2");        Banners.add (TV2); Viewpager VP=(Viewpager) View.findviewbyid (R.id.vp_banner); Vp.setadapter (NewPageradapter () {@Override Public voidDestroyitem (ViewGroup container,intposition, Object object)            {Container.removeview (Banners.get (position)); } @Override PublicObject Instantiateitem (ViewGroup container,intposition)                {Container.addview (Banners.get (position)); returnBanners.get (position); } @Override Public intGetCount () {returnbanners.size (); } @Override Public BooleanIsviewfromobject (View arg0, Object arg1) {returnarg0==arg1;        }        }); returnview; } @Override Public voidInitData (Bundle savedinstancestate) {//TODO auto-generated Method Stub            }}

[Android] News client main interface section

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.