gpt4 book ai didi

android - 如何从 View 寻呼机中的第一个 fragment 选项卡调用第二个 fragment 选项卡

转载 作者:行者123 更新时间:2023-11-30 02:29:50 26 4
gpt4 key购买 nike

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 :)

最佳答案

使用 viewPager.setCurrentItem(position, true); 在选项卡之间切换。

然后,使用 this获取当前 fragment 。

然后调用您创建的该 fragment 的公共(public)方法来执行任何您想要的操作。

关于android - 如何从 View 寻呼机中的第一个 fragment 选项卡调用第二个 fragment 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com