Android 从其父活动刷新片段列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26606527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android refresh a fragment list from its parent activity
提问by user1319668
I have a main activity which contains the action bar with 3 menu buttons in it.
我有一个主要活动,其中包含带有 3 个菜单按钮的操作栏。
I then have a fragment within this main activity which has a list.
然后我在这个主要活动中有一个片段,它有一个列表。
I would like to be able to refresh the list in the fragment from the main activity, when one of the menu buttons is clicked, or preferably just removed all the rows from the list.
当单击菜单按钮之一时,我希望能够从主活动刷新片段中的列表,或者最好只是从列表中删除所有行。
Any help is appreciated.
任何帮助表示赞赏。
Thanks.
谢谢。
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
}
}
////////////////////MY LIST FRAGMENT CLASS
////////////////////我的列表片段类
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);
}
}
回答by Biraj Zalavadia
You can easily achieve this using INTERFACE
您可以使用 INTERFACE 轻松实现此目的
MainActivity.java
主活动.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;
}
}
回答by localhost
- Just make your update/refresh method public and call it from your Activity.
- 只需公开您的更新/刷新方法并从您的活动中调用它即可。
OR
或者
- Use LocalBroadcastManager or EventBus to send event from your Activity, and by subscribing to this event in a Fragment - react to it and call refresh/update method.
- 使用 LocalBroadcastManager 或 EventBus 从您的 Activity 发送事件,并通过在 Fragment 中订阅此事件 - 对其做出反应并调用 refresh/update 方法。
回答by Jeffrey
Your activity can call methods in the fragment by acquiring a reference to the Fragment.
您的活动可以通过获取对 Fragment 的引用来调用 Fragment 中的方法。
(1) Provide a tag when you add your fragment.
(1) 添加片段时提供标签。
transaction.add(R.id.fragment_container, myFragment, "myfragmentTag");
(2) In your hostingactivity you can find the fragment and have access to it's methods.
(2) 在您的托管活动中,您可以找到片段并可以访问它的方法。
FragmentManager fm = getSupportFragmentManager();
myFragment f = (myFragment) fm.findFragmentByTag("myfragmentTag");
f.refreshAdapter()
(3) refreshAdapter() could now call adapter.notifyDataSetChanged().
(3) refreshAdapter() 现在可以调用 adapter.notifyDataSetChanged()。
This is one of the recommended ways to communicate up to a fragment. The interface implementation is mainly for communicating back to the activity.
这是与片段进行通信的推荐方式之一。接口实现主要用于与活动进行通信。
回答by Hardik Patel
Biraj Zalavadia's answer is 100% right, you will call nay fragment methods from using interface.... this interface methods is running without error...
Biraj Zalavadia 的回答是 100% 正确,您将使用接口调用 nay 片段方法......
use this in MainActivity above oncreate private FragmentRefreshListener fragmentRefreshListener;
在上面的 MainActivity 中使用这个 oncreate private FragmentRefreshListener fragmentRefreshListener;
public FragmentRefreshListener getFragmentRefreshListener() {
return fragmentRefreshListener;
}
public void setFragmentRefreshListener(
FragmentRefreshListener fragmentRefreshListener) {
this.fragmentRefreshListener = fragmentRefreshListener;
}
inside of Activity
活动内部
private void refreshcall(String result2) {
// TODO Auto-generated method stub
if (getFragmentRefreshListener() != null) {
getFragmentRefreshListener().onRefresh(result2);
}
}
and put this in needed Fragment
并将其放入需要的 Fragment
private FragmentRefreshListener fragmentRefreshListener;
public FragmentRefreshListener getFragmentRefreshListener() {
return fragmentRefreshListener;
}
public void setFragmentRefreshListener(
FragmentRefreshListener fragmentRefreshListener) {
this.fragmentRefreshListener = fragmentRefreshListener;
}
回答by Amit Sharma
In Kotlin Get the list of Support Fragment from the activity and check Instance and then call fragment function
在 Kotlin 中从活动中获取 Support Fragment 列表并检查 Instance 然后调用 fragment 函数
val fragments = supportFragmentManager.fragments
for (fragment in fragments) {
if (fragment is HomeCategoriesFragment) {
fragment.updateAdapter() // Define function in Fragment
}
}
回答by Francesco verheye
Communicating with Other Fragments
与其他片段通信
http://developer.android.com/training/basics/fragments/communicating.html
http://developer.android.com/training/basics/fragments/communicating.html
This can also be used to communicate between an Activity and a Fragment.
这也可以用于在 Activity 和 Fragment 之间进行通信。
回答by Ramkailash
When you click on ActionBar any Button then call interface to refresh the ListFragment. Because in java interface is used for inter-communication.
当您单击 ActionBar 任何 Button 时,然后调用接口来刷新 ListFragment。因为在java中接口是用于相互通信的。