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

时间:2013-01-22 17:18:42

标签: java android android-fragments android-fragmentactivity

我想在下面的代码中刷新或调用onCreateView。 我有一个上下文来删除一个Item,之后我想用新的Item刷新片段。 谢谢!

公共类ItemDetailFragmentBlackBoard扩展Fragment {

@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;  
}

3 个答案:

答案 0 :(得分:4)

最好在onCreateView函数中定义一个类似简单的linearLayout(称之为屏幕)的viewGroup,并将其填入init()之类的函数中。每次要重新创建视图时,只需删除所有linearLayout的子节点并调用init()。

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

答案 1 :(得分:2)

我解决了我的问题,用自己重新编写我的碎片

代码是:

{

    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是我的片段。我正在从listView中删除一个项目,在我删除它后,我用上面的代码重新调用我的片段,所以我得到了不刷新!

答案 2 :(得分:0)

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

//全局变量

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

//刷新你的片段

CommonBase._isRefreshDashboard = true;