2016-04-28 72 views
0

我有一個名爲DealMailListig的片段,並有主要活動。我試圖在我的片段中添加導航視圖,而不是在主要活動中用抽屜切換圖標。但是不要成功.pls指導我。如何在Android中的片段添加導航視圖?

任何幫助將不勝感激。

這裏是我的交易MainListing的代碼 : -

public class CDealMainListing extends Fragment { 
// --Commented out by Inspection (11-04-2016 10:45):public LinearLayout m_MainLayout; 
private ViewPager m_ViewPager;// declare view pager variable 
private View m_Main;// declare View Main variable 

@SuppressWarnings("ConstantConditions") 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    m_Main = inflater.inflate(R.layout.deals_main_screen, container, false); 
    //noinspection ConstantConditions 
    ((AppCompatActivity) getActivity()).getSupportActionBar().show(); // show tool bar 
    init();// initlize controls 
    return m_Main; 
} 

private void init() { 
    TabLayout m_TabLayout = (TabLayout) m_Main.findViewById(R.id.tab_layout);// finding Id of tablayout 
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Deals"));// add deal listin tab 
    m_TabLayout.addTab(m_TabLayout.newTab().setText("Stories"));// add stories tab 
    m_TabLayout.setTabGravity(TabLayout.GRAVITY_FILL);// setting Gravity of Tab 

    m_ViewPager = (ViewPager) m_Main.findViewById(R.id.pager);//finding Id of ViewPager 
    CDealMainListingPager m_oDealMainScreenPager = new CDealMainListingPager 
      (getActivity().getSupportFragmentManager(), m_TabLayout.getTabCount()); 
    m_ViewPager.setAdapter(m_oDealMainScreenPager);// adiing adapter to ViewPager 
    m_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(m_TabLayout));// performing action of page changing 
    m_TabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      m_ViewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

} 

}

回答

0

我認爲你必須創建活動,並在這個活動中,你將不得不創建navidation視圖和標籤佈局。然後,你需要一個適配器(ViewPagerAdapter)和呼叫片段經理是這樣的:

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 

,那麼你就必須片段添加到您的適配器是這樣的:

adapter.addFragment(new WhateverFragment(), 'title'); 
viewPager.setAdapter(adapter); 

(視圖尋呼機之前聲明)

那麼你就必須建立tabLayout與viewpager:

tabLayout.setupWithViewPager(viewPager); 

你可以嘗試這個,並隨時要求更多的細節:)

+0

我試過這個,但我不想在活動中作出我想在片段 –

+0

我不知道這是可能的,除非你有一個活動它擴展了FragmentActivity –

相關問題