2016-09-14 6 views
0

2つのタブフラグメントを持つビューページを持つタブレイアウトを持つコンテナとしてフラグメントがあります。メインコンテナのフラグメントの中には、カスタムボタンが付いたツールバーがあり、onclickingは、Webサービスを打つことでタブフラグメントのコンテンツを更新します。Android:サーバから更新されたコンテンツでフラグメントをリロードできません

これは、コンテナフラグメントのコードです。

public class NotificationsFragment extends Fragment { 

TabLayout tabLayout = null; 
ViewPager viewPagerNotifications = null; 

public NotificationsFragment() 
{ 
    // Required empty public constructor 
} 

public static NotificationsFragment newInstance() { 

    Bundle args = new Bundle(); 

    NotificationsFragment fragment = new NotificationsFragment(); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_notifications, null); 

    final Button mButton_right = (Button) getActivity().findViewById(R.id.Button_right); 
    mButton_right.setVisibility(View.VISIBLE); 

    final Button mButton_alerts = (Button) getActivity().findViewById(R.id.Button_alerts); 
    mButton_alerts.setVisibility(View.GONE); 

    tabLayout = (TabLayout) rootView.findViewById(R.id.TabLayout_notifications); 
    tabLayout.addTab(tabLayout.newTab().setText("Symptoms")); 
    tabLayout.addTab(tabLayout.newTab().setText("Diagnosis")); 

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
    tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.textColorPrimary)); 
    tabLayout.setSelectedTabIndicatorHeight(8); 
    tabLayout.setTabTextColors(ContextCompat.getColor(getActivity(),R.color.textColorPrimary), 
      getResources().getColor(R.color.textColorPrimary)); 

    ViewCompat.setElevation(tabLayout, 5f); 

    viewPagerNotifications = (ViewPager) rootView.findViewById(R.id.viewPagernotifications); 


    final NotificationPagerAdapter pagerAdapter = new NotificationPagerAdapter(getFragmentManager(),tabLayout.getTabCount()); 
    viewPagerNotifications.setAdapter(pagerAdapter); 

    viewPagerNotifications.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    viewPagerNotifications.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 

      Log.e("notif","onpageselected"+position); 


     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPagerNotifications.setCurrentItem(tab.getPosition()); 

      switch (tab.getPosition()) 
      { 
       case 0: 
        mButton_right.setVisibility(View.VISIBLE); 
        mButton_alerts.setVisibility(View.GONE); 

        /* LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getActivity()); 
        Intent i = new Intent("TAG_REFRESH"); 
        lbm.sendBroadcast(i);*/ 

        break; 

       case 1: 
        mButton_right.setVisibility(View.GONE); 
        mButton_alerts.setVisibility(View.VISIBLE); 

        break; 

       default: 
        break; 
      } 



     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 




    // Inflate the layout for this fragment 
    return rootView; 
} 


@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 


@Override 
public void onDetach() { 
    super.onDetach(); 
} 

}

これは、ポケットベルアダプタクラスのコード

public class NotificationPagerAdapter extends FragmentStatePagerAdapter { 

final int Illness = 1; 
final int Symptoms = 0; 

int numOfTabs; 

SymptomsTabFragment symptomsTabFragment; 
IllnessTabFragment illnessTabFragment; 


public NotificationPagerAdapter(FragmentManager fm,int numOfTabs) { 
    super(fm); 
    this.numOfTabs=numOfTabs; 
} 

@Override 
public Fragment getItem(int position) { 
    switch (position) 
    { 
     case Symptoms: 
      symptomsTabFragment = new SymptomsTabFragment(); 
      return symptomsTabFragment; 

     case Illness: 
      illnessTabFragment= new IllnessTabFragment(); 
      return illnessTabFragment; 


    } 
    return null; 
} 


    @Override 
    public int getCount() { 
    return numOfTabs; 
    } 
} 

これは、ここに直面イム問題がある第一タブ断片のコード

