从另一个片段返回时,AsyncTaskLoader没有将JSON数据加载到RecyclerView

时间:2016-03-29 16:33:49

标签: android json fragment android-recyclerview asynctaskloader

我正在通过POST从AddItem片段添加新的JSON项目,当添加项目时,您将返回到ItemListFragment,我在getLoaderManager().initLoader(0, null, mLoaderCallbacks);调用onStart()。但新添加的项目不会显示在RecyclerView上。如果我使用SwipeRefreshLayout刷新RecyclerView,则显示新项目。

BaseFragmentActivity:

public class BaseFragmentActivity extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setVisibility(View.VISIBLE);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment newFragment = new AddItem();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                transaction.replace(R.id.fragment_container, newFragment);
                transaction.addToBackStack(null);

                transaction.commit();
            }
        });
        if(fragment == null) {
            fragment = new ItemListFragment();
            fm.beginTransaction().replace(R.id.fragment_container, fragment).addToBackStack(null).commit();
        }
    }
}

的AddItem:

    addItemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        addItem();

                        Fragment newFragment = new ItemListFragment();
                        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

                        transaction.replace(R.id.fragment_container, newFragment);
                        transaction.addToBackStack(null);

                        transaction.commit();
                    }

  public void addItem(){
        String item = itemEditText.getText().toString();
        String category = categoryEditText.getText().toString();

        Item itemModel = new Item(item, category, false);
        ItemPoster.post(itemModel);
    }

ItemListFragment:

@Override
    public void onStart()
    {
        super.onStart();
        getLoaderManager().initLoader(0, null, mLoaderCallbacks);
    }

1 个答案:

答案 0 :(得分:0)