Android - 使用AsyncTask加载更多数据,AsyncTask从RecyclerView中的API获取数据

时间:2016-02-22 06:44:55

标签: android android-asynctask android-recyclerview

我正在尝试动态添加项目到我的RecyclerView

基本上,我遵循Instagram风格,特别是在Instagram的搜索片段中。目前,我的应用程序获取服务器发送的项目数(默认为10),并将其添加到recyclerview上显示的项目数。

我有一个接口来获取服务器的输出,这里是OnLoadMoreListener

System.out.println("OnLoadMore");
list.add(null);
profilTemanAdapter.notifyItemInserted(list.size());
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //remove progress item
        list.remove(list.size() - 1);
        profilTemanAdapter.notifyItemRemoved(list.size());

        TimelineFriendListAPI timelineFriendListAPIInput = new TimelineFriendListAPI();
        timelineFriendListAPIInput.kevin.query.user_id = userId;
        timelineFriendListAPIInput.kevin.query.other_user_id = otherUserId;
        timelineFriendListAPIInput.kevin.query.friend_mode = friendMode;

        nextFriendList = result.kevin.results.friend_list;
        TimelineFriendListAPIFunc timelineFriendListAPIFunc = new TimelineFriendListAPIFunc(BottomProfilTemanFragment.this.getActivity());
        timelineFriendListAPIFunc.delegate = BottomProfilTemanFragment.this;

        if (nextFriendList.size() == 10) {
            counter += 10;
            timelineFriendListAPIInput.kevin.query.friend_count = String.valueOf(counter);
        Log.d("CounterBefore", String.valueOf(counter));
            timelineFriendListAPIFunc.execute(timelineFriendListAPIInput);
            Log.d("CounterAfter", String.valueOf(counter));
            Log.d("FriendLists", nextFriendList.get(0).friend_alias);
            //Refresh Per Item
            for (int i = 0; i < 10; i++) {
                list.add(nextFriendList.get(i));
                profilTemanAdapter.notifyItemInserted(list.size());
                Log.d("FriendLists", nextFriendList.get(i).friend_alias);
            }
            Log.d("FriendLists", nextFriendList.get(10).friend_alias);
            Log.d("OnLoadNotifFriendSize10", "size = " + list.size());
            profilTemanAdapter.setLoaded();
        } else if (nextFriendList.size() < 10 && nextFriendList.size() > 0) {
            int friendCounter = nextFriendList.size();
            counter += friendCounter;
            timelineFriendListAPIInput.kevin.query.friend_count = String.valueOf(counter);
            timelineFriendListAPIFunc.execute(timelineFriendListAPIInput);
            //Refresh Per Item
            for (int i = 0; i < friendCounter; i++) {
                list.add(nextFriendList.get(i));
            }
            profilTemanAdapter.notifyItemInserted(list.size());
            profilTemanAdapter.setLoaded();
        } else {

        }
    }
}, 1000);
System.out.println("loadMore");

这是同一类中的接口函数

result = output; //result is a global variable to be accessed inside the OnLoadMore

//profilTemanAdapter = new ProfilTemanAdapter(list, recyclerView, output);
//createlist();
handler = new Handler();
list = output.kevin.results.friend_list;profilTemanAdapter = new `ProfilTemanAdapter(list, recyclerView, output);`
profilTemanAdapter.setOnLoadMoreListener(profilTemanAdapterHandler);
recyclerView.setAdapter(profilTemanAdapter);
Log.d("FriendLists", list.get(0).friend_alias);

正如可以预料的那样,nextFriendList.get(10).friend_alias中的List仍然与nextFriendList.get(0).friend_alias相同,因为nextFriendList尚未通过界面更新。

如何在等待AsyncTask完成时正确显示,但不使用.get()以便它不像Instagram(或任何“InfiniteScrolling”应用程序)那样阻止UI线程?

小更新:

在Async中使用ProgressBar,以便在显示之前加载从服务器获取的信息,但它会使布局混乱。在EditText之上有RecyclerViewOnLoadMore会在ProgressBar上重复。此外,对于每个信息加载,弹出OnLoadMore看起来不太好..

另一个更新:

我能够显示CardView个项目,但它会替换新项目之前的项目。

0 个答案:

没有答案