我正在尝试使用TabLayout和ViewPager替换选项卡中显示的片段,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.netstreamtech.phonebuylogin.LoginRegisterActivity"
android:paddingLeft="32.5dp"
android:paddingTop="27dp"
android:paddingRight="30dp">
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/signInReOtpHeader"
android:layout_width="268dp"
android:layout_height="122dp"
android:includeFontPadding="true"
android:maxLines="2"
android:text="Welcome \nback."
android:layout_gravity="center_vertical"
android:textColor="#1aa4b8"
android:textSize="35sp" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="47dp"
android:minHeight="?attr/actionBarSize"
android:elevation="6dp" />
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
来自我的片段:
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof RegisterMainToActivity) {
listener = (RegisterMainToActivity) context;
}
}
然后在同一片段中:
listener.passData(userRegistration);
界面:
public interface RegisterMainToActivity {
void passData(UserRegistration data);
}
在我的活动(LoginRegisterActivity)中:
public class LoginRegisterActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, RegisterMainToActivity {
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
private LoginPager adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_register);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
createViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
createTabIcons();
}
private void createTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.login_cutom_tab_view, null);
tabOne.setText("SIGN IN");
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.login_cutom_tab_view, null);
tabTwo.setText("REGISTER");
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void createViewPager(ViewPager viewPager) {
adapter = new LoginPager(getSupportFragmentManager());
adapter.addFrag(new Login(), "SIGN IN");
adapter.addFrag(new RegisterMain(), "REGISTER");
viewPager.setAdapter(adapter);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//tabLayout.setBackground(getResources().getDrawable(R.drawable.login_tab_selected));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//tabLayout.setBackground(getResources().getDrawable(R.drawable.login_tab_unselected));
}
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public void passData(UserRegistration data) {
RegisterOtp registerOtpFrag = new RegisterOtp();
adapter.replaceFragment(registerOtpFrag, 1, "REGISTER");
//adapter = new LoginPager(getSupportFragmentManager());
//adapter.addFrag(new Login(), "SIGN IN");
//adapter.addFrag(new RegisterOtp(), "REGISTER");
//adapter.clearFragments();
//adapter.addFrag(new Login(), "SIGN IN");
//adapter.addFrag(new RegisterOtp(), "REGISTER");
//viewPager.setAdapter(adapter);
createTabIcons();
}
// public void attemptRegister(View v){
// TextView registerName = (TextView) v.getContext()findViewById(R.id.register_name);
// Toast.makeText(
// getApplicationContext(),
// registerName.getText(),
// Toast.LENGTH_SHORT
// ).show();
// }
}
在函数passData
中,我正在尝试替换片段。我想把它添加到后台。
在我的适配器类中:
public class LoginPager extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public LoginPager(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);
}
public void replaceFragment(Fragment fragment, int index, String title) {
mFragmentList.remove(index);
mFragmentList.add(index, fragment);
mFragmentTitleList.add(title);
notifyDataSetChanged();
}
public void clearFragments()
{
mFragmentList.clear();
notifyDataSetChanged();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getItemPosition(Object object) {
return this.POSITION_NONE;
}
}
我尝试了很多变体,包括尝试清除适配器并重新创建片段。无论我做什么,我都无法替换所需的片段(RegisterMain with RegisterOtp)。理想情况下,我应该能够取代它......请帮助!!!
答案 0 :(得分:0)
FragmentStatePagerAdapter适合我。
public class LoginPager extends FragmentStatePagerAdapter {...}
FragmentPagerAdapter.notifyDataSetChanged()没有清除那些已经提交的片段。
FragmentPagerAdapter最适合在少数人使用时使用 通常会有更多静态片段被分页。