2016-10-02 5 views
1

私はMainActivityを使用してviewPagerを使用し、ユーザーのスワイプジェスチャーについて2つのfragmentビューをインスタンス化します。フラグメントの1つがサーバーからデータを取得し、そのビューを更新します。ViewPager:ユーザーのスワイプでフラグメントビューを更新する

問題:なしインターネット接続でアプリを起動する

、いくつかのダミーデータとfragment Aの更新が、その後、アプリが起動して、そのフラグメントにスワイプしている間のWi-Fiの切り替え時に更新されません。受信したデータとの断片。

fragmentビューは、インスタンス化された後に更新することができます。次のように

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private SectionsPagerAdapter mSectionsPagerAdapter; 
private ViewPager mViewPager; 

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
} 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return fragment1.newInstance(); 
      case 1: 
       return fragment3.newInstance(); 
      default: 
       return fragment1.newInstance(); 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show 2 total pages. 
     return 2; 
    } 
} 

FragmentAクラス(コードでFragment3)が与えられる。

public class fragment3 extends Fragment implements YourFragmentInterface{ 

    public static ListView mylistView; 
    public static ConfessionsAdapter adapter; 
    public static ArrayList<Confession> confessionList; 

    public static Context context; 

    public static fragment3 newInstance() { 

     fragment3 fragment = new fragment3(); 

     new HttpGetReq().execute("http://example.com"); 

     return fragment; 
    } 

    public fragment3() { 
    } 

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

     context = getActivity(); // Getting context to tell async task about it, so it can SET stuff here. 

     if (container == null) { 
      return null; 
     } 
     View view = inflater.inflate(R.layout.list_fragment, container, false); 
     mylistView = (ListView) view.findViewById(android.R.id.list); 

     new HttpGetReq().execute("http://example.com"); 

     return view; 
    } 
} 

HttpGetReqクラスは、サーバーからデータを取得すること、AsyncTaskでJSONを解析しますメソッド内のフラグメントAのlistを更新し、アダプタをonPostExecute()メソッドのフラグメントAのリストViewに設定します。

答えて

1

最初に、インターネット接続をチェックする方法を作成します。私のために私はUtilsのと呼ばれるクラスを作成して、そのクラスにインターネットは今、インターネット接続ステータスが変更されたときに通知されますBroadcast Receiverを作成

public class Utils { 

    public static boolean isNetworkAvailable(Context context) { 
     try { 
      ConnectivityManager cm = (ConnectivityManager) 
        context.getSystemService(context.CONNECTIVITY_SERVICE); 
      NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 
      // if no network is available networkInfo will be null 
      // otherwise check if we are connected 
      if (networkInfo != null && networkInfo.isConnected()) { 
       return true; 
      } 
      return false; 
     } catch (Exception ex) { 
      return false; 
     } 
    } 

} 

利用可能な場合、trueを返します方法があります。それは、利用可能なインターネットに変更された場合、それはあなたの受信登録し、ここでは放送受信のためのコードであるローカルbroadcast manager

によって

public class NetworkChangedReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (Utils.isNetworkAvailable(context)) 
     { 
      //sending local broadcast 
      Intent i = new Intent("net_connected"); 
      i.putExtra("message", true); 
      LocalBroadcastManager.getInstance(context).sendBroadcast(i); 

     } 
    } 
} 

manifestでのブロードキャストを送信します

<receiver android:name=".NetworkChangedReceiver"> 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
      </intent-filter> 
    </receiver> 

とあなたの活動記録にあなたの地方の放送受信機を登録する場合は、onResumeでそれを行い、のようにこれを登録する必要があります

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

     LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, 
       new IntentFilter("custom-event-name")); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); 
    } 

とフラグメント

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       boolean isConnectTed = intent.getBooleanExtra("net_connected",false); 
       if(isConnectTed){ 
       //if connected then notify to the 
       mSectionsPagerAdapter.notifyDataSetChanged(); 
       } 
      } 
     }; 
に、ローカル放送受信機の機能を書きます
関連する問題