search

Home  >  Q&A  >  body text

android - SwipeRefreshLayout的onRefresh的方法

public class RvList extends BaseFragment {

    private boolean isConnected;
    public RecyclerView mRecyclerView;
    private FloatingActionButton floatingActionButton;
    private SwipeRefreshLayout swipeRefreshWidget;
    private RvAdapter adapter;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.viewpager_rv, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv);
        swipeRefreshWidget = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        swipeRefreshWidget.setColorSchemeResources(R.color.colorPrimaryDark, R.color.colorAccent,R.color.colorPrimary);//setColorSchemeResources():设置进度条的颜色主题,最多设置四种
        floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab);//FloatingActionButton的Id
        floatingActionButton.setOnClickListener(new View.OnClickListener() {//FAB的点击事件
            @Override
            public void onClick(View v) {
                mRecyclerView.smoothScrollToPosition(0);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity,LinearLayoutManager.VERTICAL,false));
        return view;
    }

    @Override
    protected void initData() {
        isConnected = Utility.checkNetworkConnection(mActivity);
        adapter = new RvAdapter(mActivity);
        swipeRefreshWidget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (isConnected) {
                            new LoadNewsTask(adapter).execute();
                            Toast.makeText(mActivity, "刷新完成", Toast.LENGTH_SHORT).show();
                            swipeRefreshWidget.setRefreshing(false);
                        } else {
                            Utility.noNetworkAlert(mActivity);
                            swipeRefreshWidget.setRefreshing(false);//设置SwipeRefreshLayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到View中后,设置为false。
                        }
                    }
                },1000);
            }
        });
/*        if (isConnected) new LoadNewsTask(adapter).execute();
        else Utility.noNetworkAlert(mActivity);*/
        mRecyclerView.setAdapter(adapter);
    }
}

注:initViewonCreateViewinitDataonActivityCreated当中

注释掉的那两句话

if (isConnected) new LoadNewsTask(adapter).execute(); else Utility.noNetworkAlert(mActivity);

之前可以完美允许
但是想添加个SwipeRefreshLayout进行下拉刷新后的加载 但是 Toast打印出来了就是加载不出来数据
请问有人知道什么缘故吗

天蓬老师天蓬老师2772 days ago754

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:45:07

    After adding the data, call adapter.notifyiteminserted() to display it

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:45:07

    You have to understandnew Handler().postDelayed()的原理,它只是一个定时任务,根据你的设置postDelayed()中的代码只是延迟了1秒执行,但程序先执行的还是mRecyclerView.setAdapter(adapter);所以此时你要加载的数据还没有进行加载。就更别说更新数据了。你应该把mRecyclerView.setAdapter(adapter);放到postDelayed()中.
    In addition, network requests are time-consuming and need to be executed asynchronously. To update the data, it is executed in the callback method after the data is successfully obtained.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:45:07

    Just add the sentence "notifiydatasetchanged" under the Toast. The answer above is also correct. For asynchronous operations, just use new Thread. Don't use new Handler().postDelayed().

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:45:07

    The data of the adapter is not refreshed. The main thread branches off to a thread to execute the post, but the main thread will not stop. When the child thread executes the post, it has actually executed the setadapter. But the data had not come back yet.

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:45:07

    Just update the adapter in LoadNewsTask
    Even if you don’t use handler.post and those two sentences are not commented out, it may not necessarily run perfectly. If the network data request is too slow, the data may not be displayed.

    reply
    0
  • Cancelreply