Android - notifydatasetchanged不起作用viewpager

时间:2016-08-18 16:34:35

标签: java android

更新帖子:

FriendsFragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        parentView      = inflater.inflate(R.layout.fragment_friends, container, false);
        recyclerView    = (RecyclerView) parentView.findViewById(R.id.list_main);
        mArrayList  = new ArrayList<>();

        bindViews();
        setHasOptionsMenu(true);
        return parentView;
    }



private void bindViews() {
        parentActivity = (MainActivity) getActivity();
        adapter = new PostThreadAdapter(parentView.getContext(),         postsArrayList);
        recyclerView.setAdapter(adapter);
 LinearLayoutManager layoutManager = new LinearLayoutManager(parentActivity);
        recyclerView.setLayoutManager(layoutManager);
getItemsFromDatabase();
}

private getItemsFromDatabase()
{

        StringRequest stringRequest = new StringRequest(StringRequest.Method.PUT, "SERVERURL", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    final FeedResponse feedResponse = gson.fromJson(response, FeedResponse.class);

  mArrayList.clear();
                        mArrayList.addAll(feedResponse.getArrayList());
                            adapter.notifyDataSetChanged();
}
  } catch (Exception e) {
                    Log.e("C", "CATH " + e.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
};

    volley.addRequest(stringRequest, "FriendsFragment");

}

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
            Collections.sort(mArrayList, new Comparator<Objectx>() {
                    @Override
                    public int compare(Objectx d, Objectx t1) {
                        return t1.getPostType() - d.getPostType();
                    }
                });
         adapter.notifydatasetchanged(); // this only work the first time
   }

问题: 我有1个活动(MainActivity)

我的活动有framelayout,这可能包含2个片段。

HomeFragment或UserFragment

我的HomeFragment包含TabLayout和viewpager,其中包含2个片段:

Tab1:FriendsFragment Tab2:NotifiFragment

在我的朋友片段中,我有自定义适配器和视图持有者的recyclerview。 这在第一次工作正常,但是当我更改片段(从HomeFragment更改为UserFragment)并再次返回Homefragment时,适配器.notifyDataSetChanged();不再工作了。我已经尝试过了:

@Override
public void onResume() {

    super.onResume();
    array.clear();
    getInfoFromDatabase(); // this include .addAll array
    adapter.notifyDataSetChanged();
    Log.d(TAG, "List Frag Resumed");

}

此外:

@Override
    public void onResume() {

        super.onResume();
        adapter = new CustomAdapter(mView.getContext(), array);
        recyclerview.setAdapter(adapter);
        getInfoFromDatabase(); // this include .addAll array
        adapter.notifyDataSetChanged();
        Log.d(TAG, "List Frag Resumed");

    }

但没有效果。我也使用集合排序来命令我的项目,我注意到数组的项目发生了变化但在应用程序中看不到,因为notifydatasetchanged什么也没做。

0 个答案:

没有答案