gpt4 book ai didi

java - 根据特定条件替换 ViewPager fragment

转载 作者:搜寻专家 更新时间:2023-11-01 08:37:42 26 4
gpt4 key购买 nike

我创建了一个简单的 ViewPager TabLayout 以在 3 个选项卡中显示 3 个 fragment 。现在,如果用户打开另一个 Activity 并进行一些更改,然后返回到包含 ViewPager 的 Activity ,则应将第 3 个 fragment 替换为新 fragment 。我不希望用户再看到旧的第三个 fragment 。

当用户从另一个 Activity 返回到 ViewPager Activity 时,onResume() 方法被调用,由此我们可以知道是否替换第 3 个 fragment 。

这是 ViewPager 的适配器:

public class TabsPagerAdapterMain extends FragmentPagerAdapter {

int mNumOfTabs;

public TabsPagerAdapterMain(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

switch (position) {
case 0:
YourStoryFragment yourStoryTab = new YourStoryFragment();
return yourStoryTab;
case 1:
BrowseStoriesFragment browseStoriesTab = new BrowseStoriesFragment();
return browseStoriesTab;
case 2:
ParseObject collegeObject;
ParseUser user = ParseUser.getCurrentUser();
collegeObject = user.getParseObject("college");
if (collegeObject == null) {
CollegeSelectFragment collegeSelectFragment = new CollegeSelectFragment();
return collegeSelectFragment;
} else {
YourCollegeFragment yourCollegeTab = new YourCollegeFragment();
return yourCollegeTab;
}

default:
return null;
}
}

@Override
public int getCount() {
return mNumOfTabs;
}
}

包含 ViewPager 的 Activity:

public class XXXXXXMainTabbed extends AppCompatActivity {
ViewPager viewPager;
public Student xxxxxMainStudent;
private boolean brandNewAccount = false;
private CharSequence mTitle;
private long firstTime = 0;

public int yourStoryFragmentMenuId = 0;
Fragment globalFragmentThatIsOpen;
static TextView actionTitle;
Spinner browse_spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxxxxx_main_tabbed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setIcon(R.drawable.actionbar_space_between_icon_and_title);
actionTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
actionTitle.setTypeface(TypeFaceController.titleFace(getApplicationContext()));

browse_spinner = (Spinner) toolbar.findViewById(R.id.browse_spinner);
browse_spinner.setVisibility(View.GONE);


TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(" Story").setIcon(R.drawable.browse_tab));
tabLayout.addTab(tabLayout.newTab().setText(" Browse").setIcon(R.drawable.browse_tab));
tabLayout.addTab(tabLayout.newTab().setText(" College").setIcon(R.drawable.colleges_tab));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
setTitle("Your Story");



FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent inte = new Intent(getApplicationContext(), WriteStory.class);
startActivity(inte);
}
});

viewPager = (ViewPager) findViewById(R.id.pager);
final TabsPagerAdapterMain adapter = new TabsPagerAdapterMain
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setOffscreenPageLimit (4);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());

switch (tab.getPosition()) {
case 0:
setTitle("Your Story");
browse_spinner.setVisibility(View.GONE);
break;
case 1:
setTitle("Browse");
browse_spinner.setVisibility(View.VISIBLE);
break;
case 2:
setTitle("Your College");
browse_spinner.setVisibility(View.GONE);
break;
default:
setTitle("XXXXXX");
browse_spinner.setVisibility(View.GONE);
break;
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

// Grab the bundle information
Intent intentFromPrevious = getIntent();
Bundle extras = intentFromPrevious.getExtras();

saveInPreferenceBoolean("LoggedIn",true);

if (extras != null) {
brandNewAccount = extras.getBoolean("brandNewAccount");

xxxxxxMainStudent = (Student) intentFromPrevious.getParcelableExtra("studentObject");

} else {

}

if (brandNewAccount == true) {
Intent inte = new Intent(getApplicationContext(), WriteStory.class);
brandNewAccount = false;
startActivity(inte);
}

saveInPreference("firstName", "");
saveInPreference("lastName", "");
saveInPreference("countryFrom", "");
saveInPreference("collegeExists", "");
saveInPreference("username", "");
saveInPreference("email", "");
saveInPreference("password", "");
saveInPreference("dateOfBirth", "");
saveInPreference("collegeId", "");
saveInPreference("courseId", "");
saveInPreference("facebookAccount", "");

//updating app version in the user table
updateUserAppVersion();
}

@Override
protected void onResume() {
super.onResume();
Log.e("xxxxxxMainTabbed", "The onResume() event");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

Log.e("postCounter", "" + xxxxxxMainStudent.getPostCount());
if (isItYourStory() == true) {

YourStoryFragment obj = new YourStoryFragment();
if (obj.canEditItem == false) {
obj.menuTitle = "EDIT";
} else {
if (xxxxxxMainStudent.getPostCount() == 0) {
obj.menuTitle = "EDIT";
}
else {
obj.menuTitle = "DONE";
}
}

}


return super.onPrepareOptionsMenu(menu);
}

public Student getStudentDataFromMain() {
return this.xxxxxxMainStudent;
}

public void displayView(int pos) {

}

private void updateUserAppVersion(){
String appVersionName = getAppVersionName();

ParseUser user = ParseUser.getCurrentUser();
user.put("appVersion", appVersionName);

user.saveInBackground(new SaveCallback() {
public void done(com.parse.ParseException e) {
if (e == null) {
// Save was successful!
Log.e("appVersion", "saved successfully");
} else {
// Save failed. Inspect e for details.
Log.e("appVersion", "saved successfully");
}
}
});
}

public String getAppVersionName() {
PackageInfo pInfo = null;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String version = pInfo.versionName;

return version;
}

public boolean isItYourStory() {

if (viewPager.getCurrentItem() == 0) {
return true;
}
return false;
}

// method to save variable in preference
public void saveInPreference(String name, String content) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name, content);
editor.commit();
}

// getting content from preferences
public String getFromPreference(String variable_name) {
String preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name, "");

return preference_return;
}

// method to save variable in preference
public void saveInPreferenceBoolean(String name, boolean content){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(name, content);
editor.commit();
}

// getting content from preferences
public boolean getFromPreferenceBoolean(String variable_name){
boolean preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getBoolean(variable_name, false);

return preference_return;
}

public void setTitle(String title) {
actionTitle.setText(title);
}
}

当调用 Activity 的 onResume() 时,我应该如何在第 3 个选项卡中设置 YourCollegeFragment

最佳答案

您可能想要更改 Fragment 中膨胀的布局,而不是替换框架本身。在 fragment 内,根据您的条件扩充布局并相应地扩充 View 。这是我试过的示例代码。

public class DemoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview;

if (your condition is true) {
// layout_one will be the layout for your CollegeSelectFragment
rootview = inflater.inflate(R.layout.layout_one, container, false);
} else {
// layout_one will be the layout for your YourCollegeFragment
rootview = inflater.inflate(R.layout.layout_two, container, false);
}
setupViews(rootview);
return rootview;
}


private void setupViews(View v) {
if (your condition is true -- same as above check) {
// find and set view that are in the layout_one.xml
TextView bigText = (TextView) v.findViewById(R.id.bigText);
bigText.setText("set from code");
} else {
// find and set view that are in the layout_two.xml
TextView bigText2 = (TextView) v.findViewById(R.id.bigText2);
bigText2.setText("big text 2 set from code");
}

}

关于java - 根据特定条件替换 ViewPager fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402018/

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