ActionBar tab +fragment, Viewpager+pager Title Strip, 소스 프레임워크 ViewPageIndicator와 ViewPager

16069 단어

액션은 탭 라벨을 실현하고fragment와 결합시킨다. 왜냐하면 뉴스 클라이언트를 써야 하기 때문이다. 이탭 네비게이션은 필수적이기 때문이다. 여기에 제가 몇 가지 작은 연습을 써서 여러분들이 관통되기를 바란다.
 
1actionbar 설정tab +fragment
레이아웃은 레이아웃이고 어떤 레이아웃이든 Id를 추가할 수 있습니다.
package com.example.demoforactionbar; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity implements ActionBar.TabListener { private static final String SELECTED_ITEM = "selected_item"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ActionBar actionBar = getActionBar(); //   ActionBar     :Tab  
 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //       Tab ,    Tab         
        actionBar.addTab(actionBar.newTab().setText("   ") .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("   ") .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("   ") .setTabListener(this)); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState.containsKey(SELECTED_ITEM)) { //             Fragment 
 getActionBar().setSelectedNavigationItem( savedInstanceState.getInt(SELECTED_ITEM)); } } @Override public void onSaveInstanceState(Bundle outState) { //       Fragment       Bundle 
 outState.putInt(SELECTED_ITEM, getActionBar().getSelectedNavigationIndex()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } //    Tab         
 @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { //       Fragment  
        Fragment fragment = new DummyFragment(); //     Bundle  ,   Fragment    
        Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); //  fragment    
 fragment.setArguments(args); //   FragmentTransaction  
        FragmentTransaction ft = getFragmentManager() .beginTransaction(); //   fragment   Activity  container  
 ft.replace(R.id.container, fragment); //     
 ft.commit(); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } }

 
fragment
 
package com.example.demoforactionbar; import android.app.Fragment; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class DummyFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; //           Fragment   View  
 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER_HORIZONTAL); //      Fragment      Bundle
        Bundle args = getArguments(); //   TextView     
        textView.setText(args.getInt(ARG_SECTION_NUMBER) + ""); textView.setTextSize(30); //    TextView
        return textView; } }

 
2 .Viewpager + pagerTopStrip
 
 
 
xml:
 
<?xml version="1.0" encoding="utf-8" ?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent">
    <!--           -->
    <android.support.v4.view.PagerTitleStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:textColor="#fff" android:paddingTop="4dp" android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>

 
 
MainActivity:
package com.example.demoforactionbar; import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends FragmentActivity implements ActionBar.TabListener { ViewPager viewPager; ActionBar actionBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //   ActionBar  
        actionBar = getActionBar(); //   ViewPager
        viewPager = (ViewPager) findViewById(R.id.pager); //     FragmentPagerAdapter  ,      ViewPager    Fragment
        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter( getSupportFragmentManager()) { //    position   Fragment
 @Override public Fragment getItem(int position) { Fragment fragment = new DummyFragment(); Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); return fragment; } //        i   Adapter       Fragment
 @Override public int getCount() { return 3; } //            Fragment   
 @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "   "; case 1: return "   "; case 2: return "   "; } return null; } }; //   ActionBar  Tab    
 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //   pagerAdapter        Fragment //   Fragment      Tab  
        for (int i = 0; i < pagerAdapter.getCount(); i++) { actionBar.addTab(actionBar.newTab() .setText(pagerAdapter.getPageTitle(i)) .setTabListener(this)); } //  ViewPager    FragmentPagerAdapter
        viewPager.setAdapter(pagerAdapter); ////  ViewPager         
 viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() { //  ViewPager   Fragment          
 @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } //    Tab         
 @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); //
 } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } }

 
Dummy Fragment 부류의 가방을 V4 가방으로 바꾸면 돼요. 안 바꾸면 틀려요.
public class DummyFragment extends android.support.v4.app.Fragment

 
 
3 소스 오픈 프레임워크 ViewPageIndicator 및 ViewPager
프레임이 스스로 검색합니다.방법은 두 번째와 비슷하다.
 
XML
 
<LinearLayout 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:background="#C5DAED" android:orientation="vertical" >

    <include layout="@layout/top" />

    <com.viewpagerindicator.TabPageIndicator android:id="@+id/id_indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" >
    </com.viewpagerindicator.TabPageIndicator>

    <android.support.v4.view.ViewPager android:id="@+id/id_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>

</LinearLayout>

 
MainActivity:
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.view.Window; import com.viewpagerindicator.TabPageIndicator; public class MainActivity extends FragmentActivity { private ViewPager mViewPager; private TabPageIndicator mTabPageIndicator; private TabAdapter mAdapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } private void initView() { //      
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager); mTabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mAdapter = new TabAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mAdapter); mTabPageIndicator.setViewPager(mViewPager, 0); // mTabPageIndicator   ViewPager ,   0      
 } }

 
Adapter:
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 String[] TITLES = new String[] {"ACE", "1", "2", "3", "4", "5" }; public TabAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { TabFragment fragment = new TabFragment(arg0); return fragment; } @Override public int getCount() { return TITLES.length; } @Override public CharSequence getPageTitle(int position) { return TITLES[position]; } }

 
 
TabFragment
 
public class TabFragment extends Fragment { private int pos; public TabFragment(int pos) { this.pos = pos; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frag, container, false); TextView tv = (TextView) view.findViewById(R.id.id_tv); tv.setText(TabAdapter.TITLES[pos]); return view; } }

 
Fragmen 레이아웃 frag.xml
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#ffffff" android:layout_height="match_parent" >

    <TextView android:id="@+id/id_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textStyle="bold" android:textSize="22sp" android:text="hello ACE" />

</RelativeLayout>

좋은 웹페이지 즐겨찾기