从儿童片段背面重新加载片段

时间:2018-02-06 12:55:51

标签: android json fragment

我有两个片段 - 父片段和子片段。我的父片段由RecyclerView和CardView组成,包含一些用户数据,Child Fragment包含Cardview内容的详细页面,其中包含一些喜欢和评论按钮。当我点击子片段中的like按钮时,在后面按下父片段没有通过详细页面中所做的更改来刷新。 我试过在onResume方法中调用我的API,但它似乎不起作用!!!

public void getNewsFeed() {
    System.out.println("=* called getNewsFeed method");
    if (Utils.isNetworkAvailable(context)) {
        new AsyncTask<NewsFeedRequest, Void, Response>() {

            ProgressDialog progressDialog;
            Response response;
            int statusCode = 0;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                try {
                    progressDialog = Utils.showProgressDialig(Utils.PLEASE_WAIT, Utils.UPLOADING_DETAILS, false, context);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected Response doInBackground(NewsFeedRequest... newsFeedRequests) {

                Gson gson = new Gson();
                String bodySr = gson.toJson(newsFeedRequests[0]);

                EpaLog.e(TAG, "RequestBody--" + newsFeedRequests[0]);

                // String bodySr = "{\"org_id\": " + org_id + ",\n  \"trackedLikeActivityPostIds\": " + trackedLikeActivityPostIds + ",\n  \"trackedCommentActivityPostIds\":\"" + trackedCommentActivityPostIds + ",\n\"trackedUpdateActivityPostIds\":\"" + trackedUpdateActivityPostIds + ",\n\"offSetValue\":\""+offSetValue+"\"}";

                EpaLog.e(TAG, "NewsFeed Body--" + bodySr);

                OkHttpClient client = new OkHttpClient();
                String regId = SharedPreferenceHelper.getPreference(context, SharedPreferenceHelper.ACCESS_TOKEN);

                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, bodySr);

                EpaLog.e(TAG, "bodyStr-- " + bodySr);

                Request request = new Request.Builder()
                        .url(WebUtils.URL + WebUtils.Main + WebUtils.GET_NEWSFEED)
                        .post(body)
                        .addHeader("authorization", regId)
                        .build();
                try {
                    response = client.newCall(request).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return response;
            }

            @Override
            protected void onPostExecute(Response response) {
                super.onPostExecute(response);
                String responseBody;

                try {
                    EpaLog.e(TAG, "response-- " + response.toString());
                    statusCode = response.code();


                    if (statusCode == 200) {

                        Gson gson = new Gson();
                        //    newsfeed_responseBody_copy = response.body().string();
                        responseBody = response.body().string();
                        Reader reader = new InputStreamReader(Utils.getInputstream(responseBody));
                        newsFeedDataList = gson.fromJson(reader, NewsFeedDataList.class);

                        EpaLog.e(TAG, "*** Response Body String : --- : " + responseBody);

                        for (NewsFeedData data : newsFeedDataList.getNewsfeeds()) {
                            newsFeedDatas.add(data);
                        }

                        offSetValue = 0;
                        offSetValue = newsFeedDataList.getOffset();

                        EpaLog.e(TAG, "OffsetValue: " + offSetValue);

                        customNewsFeedAdapter = new CustomNewsFeedAdapter(context, newsFeedDatas, fragmentTransaction, itemClickListener);
                        recyclerView.setAdapter(customNewsFeedAdapter);
                        customNewsFeedAdapter.notifyDataSetChanged();


                        customNewsFeedAdapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(int position, View view, RecyclerView.ViewHolder vh) {


                                newsFeed_SourceID = newsFeedDatas.get(position).getNewsfeedSourceId();
                                newsFeed_type = newsFeedDatas.get(position).getNewsfeedType();


                                EpaLog.e(TAG, "post id--" + newsFeed_SourceID);
                                EpaLog.e(TAG, "post type --" + newsFeed_type);

                                Utils.saveSharedPrefValue(context, SharedPreferenceHelper.POST_ID, newsFeed_SourceID);
                                Utils.saveSharedPrefValue(context, SharedPreferenceHelper.TYPE, newsFeed_type);

//                                     time_zone_id = newsFeedDatas.get(position).getPostTime();

                                EpaLog.e(TAG, "timeZone is---" + time_zone_id);


                                NewsFeedProfileFragment newsFeedProfileFragment = NewsFeedProfileFragment.newInstance(customNewsFeedAdapter.getItem(position).getUserName());
                                start(newsFeedProfileFragment);


                                Bundle args = new Bundle();
                                args.putString("user_name", customNewsFeedAdapter.getItem(position).getUserName());
                                args.putInt("position", position);
                                newsFeedProfileFragment.setArguments(args);

                                EpaLog.e("itemPosition==", String.valueOf(position));
                                EpaLog.e("Username== ", customNewsFeedAdapter.getItem(position).getUserName());


                                WebUtils.TYPE = newsFeed_type;
                                WebUtils.ORG = org_id;
                                WebUtils.POSTID = newsFeed_SourceID;
                                WebUtils.TIME_ZONE = time_zone_id;
                                WebUtils.NEWSFEED_ID = newsFeedID;
                            }

                            @Override
                            public void onUserClick(Bundle bundle) {
                            }

                            @Override
                            public void contentClick(View v, int position, String newsFeedId) {

                                CustomNewsFeedDetailFragment newsFeedFragmentDetail = CustomNewsFeedDetailFragment.newInstance(position, customNewsFeedAdapter.getItem(position).getId());
                                start(newsFeedFragmentDetail);

                                Bundle args1 = new Bundle();
                                args1.putString("user_ID", customNewsFeedAdapter.getItem(position).getId());
                                args1.putInt("position", position);
                                newsFeedFragmentDetail.setArguments(args1);

// Utils.saveSharedPrefValue(context,SharedPreferenceHelper.NEWSFEEDID,newsFeedDatas.get(position).getId());                                     EpaLog.e(“NewsFeedID -------”,customNewsFeedAdapter.getItem(position).getId());

                            }
                        });
                        // EpaLog.e(TAG, "Count " + responseBody.length());

                        // store this response for dispalying full screen image
                        FullScreenViewActivity.addResponse_newsfeed(responseBody);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //dismiss progressDialog after parsing json(because it taking time) instead of just after api call.
                try {
                    progressDialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.execute(new NewsFeedRequest(org_id, offSetValue, trackedLikeActivityPostIds, trackedCommentActivityPostIds, trackedUpdateActivityPostIds));
    } else {
        Utils.showAlertDialog(context, "No Internet Connection",
                "You don't have internet connection.", false);
    }
}

1 个答案:

答案 0 :(得分:0)

也许它不是很好的方法,但你可以尝试像首先在背景上删除那个片段

getActivity().getSupportFragmentManager().popBackStack();

getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

然后调用你父级的onresume并加载要加载或刷新的数据。!

((MainActivity) getContext()).onResume();