2016-07-12 99 views
0

子片段我有下面的代碼片段:將數據傳遞從父母到ViewPagerAdapter

public class VehicleEditFragment extends Fragment { 

    private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    private SharedPreferences preferences; 
    private View myView; 
    private ViewPagerAdapter adapter; 
    private Vehicle vehicle; 
    private int vehicle_id; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView = inflater.inflate(R.layout.activity_scrollable_tabs, container, false); 
     preferences = getActivity().getSharedPreferences(Config.SHARED_PREF_LOGIN, Context.MODE_PRIVATE); 
     Bundle args = this.getArguments(); 
     vehicle_id = args.getInt("vehicle_id"); 
     viewPager = (ViewPager) myView.findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) myView.findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
     return myView; 
    } 

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

    } 


    private void setupViewPager(ViewPager viewPager) { 
     getActivity().setTitle("Vehiculo"); 
     adapter = new ViewPagerAdapter(getChildFragmentManager()); 
     adapter.addFrag(new VehicleBasicInfoFragment(), "Datos básicos"); 
     adapter.addFrag(new VehicleEquipmentServicesFragment(), "Equipamiento y Servicios"); 
     adapter.addFrag(new VehicleExplanationFragment(), "Descripción"); 
     adapter.addFrag(new VehicleLocationFragment(), "Localización"); 
     adapter.addFrag(new VehicleImagesFragment(), "Fotos"); 
     adapter.addFrag(new VehicleRatesFragment(), "Tarifas"); 
     adapter.addFrag(new VehicleDisponibilityFragment(), "Disponibilidad"); 
     viewPager.setAdapter(adapter); 
    } 



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

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public android.support.v4.app.Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFrag(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 

    private class FindVehicleInfo extends AsyncTask<String, Void, String> { 
     String token = preferences.getString(Config.TOKEN, ""); 

     @Override 
     protected void onPreExecute() { 
      //swipeRefreshLayout.setRefreshing(true); 
      //toast = Toast.makeText(getActivity().getApplicationContext(), "", Toast.LENGTH_LONG); 
     } 

     @Override 
     protected void onPostExecute(String s){ 
      if(vehicle.getError() != 0){ 
       if(vehicle.getError() == Config.JWT_EXPIRED){ 
        //logout(); 
        Toast.makeText(getActivity().getApplicationContext(), 
          getActivity().getString(R.string.session_expired), 
          Toast.LENGTH_LONG).show(); 
       }else { 
        //toast.setText(getActivity().getString(R.string.find_pendings_errror)); 
        // toast.show(); 
       } 
      }else { 
       //setUpAdapter(); 
      } 
      //swipeRefreshLayout.setRefreshing(false); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      vehicle = DBConnect.GetVehicleInfo(token, vehicle_id); 
      return "done"; 
     } 
    } 
} 

我需要的是通過車輛類的信息給我的所有子片段。完美的方法是將父母與每個孩子聯繫起來,並從父母那裏獲取車輛信息給孩子。那可能嗎?或者有什麼其他方式將信息傳遞給每個孩子?

回答

1

所以,假設Vehicle類是你寫的,那麼我會建議讓這個類實現Parcelable接口。

然後,由於對象是Parcelable,則可以使用一個新的實例方法傳入一個片段:

public static VehicleBasicInfoFragment NewInstance(Vehicle vehicle) { 
    Bundle args = new Bundle(); 
    args.putParcelable("vehicleKey", vehicle); 

    VehicleBasicInfoFragment fragment = new VehicleBasicInfoFragment(); 
    fragment.setArguments(args); 

    return fragment; 
} 

然後,您可以從您的onCreate方法檢索信息:

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

    Vehicle vehicle = getArguments.getExtras().getParcelable("vehicleKey"); 
} 

一旦你已經更新你的孩子片段以擁有自己的NewInstance方法,你可以像這樣更新你的ViewHolder方法:

adapter.addFrag(VehicleBasicInfoFragment.NewInstance(vehicleObject), "Datos básicos"); 
相關問題