我有一个主活动,其中包含带有3个菜单按钮的 *** 作栏.
然后我在这个主要活动中有一个片段,里面有一个列表.
我希望能够刷新主活动片段中的列表,当单击其中一个菜单按钮时,或者最好只删除列表中的所有行.
任何帮助表示赞赏.
谢谢.
public class Favourite extends SherlockFragmentActivity { Actionbar actionbar; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.favourite); actionbar = getSupportActionbar(); actionbar.setdisplayShowTitleEnabled(false); BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.actionbar_bg); bg.setTileModeX(TileMode.REPEAT); getSupportActionbar().setBackgroundDrawable(bg); getSupportActionbar().setIcon(R.drawable.favourite_Title); actionbar.setNavigationMode(Actionbar.NAVIGATION_MODE_TABS); Actionbar.Tab tabAll = actionbar.newTab(); Actionbar.Tab tabfavs = actionbar.newTab(); Actionbar.Tab tabhist = actionbar.newTab(); tabAll.setText("all"); tabfavs.setText("favs"); tabhist.setText("hist"); tabAll.setTabListener(new MyTabListener()); tabfavs.setTabListener(new MyTabListener()); tabhist.setTabListener(new MyTabListener()); actionbar.addTab(tabAll); actionbar.addTab(tabfavs); actionbar.addTab(tabhist); try{ } catch(Exception e) { } } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.actionbar_itemList_favourite, menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { switch(item.getItemID()) { case R.ID.history: break; case R.ID.favourite: Intent favAct = new Intent(this, Favourite.class); startActivity(favAct); break; case R.ID.delete: ///I WANT TO BE ABLE TO REFRESH FRAGMENTList FROM HERE } return true; } }class MyTabListener implements Actionbar.TabListener { public voID onTabSelected(Tab tab, FragmentTransaction ft) { if(tab.getposition()==0) { FavouriteallWords frag = new FavouriteallWords(); ft.replace(androID.R.ID.content, frag); } else if(tab.getposition()==1) { FavouriteFavWords frag = new FavouriteFavWords(); ft.replace(androID.R.ID.content, frag); } else if(tab.getposition()==2) { FavouriteHistWords frag = new FavouriteHistWords(); ft.replace(androID.R.ID.content, frag); } } public voID onTabUnselected(Tab tab, FragmentTransaction ft) { // Todo auto-generated method stub } public voID onTabReselected(Tab tab, FragmentTransaction ft) { // Todo auto-generated method stub }}
////////////////////我的列表碎片类
public class FavouriteallWords extends ListFragment { ArrayAdapter<String> adapter; List<String> stringOfFavWords; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup group, Bundle saved) { adapter = new ArrayAdapter<String>( inflater.getContext(), R.layout.row, stringOfFavWords); setlistadapter(adapter); return super.onCreateVIEw(inflater, group, saved); } @OverrIDe public voID onActivityCreated (Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); }}
解决方法:
您可以使用INTERFACE轻松实现此目的
MainActivity.java
public class MainActivity extends Activity { public FragmentRefreshListener getFragmentRefreshListener() { return fragmentRefreshListener; } public voID setFragmentRefreshListener(FragmentRefreshListener fragmentRefreshListener) { this.fragmentRefreshListener = fragmentRefreshListener; } private FragmentRefreshListener fragmentRefreshListener; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button b = (button)findVIEwByID(R.ID.btnRefreshFragment); b.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(getFragmentRefreshListener()!=null){ getFragmentRefreshListener().onRefresh(); } } }); } public interface FragmentRefreshListener{ voID onRefresh(); }}
MyFragment.java
public class MyFragment extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw v = null; // some vIEw /// Your Code ((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener() { @OverrIDe public voID onRefresh() { // Refresh Your Fragment } }); return v; }}
总结 以上是内存溢出为你收集整理的Android从其父活动刷新片段列表全部内容,希望文章能够帮你解决Android从其父活动刷新片段列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)