public class SymptomsTabFragment extends Fragment { 

@Bind(R.id.recycler_view_notificationsSymptoms) 
RecyclerView mRecyclerView_notificationSymptoms; 


NetworkStatus mNetworkStatus = null; 

static AlertDialog mShowDialog = null; 

Button mButton_right; 

ArrayList<NotificationSymptomdata> notificationSymptoms = null; 
ArrayList<NotificationSymptomdata> notificationSymptomid = null; 
ArrayList<String> notificationSettingID = null; 
ArrayList<NotificationSymptomdata> isNotification = null; 


public SymptomsTabFragment() { 
    // Required empty public constructor 
} 


public static SymptomsTabFragment newInstance() { 
    SymptomsTabFragment fragment = new SymptomsTabFragment(); 
    Bundle args = new Bundle(); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Log.e("stb", "oncreate"); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_symptoms_tab, container, false); 
    ButterKnife.bind(this, rootView); 

    mButton_right = (Button) getActivity().findViewById(R.id.Button_right); 


    Log.e("stb", "oncreateview"); 


    Toolbar.LayoutParams params = new Toolbar.LayoutParams(marginInDp(80), marginInDp(50)); 
    params.gravity = Gravity.RIGHT; 
    params.setMargins(0, 0, marginInDp(20), 0); 
    mButton_right.setLayoutParams(params); 
    mButton_right.setBackgroundResource(0); 
    mButton_right.setTag(1); 
    mButton_right.setVisibility(View.VISIBLE); 
    mButton_right.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      final int status = (Integer) v.getTag(); 
      if (status == 1) { 
       mButton_right.setText("Select All"); 
       v.setTag(0); 

      } else { 

       mButton_right.setText("DeSelect All"); 
       v.setTag(1); 
      } 

      if (mButton_right.getText().toString().equalsIgnoreCase("Select All")) { 


       ArrayList<UpdateNotificationRequestData> Updatenoti; 

       Log.e("stb", "onclick"); 

       UpdateNotificationsRequestModel requestModel = new UpdateNotificationsRequestModel(); 
       requestModel.setUserID(AppPreferences.readString(getActivity(), AppPreferenceNames.sUserid, "")); 
       requestModel.setAppVersion(CommonUtils.APP_VERSION); 
       requestModel.setDeviceInfo(CommonUtils.DeviceInfo); 
       requestModel.setDeviceTypeID(CommonUtils.DEVICE_TYPE_ID); 

       Updatenoti = new ArrayList<UpdateNotificationRequestData>(); 
       UpdateNotificationRequestData requestData; 
       for (int i = 0; i < notificationSymptoms.size(); i++) { 
        requestData = new UpdateNotificationRequestData(); 
        requestData.setIsNotification("1"); 
        requestData.setNotificationSettingID(notificationSymptoms.get(i).getNotificationSettingID()); 
        Updatenoti.add(requestData); 
       } 

       requestModel.setUpdateNotification(Updatenoti); 

       if (mNetworkStatus.isNetWorkAvailable(getActivity()) == true) { 
        update_notifications(requestModel); 

       } else { 
        CommonUtils.showAlertDialog(getActivity(), "No Network Available. Please connect to network"); 
       } 

      } else if (mButton_right.getText().toString().equalsIgnoreCase("DeSelect All")) { 

       ArrayList<UpdateNotificationRequestData> Updatenoti; 

       Log.e("stb", "onclick"); 

       UpdateNotificationsRequestModel requestModel = new UpdateNotificationsRequestModel(); 
       requestModel.setUserID(AppPreferences.readString(getActivity(), AppPreferenceNames.sUserid, "")); 
       requestModel.setAppVersion(CommonUtils.APP_VERSION); 
       requestModel.setDeviceInfo(CommonUtils.DeviceInfo); 
       requestModel.setDeviceTypeID(CommonUtils.DEVICE_TYPE_ID); 

       Updatenoti = new ArrayList<UpdateNotificationRequestData>(); 
       UpdateNotificationRequestData requestData; 
       for (int i = 0; i < notificationSymptoms.size(); i++) { 
        requestData = new UpdateNotificationRequestData(); 
        requestData.setIsNotification("0"); 
        requestData.setNotificationSettingID(notificationSymptoms.get(i).getNotificationSettingID()); 
        Updatenoti.add(requestData); 
       } 

       requestModel.setUpdateNotification(Updatenoti); 

       if (mNetworkStatus.isNetWorkAvailable(getActivity()) == true) { 
        update_notifications(requestModel); 
       } else { 
        CommonUtils.showAlertDialog(getActivity(), "No Network Available. Please connect to network"); 
       } 


      } 


     } 
    }); 


    notificationSymptoms = new ArrayList<>(); 
    notificationSymptomid = new ArrayList<>(); 
    notificationSettingID = new ArrayList<>(); 
    isNotification = new ArrayList<>(); 

    mNetworkStatus = new NetworkStatus(getActivity()); 


    // Inflate the layout for this fragment 
    return rootView; 
} 


