0

I'm adding new JSON item by POST from AddItem fragment and when item is added you return to ItemListFragment where I call getLoaderManager().initLoader(0, null, mLoaderCallbacks); at onStart(). But the newly added item doesn't show on RecyclerView. If I refresh RecyclerView with SwipeRefreshLayout then the new item shows.

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);
    }
neX
  • 35
  • 8

1 Answers1

0

You can force your adapter to render new item calling RecyclerView.Adapter#notifyDataSetChanged() or RecyclerView.Adapter#notifyItemInserted(int).

Miha_x64
  • 5,973
  • 1
  • 41
  • 63
  • At onBindViewHolder() I could do something like if(position == getItemCount() -1) notifyDataSetChanged(); But that isn't working, and anyway shouldn't the recyclerview get updated after transaction because it should be loading recyclerview again? – neX Apr 06 '16 at 18:02
  • @neX, you mustn't call notifyDataSetChanged from onBindViewHolder (it's useless and may become a cycle). I think, you should call notifyDataSetChanged after addItem, but I don't know what is ItemPoster. – Miha_x64 Apr 07 '16 at 07:44
  • ItemPoster is just posting item to server. I'm not sure where I can call notfiDataSetChanged when i'm adding item on other class and then doing transaction to go to the recyclerview fragment. – neX Apr 08 '16 at 11:12
  • @neX, it depends on your adapter. For example, if it is backed by a List, you shoud call `notify...` just after `List#add`. – Miha_x64 Apr 08 '16 at 15:40