使用viewpager

时间:2017-02-15 00:58:14

标签: android android-fragments android-viewpager

我正在构建一个应用程序,将用户输入的费用存储到SQLite数据库中,并将它们显示在RecyclerView中。我的应用目前有一个带3个标签的视图寻呼机。每个选项卡都会切换到一个新片段,该片段从SQLite数据库中提取来自不同时间段的费用,特别是今天,本周和所有显示在RecyclerView中的费用。在每个列表中,我可以长按一个项目并将其删除,这将从SQLite数据库和当前的RecyclerView中删除它并更新列表。由于一个列表中的项目可能在其他列表中,我想实现一种方法,我可以检查其他两个片段中的列表,其中任何项目都被删除,并从那里删除它很好,但我有难以找出最佳方法。

现在我可以轻松地让视图寻呼机重新创建每个片段,这些片段将再次调用数据库并创建全新的列表,但这样效率很低。有没有人对如何实现这一点有任何想法?以下是我目前的代码:

ExpensePagerAdapter

class ExpensePagerAdapter extends FragmentPagerAdapter {
    private String[] tabTitles = {getString(R.string.label_today),
            getString(R.string.label_week), getString(R.string.label_all)};


    ExpensePagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {
        return ExpenseListFragment.newInstance(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

}

ExpenseListFragment

public class ExpenseListFragment extends Fragment{
private static final String LOG_TAG = "ExpenseListFragment";
private static final String ARG_TYPE = "viewType";
private RecyclerView rv;
private List<Expense> dayViewExpenses;
private List<LocalDate> weekViewDay;
private List<List<Expense>> weekViewExpenses;
private List<Expense> allExpenses;

public static ExpenseListFragment newInstance(int viewType){
    Bundle bundle = new Bundle();
    bundle.putInt(ARG_TYPE, viewType);
    ExpenseListFragment f = new ExpenseListFragment();
    f.setArguments(bundle);
    return f;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.d(LOG_TAG, "onCreateView");
    View v = inflater.inflate(R.layout.fragment_list, container, false);
    rv = (RecyclerView) v.findViewById(R.id.rv_list);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));
    int viewType = getArguments().getInt(ARG_TYPE);
    switch(viewType) {
        case 0:
            setupDayView();
            break;
        case 1:
            setupWeekView();
            break;
        case 2:
            setUpAllView();
            break;
        default:
    }
    return v;
}

private void setupDayView(){
    dayViewExpenses = new ArrayList<>();
    LocalDate currentDate = new LocalDate();
    for (Expense e : ExpenseDb.getInstance(getActivity()).getAllExpenses()) {
        if (currentDate.getMonthOfYear() == e.getMonth()
                && currentDate.getDayOfMonth() == e.getDay() && currentDate.getYear() == e.getYear()) {
            dayViewExpenses.add(e);
        }
    }
    rv.setAdapter(new ExpenseAdapter(getActivity(), dayViewExpenses));
}

private void setupWeekView(){
    weekViewDay = new ArrayList<>();
    weekViewExpenses = new ArrayList<>();
    LocalDate weekStart = new LocalDate().withDayOfWeek(DateTimeConstants.MONDAY);
    for (int i = 0; i < 7; i++) {
        LocalDate dayOfWeek = weekStart.plusDays(i);
        weekViewDay.add(dayOfWeek);
        List<Expense> expensesForDay = new ArrayList<>();
        for (Expense expense: ExpenseDb.getInstance(getActivity()).getAllExpenses()) {
            if (expense.getDay() == dayOfWeek.getDayOfMonth() && expense.getMonth() == dayOfWeek.getMonthOfYear()
                    && expense.getYear() == dayOfWeek.getYear()) {
                expensesForDay.add(expense);
            }
        }
        weekViewExpenses.add(expensesForDay);
    }
    rv.setAdapter(new WeekViewAdapter(getActivity(), weekViewDay, weekViewExpenses));
}

private void setUpAllView(){
    allExpenses = ExpenseDb.getInstance(getActivity()).getAllExpenses();
    rv.setAdapter(new ExpenseAdapter(getActivity(), allExpenses));
}

}

任何帮助都会很棒,因为我一直在努力找到理想的方法来实现这一目标。

1 个答案:

答案 0 :(得分:1)

您可以使用事件总线库(http://square.github.io/otto/)来解决此问题。您无法在ExpenseListFragments上附加总线并同时将对象发送到该片段。重新设置适配器对于设备来说太昂贵了。