如何使用选项卡视图片段将RecyclerView中的数据绑定

时间:2017-03-19 14:35:15

标签: android android-fragments tabs android-recyclerview

我的应用程序中有3个Tab。假设第一个是A,B和C.我的MainActivity声明并设置一个Tab Listener,我的每个Tab再次有3个Activity。我的问题是,在标签A中我用Volley加载数据。在标签B中我也加载不同的数据。但有时我的RecyclerView,数据很奇怪(标签B或标签B中显示的标签B中的数据成功加载数据,但标签A没有显示任何内容)。我在每个活动选项卡中加载数据,如下面的代码。我做错了吗?或者我应该加载MainActivity中的所有数据?任何答案对我都有帮助。

之前谢谢
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.popular_tab, container, false);


    searchViewPopular = (SearchView) rootView.findViewById(R.id.searchView_Popular);

    popularRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_Popular);
    popularRecyclerView.setHasFixedSize(true);
    popularaLinearLayoutManager = new LinearLayoutManager(getContext());
    popularGenericAdapter = new GenericAdapter(popularRecyclerView);

    popularRecyclerView.setLayoutManager(popularaLinearLayoutManager);
    popularRecyclerView.setAdapter(popularGenericAdapter);

    popularData = new JSONArray();
    filteredPopularData = new JSONArray();

    filteredPopularData = ((GlobalVariable)this.getActivity().getApplication()).getPopularData();

    loadData();

    listener = new GenericAdapter.OnRecyclerViewListener() {
        @Override
        public void OnRecyclerViewItemClicked(RecyclerView recyclerView, int position) {

        }

        @Override
        public void OnRecyclerViewItemBind(RecyclerView recyclerView, GenericAdapter.GenericHolder view, int position) {
            try {
                currPopularData = filteredPopularData.getJSONObject(position);

                view.textViewTitle.setText(currPopularData.getString("title"));
                view.textViewDesc.setText("Popularity       : " + currPopularData.getString("popularity"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public int OnRecyclerViewItemCount(RecyclerView recyclerView) {
            return filteredPopularData.length();
        }
    };

    popularGenericAdapter.setOnRecyclerViewListener(listener);
    searchViewPopular.setOnQueryTextListener(searchListener);

    return rootView;
}

//  @Override
//  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    //   super.onViewCreated(view, savedInstanceState);

    //  loadData();
//  }

SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String searchText) {
        try {
            filteredPopularData = ProgramMethod.filterArray(popularData, "title", searchText, false);
            popularGenericAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
};

    public void loadData() {
    final RequestQueue requestQueue;
    Network network = new BasicNetwork(new HurlStack());
    requestQueue = new RequestQueue(new NoCache(), network);
    requestQueue.start();

    final String urlAPIGenre = "https://api.themoviedb.org/3/movie/popular?api_key=8517f7eb51ca49a4558f33f8c832c1db";
    final String TAG = "";

    final JsonObjectRequest jsonGenre = new JsonObjectRequest
            (Request.Method.GET, urlAPIGenre, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        popularData = response.getJSONArray("results");
                        filteredPopularData = popularData;

                        ((GlobalVariable)getActivity().getApplication()).setPopularData(filteredPopularData);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    System.out.println("filteredPopularData = " + response);
                    popularGenericAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
            });
    requestQueue.add(jsonGenre);
}

编辑:

这低于我的标签

enter image description here

enter image description here

enter image description here

在第一个标签中,应该显示带有文字Popularity的文字视图,但是在App首次打开时始终显示Top Rated内容

0 个答案:

没有答案