更新 SlidingTabs 中的片段
Update Fragments within SlidingTabs
根据 this Tutorial,我实现了滑动标签。
ViewPager
持有 3 Fragments
。在第一个 Fragment
中,我将项目添加到 sqlite table。在第二个 Fragment
中,这些 table 项列在 ListFragment
.
中
如何实现触发第二个视图的更新以查看这些新添加的项目?我已经调用了第二个 Fragment
的刷新函数,它在第二个 Fragment
本身内运行良好,但如果我添加项目则不行。
/* ListFragment */
@Override
public void refreshView() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
/* ListAdapter */
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mItems.clear();
mItems = mItemDAO.getAllEntries();
}
getAllEntries()
工作正常。适配器也应该工作正常,因为当我使用旧的 ActionBar
选项卡时就是这种情况。添加一个项目后,我滑动到第二个 Fragment
,我可以看到新添加的项目。
更新
获取所有条目后调用super.notifyDataSetChanged()
。
@Override
public void notifyDataSetChanged() {
mItems.clear();
mItems = mItemDAO.getAllEntries();
// Update the ListAdapter now that you have the new Items
super.notifyDataSetChanged();
}
这是可行的,因为您告诉适配器在获取新条目之前进行更新,它必须在您获取项目之后完成。
此外,对于片段:
无论您的实施细节如何,根据其他片段的数据更新片段的流程如下:
FragmentA -> 通过接口通知Parent Activity -> 更新FragmentB
首先,用户在片段 A 中添加一个项目,成功完成此项目添加到 SQLite 数据库后,使用接口通知 Parent Activity 更新其片段 B。
类似于 FragmentA
创建:
public interface IDataBaseChanged{
void databaseUpdated(boolean updated);
}
ParentActivity必须implements IDataBaseChanged
在FragmentA
中创建一个可以回调到parent
的局部变量
private IDataBaseChanged mCallback;
public void addItemToDB(Object itemToAdd){
// ... perform the operation which adds the Item then if this item
// is actually added successfully meaning you get the long representation
// of the newly added row id and its not -1, perform a callback
// Callback method to tell the Parent Activity data was added
mCallback.databaseUpdated(true);
}
确保重写 FragmentA 中的 onAttach 并将接口附加到 Activity:
@Override
public void onAttach (Activity activity){
try{
mCallback = (IDataBaseChanged) activity;
}catch(ClassCastException ex){
Log.e("Interface", "Failed to implement interface in parent", ex);
}
}
并在 Parent Activity:
// Initialize this in your ViewPager's Adapter...
private FragmentB fragmentB;
@Override
void databaseUpdated(boolen updated){
if(updated && fragmentB != null){
// call a public method in fragment B to requery the DB
fragmentB.updateUI();
}
}
FragmentB
中的方法:
public void updateUI(){
// ... Perform the work to requery DB and display its results
// in the UI
}
根据 this Tutorial,我实现了滑动标签。
ViewPager
持有 3 Fragments
。在第一个 Fragment
中,我将项目添加到 sqlite table。在第二个 Fragment
中,这些 table 项列在 ListFragment
.
如何实现触发第二个视图的更新以查看这些新添加的项目?我已经调用了第二个 Fragment
的刷新函数,它在第二个 Fragment
本身内运行良好,但如果我添加项目则不行。
/* ListFragment */
@Override
public void refreshView() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
/* ListAdapter */
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mItems.clear();
mItems = mItemDAO.getAllEntries();
}
getAllEntries()
工作正常。适配器也应该工作正常,因为当我使用旧的 ActionBar
选项卡时就是这种情况。添加一个项目后,我滑动到第二个 Fragment
,我可以看到新添加的项目。
更新
获取所有条目后调用super.notifyDataSetChanged()
。
@Override
public void notifyDataSetChanged() {
mItems.clear();
mItems = mItemDAO.getAllEntries();
// Update the ListAdapter now that you have the new Items
super.notifyDataSetChanged();
}
这是可行的,因为您告诉适配器在获取新条目之前进行更新,它必须在您获取项目之后完成。
此外,对于片段:
无论您的实施细节如何,根据其他片段的数据更新片段的流程如下:
FragmentA -> 通过接口通知Parent Activity -> 更新FragmentB
首先,用户在片段 A 中添加一个项目,成功完成此项目添加到 SQLite 数据库后,使用接口通知 Parent Activity 更新其片段 B。
类似于 FragmentA
创建:
public interface IDataBaseChanged{
void databaseUpdated(boolean updated);
}
ParentActivity必须implements IDataBaseChanged
在FragmentA
中创建一个可以回调到parent
private IDataBaseChanged mCallback;
public void addItemToDB(Object itemToAdd){
// ... perform the operation which adds the Item then if this item
// is actually added successfully meaning you get the long representation
// of the newly added row id and its not -1, perform a callback
// Callback method to tell the Parent Activity data was added
mCallback.databaseUpdated(true);
}
确保重写 FragmentA 中的 onAttach 并将接口附加到 Activity:
@Override
public void onAttach (Activity activity){
try{
mCallback = (IDataBaseChanged) activity;
}catch(ClassCastException ex){
Log.e("Interface", "Failed to implement interface in parent", ex);
}
}
并在 Parent Activity:
// Initialize this in your ViewPager's Adapter...
private FragmentB fragmentB;
@Override
void databaseUpdated(boolen updated){
if(updated && fragmentB != null){
// call a public method in fragment B to requery the DB
fragmentB.updateUI();
}
}
FragmentB
中的方法:
public void updateUI(){
// ... Perform the work to requery DB and display its results
// in the UI
}