Android:在活动中重新加载片段

时间:2016-10-15 23:38:51

标签: android android-fragments refresh android-listfragment

我有这个活动:

public class TabLayoutScreenActivity extends BaseActivity {

    Toolbar mToolbar;
    Drawer drawer;

    private static final String TAG = "TabLayoutScreenActivity";

    private TabLayout tabLayout;
    private Bundle bundle;

    Switch mySwitch;
    TextView OpenClose;

    private FloatingActionButton action_find_location;

    boolean userLoggedIn=false,chefLoggedIn=false;
    String selectedCity, selectedLocality,date;

    ViewPager viewPager;
    TextView toolbarCityText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_screen);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        viewPager = (ViewPager) findViewById(R.id.pager);
        mySwitch = (Switch) findViewById(R.id.mySwitch);
        OpenClose = (TextView) findViewById(R.id.OpenClose);

        initToolbar();
        initDrawer();

    }

    private String convertDate(String date)
    {

        return date;
    }

    private void initToolbar()
    {

    }

    private void initDrawer()
    {

    }

    private void signOutUser()
    {

    }

    private void signOutChef()
    {

    }

    public void selectDate(View view) {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        DatePickerDialog datePicker = new DatePickerDialog(this,
                R.style.AppTheme, datePickerListener,
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        datePicker.setCancelable(false);
        datePicker.setTitle("Select the date");

        datePicker.show();
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            String year1 = String.valueOf(selectedYear);
            String month = String.valueOf(selectedMonth + 1);
            String month1 = new DateFormatSymbols().getMonths()[selectedMonth];
            String day = String.valueOf(selectedDay);
            if (Integer.parseInt(day) < 10) {
                day = "0" + day;
            }
            if (Integer.parseInt(month) < 10) {
                month = "0" + month;
            }
            toolbarCityText.setText(day + " " + month1 + " " + year1);
            String date = year1 + "-" + month + "-" + day;
            PrefernceHelper.putString(TabLayoutScreenActivity.this, Commons.Constants.DATE, date);

        }
    };

    public void changeViewPager(int position)
    {
        viewPager.setCurrentItem(position,true);
    }

    public class SectionPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ChefFragment();
                case 1:
                    return new OrderFragment();
                case 2:
                    return new BasketFragment();
                default:
                    return new ChefFragment();
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Pending";
                case 1:
                    return "Accepted";
                case 2:
                    return "Rejected";
                case 3:
                    return "More";
                default:
                    return "Chef";
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (bundle == null) {
            FoodleBeeBasketDao foodleBeebasket = new FoodleBeeBasketDao(
                    this.getApplicationContext(), DbHelper.getInstance(
                    this.getApplicationContext(),
                    DbConfiguration.getInstance(this.getApplicationContext())
            ).getSQLiteDatabase()
            );

            foodleBeebasket.deleteAll();
            TabLayoutScreenActivity.this.finish();
        }

        TabLayoutScreenActivity.this.finish();
    }

}

称之为片段:

public class ChefFragment extends Fragment implements AdapterView.OnItemClickListener {

    // static ChefFragment fragment = null;
    ListView lstOrders;
    ChefsAdapter adapter;
    BeeGif viewGif;
    // Bundle bundle;
    ArrayList<Chefs> chefList;
    private String imageUrl;
    private FloatingActionButton action_find_location;
    ArrayList<ChefOrderList> orderLists;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lstOrders = (ListView) view.findViewById(lstFood);
        lstOrders.setOnItemClickListener(this);

        RelativeLayout viewLayout = (RelativeLayout) view.findViewById(R.id.beeGif);
        viewGif = (BeeGif) viewLayout.findViewById(R.id.view);

        System.out.println(PrefernceHelper.getString(getActivity(), Commons.Constants.DATE));

        if (NetworkHelper.isOnline(getActivity())) {
            callServiceForMenuStatusDetails(PrefernceHelper.getString(getActivity(), Commons.Constants.DATE));
        } else {
            NetworkHelper.noNetworkToast(getActivity());
        }

    }

    private void callServiceForMenuStatusDetails(String date) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        System.out.println("Test");
        Intent intent = new Intent();
        intent.putExtra("orderID", orderLists.get(position).getOrderId());
        intent.setClass(getActivity(), OrderDetailsActivity.class);
        startActivity(intent);

    }
}

方法SelectDate调用日期选择器对话框。根据此日期,在我的数据库中执行搜索。结果列在上面的片段中。每次我选择一个新约会时,我都需要刷新片段。

1 个答案:

答案 0 :(得分:0)

我想出了如何解决我的问题。我在选择一个新日期后立即放了这段代码:

{{1}}

“pager”是活动布局中加载Fragment的空间。它完美无缺!