/** 
* Used to set margin value programmatically 
* 
* @param sizeInDP 
* @return 
*/ 
private int marginInDp(int sizeInDP) { 
    int marginInDp = (int) TypedValue.applyDimension(
      TypedValue.COMPLEX_UNIT_DIP, sizeInDP, getResources() 
        .getDisplayMetrics()); 
    return marginInDp; 
} 


/** 
* Update the notification settings 
*/ 
public void update_notifications(UpdateNotificationsRequestModel object) { 
    /** 
    * Start the progress Bar. 
    */ 
    CommonUtils.show_progressbar(getActivity()); 

    /** 
    * call api 
    */ 

    Call<UpdateNotificationsResponseModel> responsecall = VirusApplication.getRestClient().getAPIService().updateNotifications(object); 
    responsecall.enqueue(new Callback<UpdateNotificationsResponseModel>() { 
     @Override 
     public void onResponse(Response<UpdateNotificationsResponseModel> response, Retrofit retrofit) { 

      /** 
      * Stop the progress Bar 
      */ 
      CommonUtils.stop_progressbar(); 

      if (response.isSuccess()) { 
       //Server Success 
       UpdateNotificationsResponseModel responseModel = response.body(); 
       if (responseModel.getErrorCode().equalsIgnoreCase("0")) { 
        //Data Success 
        Log.e("nf", "data success"); 

        CommonUtils.showAlertDialog(getActivity(), responseModel.getMessage()); 

        // getNotificationSettings(); 


       } else { 
        CommonUtils.showAlertDialog(getActivity(), responseModel.getMessage()); 

        // getNotificationSettings(); 


       } 
      } else { 

       CommonUtils.showAlertDialog(getActivity(), "Server Error"); 

       // getNotificationSettings(); 

      } 

     } 

     @Override 
     public void onFailure(Throwable t) { 

      /** 
      * Stop the progress Bar 
      */ 
      CommonUtils.stop_progressbar(); 


     } 
    }); 


} 

@Override 
public void onResume() { 
    super.onResume(); 

    Log.e("stb", "onresume"); 

    /* r = new MyReceiver(); 
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(r, 
      new IntentFilter("TAG_REFRESH")); 
     */ 
     /** 
     * Downloads mychildren details. 
    */ 
     if (mNetworkStatus.isNetWorkAvailable(getActivity()) == true) { 
      getNotificationSettings(); 
     } else { 
     CommonUtils.showAlertDialog(getActivity(),      getString(R.string.network_unavailable)); 
    } 




} 


