如何从视图寻呼机中的第一个片段选项卡调用第二个片段选项卡

时间:2014-12-13 12:46:45

标签: android android-fragments android-viewpager

This is MyTabActivity.java  class

public class MyTabActivity extends FragmentActivity {
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

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

        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();

        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
                Toast.makeText(getBaseContext(), "Tab ReSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                Tab.setCurrentItem(tab.getPosition());
                Toast.makeText(getBaseContext(), "Tab Selected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
                Toast.makeText(getBaseContext(), "Tab UnSelected: " + tab.getPosition(), Toast.LENGTH_SHORT).show();
            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(tabListener));
}
}


This is TabPagerAdapter.java

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            return new Tab1Activity();
        case 1:
            return new Tab2Activity();

        }
        return null;
    }
    @Override
    public int getCount() {
        //No of Tabs
        return 2; 
    }
}


public class Tab1Activity extends Fragment {

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
    /// code to display LIstView
       .
       .
     //here i m trying to invoke other tab on click of list item...this where i stuck
listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adptr, View view, int pos,
                    long id) {
                /// this code here is not working for me
                                Tab2Activity myDetailFragment = new Tab2Activity ();
                                Bundle bundle = new Bundle();
                                bundle.putString("KEY_DETAIL", "Hi");
                                myDetailFragment.setArguments(bundle);
                                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                                fragmentTransaction.attach(myDetailFragment);
                                fragmentTransaction.addToBackStack(null);
                                fragmentTransaction.commit();


            }});
        return monthlyView;
    }
}

This is Tab2Activity.java

public class Tab2Activity extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View dailyView = inflater.inflate(R.layout.activity_mian, container, false);
//      displaying list item based on the input from tab1
        return dailyView;
    }
}



This is activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <ListView 
        android:id="@+id/list_View1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ListView>


</RelativeLayout>

This is pager.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view starts -->

    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

现在我的问题是,我在tab1中使用listview显示项目列表 我想在用户点击tab1的列表项时调用tab2 并通过从tab1向tab2发送一些数据在tab2上显示一些数据。

I an mew to android, please guide me with proper solutions. Thanks :)

1 个答案:

答案 0 :(得分:1)

使用viewPager.setCurrentItem(position, true);在标签之间切换。

然后,使用this获取当前片段。

然后,您调用您创建的片段的公共方法,以执行您想要的任何操作。