我正在尝试刷新片段,具体取决于用户所在的抽屉......
这是我的尝试,它没有做任何事情,但没有给我一个错误。
这是我的代码
public void fragmentRefresh() {
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
.commit();
}
这是抽屉片段代码:
/**
* Diplaying fragment view for selected nav drawer list item
*/
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new BrowseFragment();
break;
case 2:
fragment = new InviteFragment();
break;
case 3:
fragment = new ProfileFragment();
break;
case 4:
fragment = new SettingsFragment();
break;
case 5:
callConfirmLogout();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment, "TagForRefresh").commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
在标签swiper片段类中实现:
public class ViewVotesActivity extends FragmentActivity implements ActionBar.TabListener{
//Tab options
private ViewPager viewPager;
private TabSwipeAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Created", "Up", "Down"};
//for custom menu to add progress bar to refresh
private MenuItem menuItem;
//Search view action bar
private SearchView search_action_bar;
//getApplicationContext().getString(R.string.created),getApplicationContext().getString(R.string.upvote), getApplicationContext().getString(R.string.downvote)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_votes);
// Initializing tab
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabSwipeAdapter(getSupportFragmentManager(), new UserCreatedFragment(), new UserUpVotesFragment(),new UserDownVotesFragment());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
setupActions(menu);
callSearchActionBar(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_refresh:
menuItem = item;
menuItem.setActionView(R.layout.refresh_progress_bar);
//menuItem.expandActionView();
fragmentRefresh();
TestTask task = new TestTask();
task.execute("test");
return true;
case R.id.action_discover:
addNewItem();
return true;
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void addNewItem() {
// user redirect to register followup
Intent createItemIntent = new Intent(ViewVotesActivity.this, CreateItemActivity.class);
// Closing all the Activities
createItemIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
createItemIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
startActivity(createItemIntent);
}
public void fragmentRefresh() {
// // Insert the fragment by replacing any existing fragment
// FragmentManager fragmentManager = getFragmentManager();
//
// fragmentManager.beginTransaction()
// .replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
// .commit();
}
private class TestTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// Simulate something long running
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
menuItem.collapseActionView();
menuItem.setActionView(null);
}
};
public void setupActions(Menu menu) {
MenuItem refreshItem = menu.findItem(R.id.action_refresh);
MenuItem discoverItem = menu.findItem(R.id.action_discover);
if (isAlwaysExpanded()) {
} else {
refreshItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
discoverItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
}
public void callSearchActionBar(Menu menu) {
//find the menu item and set search view at the same time
MenuItem searchItem = menu.findItem(R.id.action_search);
search_action_bar
= (SearchView) searchItem.getActionView();
if (isAlwaysExpanded()) {
search_action_bar.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
search_action_bar.setSearchableInfo(info);
}
search_action_bar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
Log.e("Query = ", newText);
return false;
}
public boolean onQueryTextSubmit(String query) {
Log.e("Submit", "Submited " + query);
// user redirect to register followup
Intent searchIntent = new Intent(ViewVotesActivity.this, SearchActivity.class);
searchIntent.putExtra("search", query);
// Closing all the Activities
searchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
searchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
startActivity(searchIntent);
return false;
}
public boolean onClose() {
Log.e("query close", "query closingggg");
return false;
}
});
}
protected boolean isAlwaysExpanded() {
return false;
}
}
答案 0 :(得分:1)
<强>更新强>: 看起来您正在尝试告诉活动,是时候根据导航抽屉中的选择来更改内容片段。
这个问题有两个部分。
如果您只需要重置其数据,我看不出有什么理由需要完全破坏Fragment的视图。看起来像一个设计缺陷。
更换片段不应该意味着你想彻底销毁它,因为用户可以简单地返回或按下最近删除的片段。再次重新创建它将是低效的。
就像我在评论中提到的那样,需要更多代码来了解您当前的方法。
通常一个简单的界面(就像我下面描述的那样)就足够了。您的活动可以在抽屉中收到“点击”并决定要替换的片段(如果需要)。
请更具体,并提供更多代码。
旧回应:
警告:如果您尝试更换当前片段,请确保没有收到错误的FragmentManager。您执行getFragmentManager()
但我确定您正在使用支持库,因此需要执行getSupportFragmentManager();
。
您没有提到您要定位的版本,因此很难知道。
另一方面,
如果您要做的是告诉当前可见片段刷新......您应该使用更常见的方法进行对象到对象的通信。
你的事情过于复杂。
(警告:伪代码)
创建如下界面:
public interface Refreshable{
void onShouldRefresh();
}
让DrawerController能够存储观察者/听众......
public class DrawerController {
private List<Refreshable> mListeners = new ArrayList<Refreshable>();
public void addRefreshListener(E listener) {
if (listener != null && !mListeners.contains(listener)) {
mListeners.add(listener);
}
}
public void removeRefreshListener(E listener) {
if (listener != null) {
mListeners.remove(listener);
}
}
}
让你的片段实现它(并在可见的时候订阅它)
public class HomeFragment extends Fragment implements Refreshable {
// your fragment code
public void onShouldRefresh(){
// do the refresh
}
@Override
public void onResume() {
super.onResume();
yourDrawerController.addRefreshListener(this);
}
@Override
public void onPause() {
super.onPause();
yourDrawerController.removeRefreshListener(this);
}
}
现在制作一个方法,告诉感兴趣的各方(在这种情况下你的“可刷新”对象)是时候刷新了,把它添加到你的DrawerController ......
public void refresh(){
for (Refreshable listener : mListeners) {
if (listener != null) {
listener.onShouldRefresh();
}
}
}
最后,在您的代码中,请致电refresh()
并喝一杯啤酒。
我是否想念你的观点?对我来说,这不是100%明确的。