亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

android - SwipeRefreshLayout的onRefresh的方法
天蓬老師
天蓬老師 2017-04-17 17:43:07
0
5
817
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當前是否處于刷新狀態(tài),一般是在請求數(shù)據(jù)的時候設置為true,在數(shù)據(jù)被加載到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打印出來了就是加載不出來數(shù)據(jù)
請問有人知道什么緣故嗎

天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見證您的進步~~

reply all(5)
劉奇

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

黃舟

You have to understandnew Handler().postDelayed()的原理,它只是一個定時任務,根據(jù)你的設置postDelayed()中的代碼只是延遲了1秒執(zhí)行,但程序先執(zhí)行的還是mRecyclerView.setAdapter(adapter);所以此時你要加載的數(shù)據(jù)還沒有進行加載。就更別說更新數(shù)據(jù)了。你應該把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.

Ty80

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().

洪濤

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.

黃舟

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template