我正在开发一个应用程序,我必须在其中实现一个允许我使用Button更改Fragment而不是在屏幕上抓取网站的功能。
Tutorial.java
public class Tutorial extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
PlaceholderFragment.java
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {}
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;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tutorial, container, false);
Button next = (Button) rootView.findViewById(R.id.next);
Button previous = (Button) rootView.findViewById(R.id.previous);
RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.relative_tutorial);
switch (getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:
rl.setBackgroundResource(R.drawable.one);
previous.setText("ESCI");
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getActivity().finish();
}
});
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
break;
case 2:
rl.setBackgroundResource(R.drawable.two);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
break;
case 3:
rl.setBackgroundResource(R.drawable.three);
next.setText("HOME");
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getActivity().finish();
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
}
});
break;
}
return rootView;
}
}
SectionsPagerAdapter.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
如何通过点击我的按钮切换“页面”,而不是在屏幕上抓取网站?
答案 0 :(得分:0)
这是显示另一个片段的方式。所以,只需将此代码放在按钮onclick方法中。
示例:强>
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragment_1(), "frag1");
fragmentTransaction.commit();