2015-11-15 61 views
0

我已經寫了一個帶有導航抽屜和3個固定選項卡的簡單代碼,但我面對這兩個錯誤 1>即使導航抽屜打開並且當我點擊它時...隱藏在導航抽屜後面的相同位置上的選項卡也會被點擊。 2>當我點擊導航抽屜上的項目時...動作得到執行,但片段並未被viewpager替換或更準確地隱藏。如何在導航抽屜打開時禁用viewpager

這裏是代碼.. MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener { 

private static String TAG = MainActivity.class.getSimpleName(); 

private Toolbar mToolbar; 
private TabLayout tabLayout; 
private ViewPager viewPager; 
private FragmentDrawer drawerFragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mToolbar = (Toolbar) findViewById(R.id.toolbar); 

    setSupportActionBar(mToolbar); 
    //getSupportActionBar().setDisplayShowHomeEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    drawerFragment = (FragmentDrawer) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
    drawerFragment.setDrawerListener(this); 

    // display the first navigation drawer view on app launch 
    displayView(0); 

    viewPager = (ViewPager) findViewById(R.id.viewpager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
} 

private void setupViewPager(ViewPager viewPager){ 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new TabOne(), getString(R.string.tabOne)); 
    adapter.addFragment(new TabTwo(), getString(R.string.tabTwo)); 
    adapter.addFragment(new TabThree(), getString(R.string.tabThree)); 
    viewPager.setAdapter(adapter); 
} 


class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public 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 AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    if(id == R.id.action_search){ 
     Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onDrawerItemSelected(View view, int position) { 
    displayView(position); 
} 

private void displayView(int position) { 
    Fragment fragment = null; 
    String title = getString(R.string.app_name); 
    switch (position) { 
     case 0: 
      fragment = new HomeFragment(); 
      title = getString(R.string.title_home); 
      break; 
     case 1: 
      fragment = new FriendsFragment(); 
      title = getString(R.string.title_friends); 
      break; 
     case 2: 
      fragment = new MessagesFragment(); 
      title = getString(R.string.title_messages); 
      break; 
     default: 
      break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container_body, fragment); 
     fragmentTransaction.commit(); 

     // set the toolbar title 
     getSupportActionBar().setTitle(title); 
    } 
} 
} 

ActivityMain.xml

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 


      <android.support.design.widget.AppBarLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
       <include 
        android:id="@+id/toolbar" 
        layout="@layout/toolbar" /> 
       <android.support.design.widget.TabLayout 
        android:id="@+id/tabs" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:tabMode="fixed" 
        app:tabGravity="fill"/> 

      </android.support.design.widget.AppBarLayout> 

     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/container_body" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 


    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 
<fragment 
    android:id="@+id/fragment_navigation_drawer" 
    android:name="com.coastallabs.chat.devchat.activity.FragmentDrawer" 
    android:layout_width="@dimen/nav_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:layout="@layout/fragment_navigation_drawer" 
    tools:layout="@layout/fragment_navigation_drawer" /> 

+0

正如我之前提到的,您的Fragment的容器FrameLayout具有零高度,因爲ViewPager佔用了所有的垂直空間。在ViewPager上設置'layout_height =「0dp」'和'layout_weight =「1」'。此外,你現在有六個不同的片段類。那是你要做的嗎? –

+0

好的,如果我使用layout_height =「0dp」和layout_weight =「1」,它將在viewpager和framelayout之間平均分割屏幕。但我想要這樣一種方式,當我點擊導航抽屜項目時,只用framelayout打開一個新的活動,並且沒有viewpager(當導航項目被選中時,我不想要這些標籤和viewpager)。 –

+0

對。我只是讓你這樣做,所以你至少可以看到你的FragmentTransactions是否工作。現在,你真的想打開一個新的活動嗎?如果你這樣做,那麼做FragmentTransactions或者擁有home,messages和friends碎片都沒有意義。 –

回答

0

在你fragment_navigation_drawer.xml

集rootview clickable =真。

+0

好吧...你知道第二個問題的解決方案。 –

+0

什麼是抽屜的佈局? –

+0

我在相對佈局中使用recyclerView。 –