2016-10-02 54 views
1

我有我的MainActivity使用viewPager並實例化兩個用戶輕掃手勢的fragment視圖。其中一個片段從服務器獲取一些數據並更新其視圖。ViewPager:刷新用戶划動的片段視圖

問題:

開始,沒有互聯網連接的應用程序,fragment一些虛擬數據,但隨後在當應用程序正在運行,然後滑動到該片段不更新切換無線網絡連接更新片段與收到的數據。

請建議如何在實例化後更新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並在doInBackground()方法中更新片段A的list,然後在其onPostExecute()方法中將適配器設置爲片段A的列表View

回答

1

首先創建一個方法來檢查你的網絡連接。對我來說,我已經創建了一個名爲utils的類,並且在類有方法,該方法將返回true,如果互聯網可

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; 
     } 
    } 

} 

現在創建一個Broadcast Receiver,當網絡連接狀態改變時它會通知你。如果它更改爲可用,那麼網絡就會通過當地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(); 
       } 
      } 
     };