我有一个带标签栏的MainActivity,当我点击图标时,viewPager中会显示相应的片段。 我在它如何工作方面遇到了麻烦,因为我无法找到代码中的哪个位置从片段切换到另一个片段。
package soccerinformations;
import allLibrariesIneed;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.soccerball,
R.drawable.zoom,
R.drawable.oldpaper,
R.drawable.moneyhand
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new MatchFragment(), "SOCCERBALL");
adapter.addFrag(new StatisticheFragment(), "ZOOM");
adapter.addFrag(new SchedinaFragment(), "OLDPAPER");
adapter.addFrag(new ContoFragment(), "MONEYHAND");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
我有一个片段Match_Fragment,这是代码:
public class MatchFragment extends Fragment{
private View rootView;
private String nazione, categoria, homeTeam, guestTeam;
private String[] arrayNazione, arrayCategoria, arrayTeam;
private Spinner spinnerNazione, spinnerCategoria, spinnerHomeTeam, spinnerGuestTeam;
private Button buttonShow;
public MatchFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_match, container, false);
buttonShow = (Button) rootView.findViewById(R.id.button);
buttonShow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//HERE I WANT REPLACE THIS FRAGMENT WITH A NEW BLANK FRAGMENT
new BlankFragment();
}
});
return rootView;
}
}
当我点击按钮时,我想要一个新的片段而不是当前的片段。怎么可能?