/** 
* Get Notification Settings 
*/ 
public void getNotificationSettings() { 

    //hit the getnotifications API to fetch the notification details 

    CommonUtils.show_progressbar(getActivity()); 

    /** 
    * Calls WebAPI 
    */ 

    Call<NotificationsModel> notificationsModelCall = VirusApplication.getRestClient().getAPIService().notifications(getNotificationsrequest()); 
    notificationsModelCall.enqueue(new Callback<NotificationsModel>() { 
     @Override 
     public void onResponse(Response<NotificationsModel> response, Retrofit retrofit) { 

      /** 
      * Stops the Progresss bar 
      */ 
      CommonUtils.stop_progressbar(); 

      if (response.isSuccess()) {//Server Success 

       NotificationsModel notificationsModel = response.body(); 
       if (notificationsModel.getErrorCode().equalsIgnoreCase("0")) {// Data Success 

        int i = 1; 
        for (NotificationSymptomdata nsymptomsData : notificationsModel.getNotificationSymptomdata()) { 
         notificationSymptoms.add(nsymptomsData); 
         // aSymptomsID.add(symptomsData.getSymptomID()); 

         /* if (edit_child_symptoms_id == symptomsData.getSymptomID()) { 
          illness_selection_position = i; 
         }*/ 

         i++; 
        } 


        Log.e("noti", "Symptoms ArraySize-->" + notificationSymptoms.size()); 

        if (notificationSymptoms.size() != 0) { 
         NotificationsAdapter notificationsAdapter = new NotificationsAdapter(getActivity(), notificationSymptoms); 
         mRecyclerView_notificationSymptoms.setAdapter(notificationsAdapter); 
         notificationsAdapter.notifyDataSetChanged(); 
         LinearLayoutManager SymptomsLayoutManager = new LinearLayoutManager(getActivity()); 
         mRecyclerView_notificationSymptoms.setLayoutManager(SymptomsLayoutManager); 
        } 

        for(int j=0;j<notificationSymptoms.size();j++) 
        { 
         if (notificationSymptoms.get(j).getIsNotification().equalsIgnoreCase("true")) 
         { 
          mButton_right.setText("DeSelect All"); 
         } 
         else 
         if(notificationSymptoms.get(j).getIsNotification().equalsIgnoreCase("false")) 
         { 
          mButton_right.setText("Select All"); 
         } 
         else 
         if (notificationSymptoms.get(j).getIsNotification().equalsIgnoreCase("true") || (notificationSymptoms.get(j).getIsNotification().equalsIgnoreCase("false"))) 
         { 
          Log.e("ins","coming here"); 

          mButton_right.setText("Select All"); 
         } 
        } 


       } else { // Data Failure 
    //      CommonUtils.makeToast(AddLogActivity.this,  illnessCategory.getMessage()); 
        /** 
        * Stops the Progresss bar 
        */ 
        CommonUtils.stop_progressbar(); 
        Log.e("noti", "data failure"); 
       } 
      } else {// Server Failure 
//     CommonUtils.makeToast(AddLogActivity.this, "Server Error"); 
       /** 
       * Stops the Progresss bar 
       */ 
       CommonUtils.stop_progressbar(); 
       Log.e("noti", "server failure"); 
      } 
     } 


     @Override 
     public void onFailure(Throwable t) { 

      /** 
      * Stops the Progresss bar 
      */ 
      CommonUtils.stop_progressbar(); 
      Log.e("noti", "retrofit failure"); 

     } 
    }); 


} 

/** 
* Request values to Get notifications. 
* 
* @return map object 
*/ 
public Map<String, Object> getNotificationsrequest() { 

    Map<String, Object> getChildrenValues = new HashMap<>(); 
    getChildrenValues.put("appVersion", CommonUtils.APP_VERSION); 
    getChildrenValues.put("deviceTypeID", CommonUtils.DEVICE_TYPE_ID); 
    getChildrenValues.put("deviceInfo", CommonUtils.DeviceInfo); 
    getChildrenValues.put("userID", AppPreferences.readString(getActivity(), AppPreferenceNames.sUserid, "")); 

    return getChildrenValues; 

    } 
} 

ですボタンのテキストに基づいてmButton_rightをクリックすると、「すべて選択」または「すべて選択解除」が更新されます。私の断片の内容それぞれのボタンの選択に関するWebサービスへの呼び出しは、タブ内で正しく行われています。

しかし、更新後に新しいデータを受信するたびに、新しい受信データでタブフラグメントを更新することができません。 更新されたデータを取得し、ボタンをクリックしてタブフラグメントを更新する方法と場所はどこですか?助けてください!

答えて

0

Fragmentのデータを更新する場合は、Fragmentを変更するたびに常に呼び出される関数を作成し、その中のデータを最新表示できます。詳細は

// In activity 
final Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.frame_container); 
     if (currentFragment instanceof MainTabFragment) { 
       ((MainTabFragment) currentFragment).doRefresh(); 
     } 
関連する問題