多个选项卡中的AsyncTask

时间:2015-06-24 15:51:35

标签: android android-fragments android-asynctask tabs android-recyclerview

我有一个有3个标签的应用。每个选项卡都需要从远程服务器上的PHP / MySQL WebService调用不同的数据集。每个标签都有一个AsyncTask,我遇到的问题是。每当我将PagerAdapter设置为显示多个标签时,应用程序就会崩溃。

每个标签都有一个RecyclerView,从WebService下载时会添加数据。

关于这个问题的任何想法?

如果我将它们作为单独的标签运行,那么脚本非常大并且运行完美。我认为问题在于tabHost预加载选项卡,因此一次执行多个AsyncTasks。

可根据要求提供代码。

Messages.java -

package ...
imports ...

public class Messages extends Fragment {
    RecyclerView recyclerView;
    MessagesAdapter messagesAdapter;
    private View rootView;
    public static List<Message> messageList = new ArrayList<>();
    RecyclerView.LayoutManager mManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tab_messages, container, false);
        mManager = new LinearLayoutManager(getActivity());

        recyclerView = (RecyclerView) rootView.findViewById(R.id.student_tab_messages_Container_RecyclerView);
        messagesAdapter = new MessagesAdapter(getActivity(), messageList);

        return rootView;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        new Load().execute();
    }
    private class Load extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPostExecute(Void o) {
            recyclerView.setAdapter(messagesAdapter);
            recyclerView.setLayoutManager(mManager);
        }
        @Override
        protected Void doInBackground(Void... params) {
            messageList = WebServices.get().getMessagesList();
            return null;
        }
    }
}

Groups.java -

package ...
imports ...


public class Groups extends Fragment {
    RecyclerView recyclerView;
    GroupsAdapter rvGroupsAdapter;
    private View rootView;
    public static List<Group> groups = new ArrayList<>();
    RecyclerView.LayoutManager mManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tab_groups, container, false);
        mManager = new LinearLayoutManager(getActivity());
        recyclerView = (RecyclerView) rootView.findViewById(R.id.student_tab_groups_Container_RecyclerView);
        rvGroupsAdapter = new GroupsAdapter(getActivity(), groups);
        new Load().execute();

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    }

    private class Load extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPostExecute(Void aVoid) {
            recyclerView.setAdapter(rvGroupsAdapter);
            recyclerView.setLayoutManager(mManager);
        }

        @Override
        protected Void doInBackground(Void... params) {
            groups = WebServices.get().getGroupsList();
            return null;
        }
        }
   }

选项卡中的2个,选项卡3包含敏感的项目特定代码。

0 个答案:

没有答案