0
我使用ListView顯示CalendarProvider中的事件實例列表。最初,我查詢所有從Today-1月到Today + 1月的實例。當用戶滾動到ListView的頂部或底部時,它應該加載下一個或上一個月。現在我已經實現了OnScrollListener,並檢測了頂部和底部的滾動。但現在我不知道該怎麼做。如何將數據添加到遊標?或者這是不可能的與CursorAdapter?我是否必須創建某種DTO或類似的東西?使用CalendarProvider中的光標在ListView上進行無盡滾動
這裏是我的代碼
public class ScheduleFragment extends Fragment implements AbsListView.OnScrollListener {
//Start and End date for queries
private long mStartDate;
private long mEndDate;
private int mPreviousLastItem = -1;
private int mPreviousFirstitem = -1;
public ScheduleFragment() {
// Required empty public constructor
}
public static ScheduleFragment newInstance() {
ScheduleFragment fragment = new ScheduleFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_schedule, container, false);
setupListView(view);
return view;
}
private void setupListView(View container) {
try {
ListView listView = (ListView) container.findViewById(R.id.fragment_schedule_list);
listView.setOnScrollListener(this);
ContentResolver cr = getActivity().getContentResolver();
//calculate the initial start and end date
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
mStartDate = calendar.getTimeInMillis();
calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, +1);
mEndDate = calendar.getTimeInMillis();
//query the event instances
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, mStartDate);
ContentUris.appendId(builder, mEndDate);
// Submit the query and get a Cursor object back.
Cursor cursor = cr.query(builder.build(), new String[]{BaseColumns._ID, CalendarContract.Instances.TITLE}, null, null, null);
CursorAdapter cursorAdapter = new ScheduleCursorAdapter(getActivity(), cursor);
listView.setAdapter(cursorAdapter);
} catch (Exception e) {
//TODO handle permissions
e.printStackTrace();
}
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (view.getChildCount() > 0 && firstVisibleItem == 0) {
if (mPreviousFirstitem != firstVisibleItem) {
//load mStartDate-1 Month to mStartDate
}
}
final int lastItem = firstVisibleItem + visibleItemCount;
if (view.getChildCount() == 0 || lastItem == totalItemCount) {
if (mPreviousLastItem != lastItem) {
//load mEndDate to mEndDate + 1 Month
}
}
mPreviousLastItem = lastItem;
mPreviousFirstitem = firstVisibleItem;
}
}