2015-12-27 84 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