11

Tôi đang sử dụng phương pháp hoạt động đơn lẻ và nhiều đoạn trong ứng dụngCách xử lý ngăn kéo bật tắt và thanh công cụ lên khi có thanh công cụ cho mỗi đoạn

của tôi thanh công cụ cho từng đoạn.

Làm thế nào để thực hiện thanh công cụ riêng biệt cho mỗi mảnh cũng bố trí ngăn kéo là trong hoạt động của tôi Home page category page

+0

Bạn đã tìm thấy giải pháp nào chưa? –

+0

không tìm thấy bất kỳ giải pháp – alphanso

Trả lời

0

Tôi không chắc chắn nếu tôi hiểu mô tả của bạn trong những ứng dụng của bạn một cách chính xác, nhưng gần đây tôi đã làm những gì tôi nghĩ rằng bạn đã mô tả. Bố cục hoạt động của tôi là một DrawerLayout với bố cục CoordinatorLayout/AppBar được bao gồm với một thanh công cụ duy nhất và FrameLayout bên dưới. Menu.xml chứa tất cả các mục tôi cần trong thanh công cụ của tôi cho tất cả các mảnh. Các mục được nhấp trong menu điều hướng sẽ hoán đổi các đoạn trong FrameLayout. My onNavigationItemSelected() kêu gọi các phương pháp này để trao đổi trên các mảnh vỡ và xử lý các backstack:

public void switchView(int id, int optArg) { 
    if (currentView != id) { 
    currentView = id; //currentView keeps track of which fragment is loaded 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    //Fragment contentFragment is the current fragment in the FrameLayout 
    switch (id) { 
     case 0: //menu item 1 
     contentFragment = new Nav_Item1_Fragment(); 
     transaction.replace(R.id.fragment_container, contentFragment, ""); 
     break; 
     case 1: //menu item 2 
     contentFragment = new Nav_Item2_Fragment(); 
     transaction.replace(R.id.fragment_container, contentFragment, ""); 
     break; 
     case 2: //menu item 3 
     contentFragment = new Nav_Item3_Fragment(); 
     transaction.replace(R.id.fragment_container, contentFragment, ""); 
     break; 
    } 
    // Replace whatever is in the fragment_container view with this fragment, 
    // and add the transaction to the back stack 
    // transaction.replace(R.id.fragment_container, contentFragment); 
    transaction.addToBackStack(null); 
    // Commit the transaction 
    transaction.commit(); 
    } 
} 

và trong onPrepareOptionsMenu() Tôi setVisible mỗi đoạn() giấu/hiện các mục trình đơn trong thanh công cụ liên quan đến đoạn đó. Mỗi mục có một phương thức trong hoạt động được trỏ đến bởi thuộc tính onClick của mục trình đơn mà biết phân đoạn đó là gì và các khung nhìn nào được truyền cho nó.

Các ngăn kéo đã được thiết lập trong onCreate() của hoạt động này với một ActionBarDrawerToggle như vậy:

drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.addDrawerListener(toggle); 
toggle.syncState(); 

hoạt động xml:

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    ...> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     .../> 

</android.support.v4.widget.DrawerLayout> 

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    ...> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ...> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:gravity="top" 
      .../> 

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

    <FrameLayout 
     android:id="@+id/fragment_container" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
    </FrameLayout> 

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

Vì vậy, ... Một menu Nav, một App/Tool Bar, nhiều đoạn

+0

không có điều này không phải là tôi muốn tôi muốn thanh công cụ cho từng đoạn và muốn xử lý chuyển đổi ngăn kéo cùng một lúc – alphanso

0

Tại sao bạn đặc biệt muốn một thanh công cụ riêng biệt cho từng đoạn? Bạn có thể dễ dàng thay đổi giao diện thanh công cụ cho từng đoạn.

Trong chức năng của bạn để chuyển đổi các mảnh -

public void selectDrawerItem(MenuItem item) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction; 
     switch (item.getItemId()) { 

        case R.id.nav_home : 
         getSupportActionBar().setCustomView(R.layout.home_nav_bar); 
         fragmentClass = HomeFragment.class; 
         break; 
        case R.id.nav_settings : 
         getSupportActionBar().setCustomView(R.layout.settings_nav_bar); 
         fragmentClass = SettingsFragment.class; 
         break; 

     } 
     fragment = (Fragment) fragmentClass.newInstance(); 
     fragmentManager.popBackStack(null, 
     FragmentManager.POP_BACK_STACK_INCLUSIVE); 

     fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.fragment_container, fragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 
} 

Vì vậy, bạn có thể dễ dàng sử dụng thanh công cụ tương tự và tùy chỉnh nó theo yêu cầu của bạn cho mỗi đoạn.

+0

