5

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

 public class ItemDetailFragmentBlackBoard extends 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;  
    }
4

3 回答 3

4

最好在 onCreateView 函数中定义一个像简单的线性布局(称为屏幕)一样的 viewGroup,并将其填充到像 init() 这样的函数中。每次您想重新创建视图时,只需删除所有 linearLayout 的子项并调用 init()。

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

于 2013-02-04T06:27:42.437 回答
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 中删除一个项目,在我删除它之后,我用上面的解码代码重新调用我的片段,这样我就不会刷新了!

于 2013-01-23T13:17:47.970 回答
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;
于 2017-07-27T08:19:12.417 回答