【问题标题】:AsyncTaskLoader not loading JSON data to RecyclerView when returning from another fragment从另一个片段返回时,AsyncTaskLoader 未将 JSON 数据加载到 RecyclerView
【发布时间】:2016-03-29 16:33:48
【问题描述】:

我正在通过 POST 从 AddItem 片段添加新的 JSON 项目,当添加项目时,您返回到 ItemListFragment,我在 onStart() 调用 getLoaderManager().initLoader(0, null, mLoaderCallbacks);。但是新添加的项目没有显示在 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();
        }
    }
}

添加项目:

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

项目列表片段:

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

【问题讨论】:

    标签: android json fragment android-recyclerview asynctaskloader


    【解决方案1】:

    您可以强制适配器渲染调用RecyclerView.Adapter#notifyDataSetChanged()RecyclerView.Adapter#notifyItemInserted(int) 的新项目。

    【讨论】:

    • 在 onBindViewHolder() 我可以做类似 if(position == getItemCount() -1) notifyDataSetChanged();但这不起作用,无论如何recyclerview不应该在事务后更新,因为它应该再次加载recyclerview?
    • @neX,不能从onBindViewHolder调用notifyDataSetChanged(没用,可能会变成循环)。我认为,你应该在 addItem 之后调用 notifyDataSetChanged,但我不知道 ItemPoster 是什么。
    • ItemPoster 只是将项目发布到服务器。当我在其他类上添加项目然后进行事务以转到 recyclerview 片段时,我不确定在哪里可以调用 notfiDataSetChanged。
    • @neX,这取决于您的适配器。例如,如果它由 List 支持,则应在 List#add 之后调用 notify...
    猜你喜欢
    • 1970-01-01
    • 2022-07-07
    • 2021-06-21
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多