Tôi muốn sử dụng thanh công cụ riêng biệt cho từng đoạn, vui lòng đọc lại câu hỏi – alphanso

+0

Bạn có thể giải thích tại sao bạn muốn sử dụng một thanh công cụ riêng biệt? Sử dụng chế độ xem tùy chỉnh giống như sử dụng thanh công cụ riêng biệt. Nếu bạn vẫn kiên quyết trên thanh công cụ riêng biệt, tùy chọn duy nhất tôi có thể nghĩ đến là tạo nhiều phiên bản ActionBarDrawerToggle với cá thể thanh công cụ bạn muốn - 'ActionBarDrawerToggle toggle1 = new ActionBarDrawerToggle ( this, drawerLayout, toolbar1, R.string.navigation_open , R.string.navigation_close); 'Và sau đó trước khi tải đoạn bạn muốn,' drawerLayout.addDrawerListener (toggle1); 'Điều này không được kiểm tra. Vì vậy, hãy cho tôi biết nếu nó hoạt động. – user3911483

4

Tôi gặp vấn đề tương tự, tôi sẽ thêm chế độ xem thanh công cụ tùy chỉnh cho từng đoạn.

phương pháp Utility của tôi là:

public static View addRemoveViewFromToolbar(FragmentActivity fragmentActivity, int resourceId) { 
    Toolbar toolbar = removeViewFromToolbar(fragmentActivity); 
    if (resourceId == 0) { 
     return null; 
    } else { 
     View view = LayoutInflater.from(fragmentActivity).inflate(resourceId, toolbar, false); 
     toolbar.addView(view); 
     return view; 
    } 
} 


public static Toolbar removeViewFromToolbar(FragmentActivity fragmentActivity) { 
    Toolbar toolbar = (Toolbar) fragmentActivity.findViewById(R.id.toolbar); 
    if (toolbar.getChildCount() > 1) { 
     for (int i = 1; i <= toolbar.getChildCount(); i++) { 
      toolbar.removeViewAt(1); 
     } 
    } 
    return toolbar; 
} 

Trong mỗi đoạn tôi

//Create your custom view based on requirement 
    View view = Utility.addRemoveViewFromToolbar(getActivity(), R.layout.toolbar_search_view); 
     if (view != null) { 
      edtCategory1 = (EditText) view.findViewById(R.id.edtCategory1); 
      edtCategory1.setOnClickListener(this); 
     } 

Hope lời giải thích này giúp bạn :)

0

Nó có thể được thực hiện một cách khá đơn giản.

  1. Trước tiên hãy tạo hoạt động với drawerlayout.

  2. Second tạo một container viewpager bên trong hoạt động để giữ các fragments

  3. thứ ba thực hiện một listener trên viewpager bạn sẽ thiết lập các liên quan toolbar dựa trên fragment trưng bày.

Hãy để tôi minh họa bởi XMLs có liên quan và mã

Đầu tiên Drawerlayout XML cho các hoạt động chính

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:openDrawer="start"> 
<include 
    layout="@layout/app_bar_landing_page" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_landing_page" 
    app:menu="@menu/activity_landing_page_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

Xin lưu ý cách bố trí thùng chứa app_bar_landing_page. Bây giờ, XML cho số này

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.veganbuddy.veganbuddy.ui.LandingPage"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:elevation="4dp" 
      app:logo="@drawable/vegan_buddy_menu_icon" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

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

Lưu ý rằng viewpager sẽ đóng vai trò như thùng chứa cho các mảnh. Bây giờ là OnPageChangeListener trên chế độ xem.

mViewPager = (ViewPager) findViewById(R.id.container_for_fragments); 
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      switch (position+1){ 
       case 0:setSupportActionBar(aToolbar); 
        break; 
       case 1:setSupportActionBar(bToolbar); 
        break; 
       case 2:setSupportActionBar(cToolbar);; 
        break; 
       case 3:setSupportActionBar(dToolbar); 
        break; 
       default: 
        break; 
      } 
     } 

Hãy cho tôi biết nếu có làm rõ thêm là cần thiết

0

Phương pháp đơn giản nhất tôi xin đề nghị là sử dụng callbacks để hoạt động. Bất cứ khi nào mỗi đoạn tải trong hoạt động, sắp xếp một cuộc gọi lại đến hoạt động và tải thanh công cụ thích hợp trong hoạt động.

Có xml thanh công cụ riêng trong thư mục bố cục của bạn. Sử dụng thẻ bao gồm để đặt các thanh công cụ trong hoạt động của bạn. Chỉ hiển thị 1 thanh công cụ tại một thời điểm. Khi các callbacks fragment đến với hoạt động của bạn, hãy thực hiện những điều cần thiết.

Các vấn đề liên quan