gpt4 book ai didi

android - 按下后退按钮时重新启动 fragment 类

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:46 24 4
gpt4 key购买 nike

我有一个 fragment 是选项卡 View 的一部分。当我按下后退按钮时,我想重新启动这个 fragment 。但我不知道如何刷新它。我尝试了一些这样的代码: 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;
}
});
}

这是一个 fragment 类按钮:

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) {
}

}

最佳答案

使用这段代码:

YourFragmentClass fragment = (YourFragmentClass) 
getFragmentManager().findFragmentById(R.id.your_fragment_container_id)

getFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();

这将导致您的 fragment OnCreateView方法再次被调用并刷新其布局。

更新:

使用支持库时,您可以通过这种方式访问​​您的 fragment :

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);

并使用上面相同的代码 fragment :

getSupportFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();

如果您尝试通过 PagerAdapter 访问您的 fragment,您可能会以 IllegalStateException 结束,告诉您该 fragment 已失败保存它的状态

关于android - 按下后退按钮时重新启动 fragment 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461560/

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