Very simple viewpager+fragmen implementation similar to Marketplace app structure

Source: Internet
Author: User

First of all, the idea in an activity in the main layout to join the SUPPORTV4 rack package Viewpager,viewpager equivalent to a ListView, in Viewpager we want to put the fragment, to Viewpager do monitoring, Achieve the desired effect, the code is simple to understand, always on the code.

Main class

Package Com.example.fragtest;import Java.util.arraylist;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.app.activity;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.textview;import Android.os.build;public class MainActivity extends fragmentactivity {private arraylist<fragment> list;private viewpager viewpager;private TextView [] tvs=new TEXTVIEW[4];p rivate int pos=0; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); viewpager= (Viewpager) Findviewbyid (R.id.viewPager); tvs[0]= (TextView) Findviewbyid (r.id.tv_f); Tvs[0].setbackgroUndcolor (0x99999999); tvs[1]= (TextView) Findviewbyid (r.id.tv_s); tvs[2]= (TextView) Findviewbyid (r.id.tv_t); tvs[3]= (TextView) Findviewbyid (R.ID.TV_TF); First Mfirst=new first (); Second mssecond=new Second (); third mthird=new third (); Fourth mfourth=new fourth (); List=new arraylist<fragment> ( ); List.add (Mfirst); List.add (Mssecond); List.add (Mthird); List.add (Mfourth); Viewpager.setadapter (New MyFragAdapter (Getsupportfragmentmanager (), this, list)); Viewpager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {//TODO Auto-generated method Stubfor (int i=0;i<4;i++) {if (i==arg0) {tvs[i].setbackgroundcolor (0x99999999);} Else{tvs[i].setbackgroundcolor (0xFFFFFFFF);}} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic vo ID onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); for (TextView Tv:tvs) { Tv.setonclicklistener (New Onclicklistener () {@Overridepublic void onclicK (view view) {//TODO auto-generated method Stubswitch (View.getid ()) {case R.id.tv_f:pos=0;break;case R.id.tv_s:pos=1; Break;case r.id.tv_t:pos=2;break;case R.id.tv_tf:pos=3;break;default:break;} for (int i=0;i<4;i++) {if (I==pos) {tvs[i].setbackgroundcolor (0x99999999);} Else{tvs[i].setbackgroundcolor (0xFFFFFFFF);}} Viewpager.setcurrentitem (POS);}});}} @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;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
Adapter

Package Com.example.fragtest;import Java.util.arraylist;import Android.content.context;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;public class Myfragadapter extends Fragmentpageradapter{private Context Mcontext;  Private arraylist<fragment> mlist;public myfragadapter (Fragmentmanager fm,context context,ArrayList< Fragment> list) {super (FM); mcontext=context;if (list!=null) {mlist=list;} Else{mlist=new arraylist<fragment> ();} Todo auto-generated constructor stub} @Overridepublic Fragment GetItem (int arg0) {//Todo auto-generated method Stubretu RN Mlist.get (arg0);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn mlist.size ();}}

Four fragment

Package Com.example.fragtest;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 ;p Ublic class First extends fragment{@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated method Stubview view=inflater.inflate (R.layout.view, container, FALSE); TextView tv= (TextView) View.findviewbyid (r.id.tv_name); Tv.settext ("first"); return view;}}

Package Com.example.fragtest;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 ;p ublic class Second extends fragment{@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated method Stubview view=inflater.inflate (R.layout.view, container, FALSE); TextView tv= (TextView) View.findviewbyid (r.id.tv_name); Tv.settext ("second"); return view;}}

Package Com.example.fragtest;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 ;p Ublic class Third extends Fragment{public View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {//TODO auto-generated method Stubview view=inflater.inflate (R.layout.view, container, false); TextView tv= (TextView) View.findviewbyid (r.id.tv_name); Tv.settext ("third"); return view;}}

Package Com.example.fragtest;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 ;p Ublic class Fourth extends Fragment{public View Oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {//TODO auto-generated method Stubview view=inflater.inflate (R.layout.view, container, false); TextView tv= (TextView) View.findviewbyid (r.id.tv_name) Tv.settext ("Fourth"); return view;}}

Main layout

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<android.support.v4.view.viewpager
Android:layout_above= "@+id/ll_bottom"
Android:id= "@+id/viewpager"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
/>
<linearlayout
Android:id= "@+id/ll_bottom"
Android:layout_alignparentbottom= "true"
Android:layout_width= "Match_parent"
android:orientation= "Vertical"
android:layout_height= "50DP" >
<textview
Android:layout_width= "Match_parent"
android:layout_height= "2DP"
android:background= "#999999"/>
<linearlayout android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<textview
Android:id= "@+id/tv_f"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
android:gravity= "Center"
android:text= "Home"
Android:textsize= "16sp"/>
<textview
Android:layout_width= "2DP"
android:layout_height= "Match_parent"
android:background= "#999999"/>

<textview
Android:id= "@+id/tv_s"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
android:gravity= "Center"
Android:text= "Hot Posts"
Android:textsize= "16sp"/>
<textview
Android:layout_width= "2DP"
android:layout_height= "Match_parent"
android:background= "#999999"/>

<textview
Android:id= "@+id/tv_t"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
android:gravity= "Center"
Android:text= "Activities"
Android:textsize= "16sp"/>
<textview
Android:layout_width= "2DP"
android:layout_height= "Match_parent"
android:background= "#999999"/>

<textview
Android:id= "@+id/tv_tf"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
android:gravity= "Center"
Android:text= "Me"
Android:textsize= "16sp"/>
</LinearLayout>
</LinearLayout>


</RelativeLayout>


Fragment layout

<?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=" match_parent "    android:o rientation= "vertical" >    <textview     android:id= "@+id/tv_name" android:layout_width= "Match_    Parent "    android:layout_height=" match_parent "    android:gravity=" center "    android:textsize=" 25SP "    android:text= "First"/></linearlayout>
    http://download.csdn.net/detail/u012303938/8550513


Very simple viewpager+fragmen implementation similar to Marketplace app structure

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.