ActionBar tab +fragment, Viewpager+pager Title Strip, 소스 프레임워크 ViewPageIndicator와 ViewPager
액션은 탭 라벨을 실현하고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>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.