gpt4 book ai didi

java - 从第二个 Activity 启动第一个 Activity 的第二个选项卡 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:04 25 4
gpt4 key购买 nike

我是 Android 新手,我被困在我正在开发的应用程序的特定部分。问题是 - 当我在 SecondActivity 中的 createEvent 按钮上执行 onClickListener 事件时,我想导航到 HomePageActivity 的第二个选项卡。我也尝试使用这里和其他网站上的各种线程的解决方案,但我仍然无法运行我的代码。

我还在 HomePageActivity 上有 RecyclerView,它将根据点击事件进行填充。

这是两个 Activity 的代码 fragment -

HomePageActivity.java - 选项卡式 Activity

public class HomePageActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);

fragmentInfo = getIntent().getExtras();
if (fragmentInfo != null)
{
tabNumber = fragmentInfo.getInt("tabNumber");
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}

public class SectionsPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
{
String[] tabList = {"Communities", "Events", "People"};

public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm)
{
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int position)
{
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount()
{
return tabList.length;
}

@Override
public CharSequence getPageTitle(int position)
{
return tabList[position];
}
}
public static class PlaceholderFragment extends android.support.v4.app.Fragment
{
private static final String ARG_SECTION_NUMBER = "section_number";

public static PlaceholderFragment newInstance(int sectionNumber)
{
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

public PlaceholderFragment()
{
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
int viewNumber = getArguments().getInt(ARG_SECTION_NUMBER);
final String[] eventsListArray = {"Event1", "Event2"};

if (viewNumber == 1)
{
View rootView = inflater.inflate(R.layout.fragment_communities, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.communitieslabel);
textView.setText("communities");
return rootView;
}
else if (viewNumber == 2)
{
View rootView = inflater.inflate(R.layout.fragment_events, container, false);
eventsList = (RecyclerView) rootView.findViewById(R.id.recyclerViewEvents);
fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(getActivity().getBaseContext(), CreateEvents.class);
startActivity(intent);
}
});

eventsList.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
eventsList.setLayoutManager(mLayoutManager);
eventsListAdapter = new RecyclerEventsAdapter(getActivity(), eventListInfo);
eventsList.setAdapter(eventsListAdapter);
return rootView;
}
else
{
View rootView = inflater.inflate(R.layout.fragment_peopleprofile, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.peoplelabel);
textView.setText("people");
return rootView;
}
}
}
}

以及SecondActivity中的代码 -

buttonCreateEvent.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String[] eventInfo = new String[0];
getEventName = editTextEventName.getText().toString();
getEventDetail = editTextEventDetails.getText().toString();
getEventLocation = editTextEventLocation.getText().toString();
while (!getEventName.equals("") && !getEventDetail.equals("") && !getEventLocation.equals("") && !getEventDate.equals(""))
{
eventInfo = new String[]{getEventName, getEventDetail, getEventLocation, getEventDate};
}
Intent intent = new Intent(CreateEvents.this, HomePageActivity.class);
intent.putExtra("tabNumber", 2);
intent.putExtra("openEventFragment", eventInfo);
startActivity(intent);
}
});

感谢任何帮助。预先感谢您。

最佳答案

您可以调用 getActivity().getIntent().getExtras() 来获取您在其他 Activity 中设置的附加内容的 Bundle

关于java - 从第二个 Activity 启动第一个 Activity 的第二个选项卡 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701042/

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