我有一个片段,它是标签视图的一部分。当我按下后退按钮时,我想重新启动此片段。但我不知道如何更新它。 我试过这样的代码: Restart fragment inside activity但它没有用。
这是一个检测后退按钮的代码:
@Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button
return true;
}
return false;
}
});
}
这是一个片段类按钮:
public class MoviesFragment extends Fragment {
static ListView list;
static String[] numbers_text = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen" };
String[] songs = new String[] { "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15" };
MainCustomList adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movies, container, false);
adapter = new MainCustomList(this.getActivity(), numbers_text);
list=(ListView) view.findViewById(R.id.list);
list.setAdapter(adapter);
return view;
}
主要:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
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
* */
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 onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
答案 0 :(得分:14)
使用此代码:
todo_url = '/todolists/' + todolist_id + '/todos/update_row_order'
$.ajax(
type: 'POST'
url: todo_url
dataType: 'json'
data: { todo: {todo_id: item_id, todolists_attributes: { row_order: position } } }
)
&安培;这将导致您的YourFragmentClass fragment = (YourFragmentClass)
getFragmentManager().findFragmentById(R.id.your_fragment_container_id)
getFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
fragment
方法再次被调用&刷新它的布局。
<强>更新强>
使用OnCreateView
时,您可以通过以下方式访问您的片段:
Support Library
并使用上面相同的代码段:
int YOUR_FRAGMENT_POSITION = 2; // in your case you wanna access the 3rd fragment, 0 indexed
YourFragmentClass fragment = (YourFragmentClass) getSupportFragmentManager().getFragments().get(YOUR_FRAGMENT_POSITION);
如果您尝试通过getSupportFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
访问fragment
,最终可能会收到PagerAdapter
,告诉您该片段未能保存其IllegalStateException
。