再次启动时ViewPagerAdapter更新片段

时间:2016-04-29 12:59:37

标签: android android-viewpager

我有以下ViewPagerAdapter:

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager());
    adapter.addFrag(new OwnerPendingBookingsFragment(), "Pendientes");
    adapter.addFrag(new OwnerConfirmedBookingsFragment(), "Confirmados");
    adapter.addFrag(new OwnerFinishedBookingsFragment(), "Finalizados");
    viewPager.setAdapter(adapter);
}


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

我使用的其中一个片段是这样的:

public class OwnerConfirmedBookingsFragment extends Fragment {

private View myView;
private ListView booking_list;
private ArrayAdapter adapter;
private SharedPreferences preferences;
private Toast toast;
private ProgressDialog progressDialog;
private BookingList bookings;
private boolean isFirstTime;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (isFirstTime) {
        myView = inflater.inflate(R.layout.owner_confirmed_bookings_layout, container, false);
        isFirstTime = false;
    }

    return myView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    isFirstTime = true;
    //Getting out sharedpreferences
    preferences = getActivity().getSharedPreferences(Config.SHARED_PREF_LOGIN, Context.MODE_PRIVATE);
        new FindBookings().execute("");

}


private void setUpAdapter(){
    booking_list = (ListView) myView.findViewById(R.id.owner_confirmed_bookings);
    getActivity().setTitle("Nuevas solicitudes");
    if(bookings.getBookings().size() == 0){
        booking_list.setVisibility(View.GONE);
        LinearLayout no_vehicles = (LinearLayout) myView.findViewById(R.id.no_confirmed_bookings_layout);
        no_vehicles.setVisibility(View.VISIBLE);
    }else {
        for (int i = 0; i < bookings.getBookings().size(); i++){
            try {
                new setImageTask(i).execute(bookings.getBookings().get(i).getDriver_image()).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        adapter = new OwnerFinishedBookingsAdapter(myView.getContext(), bookings.getBookings());
        booking_list.setAdapter(adapter);
        booking_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int booking_line = bookings.getBookings().get(position).getBooking_line_id();
                Fragment booking_info = new OwnerPendingBookingInfoFragment();
                Bundle args = new Bundle();
                args.putInt("bookingLine", booking_line);
                booking_info.setArguments(args);
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, booking_info);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }
}

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

    @Override
    protected void onPreExecute() {
        toast = Toast.makeText(getActivity().getApplicationContext(), "", Toast.LENGTH_LONG);
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(getActivity().getString(R.string.finding_bookings));
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(String s){
        if(bookings.getError() != 0){
            if(bookings.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();
        }
        progressDialog.dismiss();
    }

    @Override
    protected String doInBackground(String... params) {
        bookings = DBConnect.getPendingBookingList(preferences.getInt(Config.IDUSER, 0), token);
        return "done";
    }
}

我第一次使用它时,所有碎片都会毫无问题地加载。但是,当我去另一个活动,然后返回它不会更新数据。我调试它并且onCreate和onCreateView没有触发? 我去另一个活动时试图删除片段,但没有任何对我有用。

0 个答案:

没有答案