2015-12-27 33 views
1

我只是試圖使用TabLayout,我有一些問題要了解它是如何工作的。碎片對我來說也是新的。 所以這就是要點,我的第一個活動,當用戶啓動應用程序是一個教程。我爲教程的每個部分使用一個片段。更重要的是,我知道只有視圖尋呼機對用戶來說足夠了,我只想用標籤佈局來試用它。 因此,在第一個片段中,我有一個TextView和一個Button。而當用戶點擊該按鈕時,我想切換到第二個選項卡的教程等TabLayout動態使用片段

的第二部分,現在第一個片段:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ext="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/tv_tuto_first_fragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<Button 
    android:layout_width="130dp" 
    android:layout_height="53dp" 
    android:id="@+id/valider_tuto_first_fragment"/> 

</RelativeLayout> 

Java文件:

public class TutoFirstFragment extends Fragment { 

    public TutoFirstFragment() 
    { 

    } 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.tuto_first_fragment, container, false); 
    } 
} 

的的onCreate()名爲Tutoriel活動方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    replaceContentLayout(R.layout.tutorial_activity, super.CONTENT_LAYOUT_ID); 

    toolbar = (Toolbar) findViewById(R.id.tutorial_toolbar); 
    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    tabLayout = (TabLayout) findViewById(R.id.tuto_tab_layout); 
    tabHost = (TabHost) findViewById(R.id.tuto_first_fragment_tabhost); 
    viewPager = (ViewPager) findViewById(R.id.tuto_view_pager); 

    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView.setNavigationItemSelectedListener(this); 

    setupViewPager(viewPager); 
    tabLayout.setupWithViewPager(viewPager); 
    tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(context, R.color.white)); 

    toSecondFragment = (Button) findViewById(R.id.valider_tuto_first_fragment); 
    toSecondFragment.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      tabHost.setCurrentTab(tabLayout.getSelectedTabPosition() + 1); 
     } 
    }); 
} 

要的問題是,我無法在活動中創建onClickListener,但我不明白如何在片段中執行此操作。

謝謝您的幫助,通過分享我自己的知識:)

回答

0

讓您的片段實施OnClickListener

public class MainFragment extends Fragment implements OnClickListener 

然後實現onClick方法在你的片段我們

@Override // LISTENER 
public void onClick(View v) { do your stuff there } 

將他的聽衆分配給一個對象,請使用以下內容:

yourWhateverObject.setOnClickListener(this); 

現在可以切換你的標籤位置在這,如果你有機會到尋呼機爲例:

pager.setCurrentItem(position); 
+0

謝謝您的回答一個例子,但我不明白這一點。在第一個片段的'onClickListener'中,我想將當前選項卡設置爲另一個位置。但是我只能在活動中訪問tabLayout,而不是在片段中。 –

+0

然後你必須給TabLayout引用你的片段。你可以考慮在你的活動中使用一個實例,你的活動作爲一個單例可以在你的片段中訪問。 – Virthuss