2014-10-21 5 views
1

조각을 사용하여 스 와이프보기를 만들려면 http://developer.android.com/training/implementing-navigation/lateral.html을 팔로우하고 있습니다.스 와이프보기에 작업 표시 줄을 추가하려면 어떻게해야하나요?

는 특히, 나는 코드

public class CollectionDemoActivity extends FragmentActivity { 
    // When requested, this adapter returns a DemoObjectFragment, 
    // representing an object in the collection. 
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; 
    ViewPager mViewPager; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_collection_demo); 

     // ViewPager and its adapters use support library 
     // fragments, so use getSupportFragmentManager. 
     mDemoCollectionPagerAdapter = 
       new DemoCollectionPagerAdapter(
         getSupportFragmentManager()); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mDemoCollectionPagerAdapter); 
    } 
} 

// Since this is an object collection, use a FragmentStatePagerAdapter, 
// and NOT a FragmentPagerAdapter. 
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { 
    public DemoCollectionPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new DemoObjectFragment(); 
     Bundle args = new Bundle(); 
     // Our object is just an integer :-P 
     args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 100; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return "OBJECT " + (position + 1); 
    } 
} 

// Instances of this class are fragments representing a single 
// object in our collection. 
public static class DemoObjectFragment extends Fragment { 
    public static final String ARG_OBJECT = "object"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) { 
     // The last two arguments ensure LayoutParams are inflated 
     // properly. 
     View rootView = inflater.inflate(
       R.layout.fragment_collection_object, container, false); 
     Bundle args = getArguments(); 
     ((TextView) rootView.findViewById(android.R.id.text1)).setText(
       Integer.toString(args.getInt(ARG_OBJECT))); 
     return rootView; 
    } 
} 

의 블록을 촬영하고 내 필요에 맞게 확장했습니다.

화면 상단에 활동 이름 (가운데에 있음)과 앱 아이콘 및 상위 활동으로 돌아 가기위한 화살표가 표시되도록 조치 바를 수정하려면 어떻게해야합니까? 왼쪽)?

나는 위의 코드에서 CollectionDemoActivity에서 onCreate에

final ActionBar actionBar = getActionBar(); 
actionBar.setHomeButtonEnabled(true); 

를 추가 시도했다, 그러나 이것은 내 응용 프로그램 충돌이 발생합니다.

편집 :

내 styles.xml은

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
<style name="AppTheme" parent="AppBaseTheme"> 

내 매니페스트는 응용 프로그램의 내부 활동 태그 ...

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

포함되어 포함되어 있습니다. 어떤 액티비티도 애플리케이션 레벨에서 설정 한 android : theme보다 우선합니다. 부모 활동에는 작업 표시 줄이 있지만 자녀 (이 게시물에서 내가 묻는 스 와이프 활동)는 그렇지 않습니다.

+0

테마가 ActionBar를 지원해야합니다. 지원되는 테마없이 액세스하려고하면 NullPointerException이 발생하고 충돌이 발생합니다. –

+0

나는 이미 그렇게 생각한다. 부모 활동 (스 와이프 활동을 시작하는 활동)에는 예를 들어 작업 표시 줄이있다. 게시물을 편집하고 더 많은 정보를 추가합니다. – Adrian

+0

테마는 활동 단위로 진행할 수 있지만이 특정 활동에 테마가 있다고 확신하는 경우 추가 정보를 살펴볼 수 있습니다. –

답변

2

문제는 스 와이프보기와 관련이 거의 없으므로 매우 간단합니다. FragmentActivity에 ActionBar 만 있으면됩니다.

The documentation makes a special note about using the ActionBar in a FragmentActivity :

참고 : 작업 표시 줄을 포함하는 활동을 구현하려면, 당신은 대신 그래서 당신은 사용할 수 있습니다,이 하나의 하위 클래스 인 ActionBarActivity 클래스를 사용해야합니다 API 레벨 7 이상의 조각화 API.

활동에 ActionBarActivity를 확장하고 getSupportActionBar()을 사용하여 ActionBar에 대한 참조를 얻으십시오.

관련 문제