java 如何调用 onCreateView 方法或刷新我的片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14464302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:22:14  来源:igfitidea点击:

How call onCreateView Method or refresh my fragment?

javaandroidandroid-fragmentsandroid-fragmentactivity

提问by alfo888_ibg

I would like to refresh or call onCreateView in the code following . I have a contex menù to delete an Item and after I would like to refresh the fragment with the new Item.. Thanks at all!

我想在以下代码中刷新或调用 onCreateView 。我有一个上下文菜单来删除一个项目,然后我想用新项目刷新片段..非常感谢!

public class ItemDetailFragmentBlackBoard extends Fragment {

公共类 ItemDetailFragmentBlackBoard 扩展片段 {

@Override

 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle  savedInstanceState) {
        ....
       return rootView;
 }

}

}

 /** Menu on LongClick */
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
    {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Delete");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if(item.getTitle()=="Delete"){
        String status="";
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        int posizione = info.position;
        String[] messaggioDaCancellare= S.getMessaggiInfo().get(posizione); 
        try{
            JSONObject del =ProxyUtils.proxyCall("deleteMessage",messaggioDaCancellare[4]);
            status=del.getString("status");
        } catch (Exception e) {
            Log.i("Eccezione", e.toString());
        }
        Activity activity= getActivity();
        if(status.equals("OK")){

                   **HERE......I would like to refresh my fragment o recall onCreateView method...**

            Toast.makeText(activity, "Delete avvenuta", Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(activity, "Delete non riuscita", Toast.LENGTH_SHORT).show();
    } else {return false;}  
    return true;  
}

回答by Afshin

It's better to define a viewGroup like a simple linearLayout (call it screen) in onCreateView function, and fill it in a function like init(). Each time you want to re-create your view, just remove all linearLayout's children and call init().

最好在onCreateView函数中定义一个像简单的linearLayout(称之为screen)的viewGroup,并用init()这样的函数填充它。每次要重新创建视图时,只需删除所有 linearLayout 的子项并调用 init()。

You did good job but it's more expensive than my simple solution.

你做得很好,但比我的简单解决方案贵。

回答by alfo888_ibg

I solved my problem replecing my fragmet with itself

我解决了用自己替换片段的问题

the code is :

代码是:

{

    arguments.putString(ItemDetailFragmentBlackBoard.ARG_ITEM_ID, id);
    ItemDetailFragmentBlackBoard fragment= new ItemDetailFragmentBlackBoard();
    fragment.setArguments(arguments);
    getFragmentManager().beginTransaction().replace(R.id.item_detail_container,   fragment).commit();   
}

ItemDetailFragmentBlackBoard is my fragment. I am deleting an Item from listView and after I delete it i re-call my fragment with de code above so I get un refresh!

ItemDetailFragmentBlackBoard 是我的片段。我正在从 listView 中删除一个项目,在我删除它之后,我用上面的 de 代码重新调用我的片段,这样我就不会刷新了!

回答by Ihdina

public class ItemDetailFragmentBlackBoard extends Fragment {
    public static View _rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (_rootView == null || _isRefreshDashboard) {
        _rootView = inflater.inflate(R.layout.ItemDetailFragmentBlackBoard, container, false);

        // your code can't be change without refreshFragment in here..

        _isDashboardRefresh = false;
    }
}

// Global variables

// 全局变量

public abstract class CommonBase extends AppCompatActivity {
    public static boolean _isRefreshDashboard;
}

// Refresh your fragment

// 刷新你的片段

CommonBase._isRefreshDashboard = true;