0

我正在開發一個應用程序,它有一個活動(MainActivity.java),我在其上集成了導航抽屜,當我點擊導航抽屜項目時,抽屜式導航欄項目負荷在<framelayout />,在那之後我在底部的同一頁(MainActivity.java)使用ViewPagerStrip上集成的標籤,現在的問題是:將導航抽屜項目和ViewPagerStrip項目的內容加載到同一容器 - Android

當我點擊抽屜式導航項,標籤項的內容Navigation Drawer項目和ViewPagerStrip項目未被加載到同一個容器上,我如何在同一個容器(Framelayout或ViewPager)上加載這兩個項目(選項卡項目和抽屜項目)的內容。

我有以下的MainActivity.java

代碼
public class MainActivity extends FragmentActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Set activity_main.xml 
     setContentView(R.layout.activity_main); 
     //Create tabs at bottom using ViewPagerStrip 
     mCustomPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), this); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mCustomPagerAdapter); 

     //Create NavigationDrawer . 
     --------------------------------------- 
     --------------------------------------- 
     --------------------------------------- 
     } 

//Click on Drawer menus 
private class SlideMenuClickListener implements ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      // display view for selected nav drawer item 
      displayDrawerItem(position); 
     } 
    } 

private void displayDrawerItem(int position) { 
     // update the main content by replacing fragments 
     Fragment fragment = null; 
     switch (position) { 
     case 0: 
      fragment = new HomeFragment(); 
      break; 
     case 1: 
      fragment = new FindPeopleFragment(); 
      break; 

     //Other fragments goes here . 

     default: 
      break; 
     } 
     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.frame_container, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      // error in creating fragment 
      Log.e("MainActivity", "Error in creating fragment"); 
     } 
    } 
} 

我在activity_main.xml中下面的代碼

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Framelayout to display Fragments --> 
    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 

    <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="wrap_content" 
     > 

     <android.support.v4.view.PagerTabStrip 
      android:id="@+id/pager_tab_strip" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:background="#33b5e5" 
      android:textColor="#fff" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" /> 

    </android.support.v4.view.ViewPager> 
</FrameLayout> 

    <!-- Listview to display slider menu --> 
    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp"   
     android:listSelector="@drawable/list_selector" 
     android:background="@color/list_background"/> 
</android.support.v4.widget.DrawerLayout> 

我自己的努力

我試圖解決這個問題與不同的-2方法

1。我將<ViewPager >放入<framelayout >之內,並嘗試加載ViewPager上的導航項目和選項卡項目的點擊事件。 2。我刪除了<framelayout >,並嘗試在ViewPager上加載導航和選項卡項目的單擊事件。 3。我遵循許多Stackoverflow鏈接,關於ViewPagerStrip和DrawerLayout的教程,試圖搜索如何在同一個容器上加載抽屜項目和選項卡項目的點擊事件。

編輯

public class CustomPagerAdapter extends FragmentPagerAdapter { 
Context mContext; 
private String[] tabs = {"Trails", "Breweries", "Near By", "Events"}; 
public CustomPagerAdapter(FragmentManager fragmentManager, Context context) { 
    super(fragmentManager); 
    mContext = context; 
} 

@Override 
public android.support.v4.app.Fragment getItem(int position) { 

    // Create fragment object 
    android.support.v4.app.Fragment fragment = new DemoFragment(); 

    // Attach some data to the fragment 
    // that we'll use to populate our fragment layouts 
    Bundle args = new Bundle(); 
    args.putString("page_name", tabs[position]); 

    // Set the arguments on the fragment 
    // that will be fetched in the 
    // [email protected] 
    fragment.setArguments(args); 

    return fragment; 
} 

@Override 
public int getCount() { 
    return tabs.length; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return tabs[position]; 
}} 


public class DemoFragment extends android.support.v4.app.Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout resource that'll be returned 
    View rootView = inflater.inflate(R.layout.fragment_tab, container, false); 

    // Get the arguments that was supplied when 
    // the fragment was instantiated in the 
    // CustomPagerAdapter 
    Bundle args = getArguments(); 
    ((TextView) rootView.findViewById(R.id.textView)).setText(args.getString("page_name")); 

    return rootView; 
}} 
+0

如何在同一個容器中加載兩個(標籤項目和抽屜項目)的內容=>您是否同時使用它們不顯示的內容? – Harry

+0

@Harry只顯示標籤的內容。 –

+0

然後顯示您的CustomPagerAdapter。 – Harry

回答

0

請參見下面的XML佈局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <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="wrap_content"> 

      <android.support.v4.view.PagerTabStrip 
       android:id="@+id/pager_tab_strip" 
       android:layout_width="match_parent" 
       android:layout_height="38dp" 
       android:layout_gravity="bottom" 
       android:background="#33b5e5" 
       android:paddingBottom="5dp" 
       android:paddingTop="5dp" 
       android:textColor="#fff" /> 

     </android.support.v4.view.ViewPager> 


     <FrameLayout 
      android:id="@+id/frame_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="38dp" 
      android:visibility="gone"></FrameLayout> 

    </FrameLayout> 

    <!-- Listview to display slider menu --> 
    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/list_background" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_selector" /> 
</android.support.v4.widget.DrawerLayout> 

在這裏,當你點擊選項卡中設置frame_container知名度,當你點擊導航抽屜項目使frame_container可見性可見

希望這會有所幫助。

+0

我會嘗試使用此代碼。 –

+0

在這裏,我不想滑動標籤,所以我怎樣才能讓他們固定標籤。 –

+0

你不應該問你的問題以外的其他功能,仍然看[添加標籤](http://developer.android.com/training/implementing-navigation/lateral.html#tabs) – Harry