>滑动到下一个片段时,我在“ HomeActivity”上有4个片段,应该显示新片段。
请检查以下代码段以供参考
ViewPageAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter{
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 addFragment( Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}}
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("PERSONAL INFORMATION");
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//1 PERSONAL INFORMATION
//2 PROFESSIONAL INFORMATION
//3 FINANCIAL INFORMATION
//4 DOCUMENT DETAILS
if (tab.getPosition() == 0) {
getSupportActionBar().setTitle("PERSONAL INFORMATION");
viewPager.setCurrentItem(0);
} else if (tab.getPosition() == 1) {
getSupportActionBar().setTitle("PROFESSIONAL INFORMATION");
viewPager.setCurrentItem(1);
} else if (tab.getPosition() == 2) {
getSupportActionBar().setTitle("FINANCIAL INFORMATION");
viewPager.setCurrentItem(2);
} else if (tab.getPosition() == 3) {
getSupportActionBar().setTitle("DOCUMENT DETAILS");
viewPager.setCurrentItem(3);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(AssignedCustomerFragment.getInstance(), getResources().getString(R.string.assigned_tab));
adapter.addFragment(CompletedCustomerFragment.getInstance(), getResources().getString(R.string.completed_tab));
adapter.addFragment(FinancialDetailsFragment.getInstance(), getResources().getString(R.string.finance_info_tab));
adapter.addFragment(CustomerDocumentFragment.getInstance(), getResources().getString(R.string.document_info_tab));
viewPager.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return (true);
}
return (super.onOptionsItemSelected(item));
}
@Override
public void onBackPressed() {
super.onBackPressed();
}}
Fragment2代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View view = inflater.inflate(R.layout.customer_professional_information, container, false);
final HorizontalScrollView horscroll = (HorizontalScrollView) view.findViewById(R.id.horscroll);
final HorizontalScrollView horscroll1 = (HorizontalScrollView) view.findViewById(R.id.horscroll1);
final HorizontalScrollView horscroll2 = (HorizontalScrollView) view.findViewById(R.id.horscroll2);
final RadioGroup salarieddom = (RadioGroup) view.findViewById(R.id.rdgsalarieddomain);
final RadioGroup nonsalarieddom = (RadioGroup) view.findViewById(R.id.rdgnonsalaried);
final RadioGroup paymentmode = (RadioGroup) view.findViewById(R.id.rdgpaymentmode);
final TextView txtpaymentmode = (TextView) view.findViewById(R.id.txtpaymentmode);
edtcompanyname = (EditText) view.findViewById(R.id.companyname);
edtofficialid = (EditText) view.findViewById(R.id.txtofficialmailid);
rdgprofession = (RadioGroup) view.findViewById(R.id.rdgprof);
rdgsalariedoption = (RadioGroup) view.findViewById(R.id.rdgsalarieddomain);
rdgsalarydrawnthrough = (RadioGroup) view.findViewById(R.id.rdgpaymentmode);
rdgnonsalaried = (RadioGroup) view.findViewById(R.id.rdgnonsalaried);
rdgprofession.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rdbsalaried:
Toast.makeText(getActivity(), "Salaried", Toast.LENGTH_SHORT).show();
salariedornot = "Salaried";
break;
case R.id.rdbnonsalaried:
Toast.makeText(getActivity(), "Non Salaried", Toast.LENGTH_SHORT).show();
salariedornot = "Non Salaried";
break;
}
}
});
rdgsalariedoption.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rdbprivate:
salarysector = "Private";
break;
case R.id.rdbpublic:
salarysector = "Public";
break;
case R.id.rdbgovernment:
salarysector = "Government";
break;
}
}
});
rdgsalarydrawnthrough.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rdbcash:
salarypaymentmode = "Cash";
break;
case R.id.rdbcheque:
salarypaymentmode = "Cheque";
break;
case R.id.rdbbanktranfer:
salarypaymentmode = "Direct Bank Transfer";
break;
}
}
});
rdgnonsalaried.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rdbselfemployed:
salarysector = "Self employed";
break;
case R.id.rdbbusiness:
salarysector = "Business";
break;
case R.id.rdbretired:
salarysector = "Retired";
break;
case R.id.rdbstudent:
salarysector = "Student";
break;
}
}
});
RadioGroup rdgGender = (RadioGroup) view.findViewById(R.id.rdgprof);
rdgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rdbsalaried:
salarieddom.setVisibility(View.VISIBLE);
horscroll1.setVisibility(View.VISIBLE);
horscroll.setVisibility(View.INVISIBLE);
nonsalarieddom.setVisibility(View.INVISIBLE);
horscroll2.setVisibility(View.VISIBLE);
paymentmode.setVisibility(View.VISIBLE);
txtpaymentmode.setVisibility(View.VISIBLE);
break;
case R.id.rdbnonsalaried:
nonsalarieddom.setVisibility(View.VISIBLE);
horscroll.setVisibility(View.VISIBLE);
horscroll1.setVisibility(View.INVISIBLE);
salarieddom.setVisibility(View.INVISIBLE);
horscroll2.setVisibility(View.INVISIBLE);
paymentmode.setVisibility(View.INVISIBLE);
txtpaymentmode.setVisibility(View.INVISIBLE);
break;
}
}
});
return view;
}
fragment3代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.customer_financial_details_fragment, container, false);
final SeekBar seekbarloanamt = (SeekBar) view.findViewById(R.id.sbloanamt);
final EditText edtseekbarval = (EditText) view.findViewById(R.id.txtloanamt);
edtseekbarval.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() >0){
int i = Integer.parseInt(s.toString());
if(i < 5000000) {
edtseekbarval.setSelection(edtseekbarval.getText().length());
seekbarloanamt.setProgress(i);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
seekbarloanamt.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
edtseekbarval.setText(String.valueOf(progress));
}
});
return view;
}