我正在处理一个需要在碎片之间传递数据的项目。所有的数据都来自于我的
以下是我的
SelectFrag extends Fragment:
该应用程序目前将信息从selectFrag传递到Activity,然后再传递到infoFrag。我希望这个ID能够一直传递到子片段。我考虑过在InfoFrag中实现MyCommunicator并将数据传递给子片段,但是未显示的子片段是否会抛出NullPointerException呢?
*注意:selectFrag和InfoFrag在Activity中始终可见。当在selectFrag的GridView中选择新项时,InfoFrag必须刷新。
谢谢您的帮助!
编辑:我尝试使用“通信接口”,但每次都会迷失方向。有人可以简要介绍一下如何做吗?所有子片段是否都可以从selectFrag实现通信器?如果是,那么这样做是正确的吗?我认为InfoFragment应该实现接口并将数据传递给其子元素。但是我不知道如何做到这一点并刷新子元素。另一件事我想做的是将lastItemClicked存储在sharedPreferences中,然后从我的数据库中访问对象。我真的很困惑:/ 再次感谢!
DatabaseHandler class
(SQLiteOpenHelper)。我手写了这篇文章,所以请忽略任何语法错误。以下是我的
Activity
的样子(包含SelectionFragment和InfoFragment):
- Selection Fragment(包含一个
GridView
) - Info Fragment(带有大约
5-6个子碎片
的ViewPager
)- childFragment1
- childFragment2
- childFragment3
- ...
SelectFrag extends Fragment:
MyCommunicator communicator;
GridView myGrid;
onItemSelected(... int position, ...) {
communicator.respond("The id i get from getTag()..");
}
public void setCommunicator(MyCommunicator communicator) {
this.communicator = communicator;
}
public interface MyCommunicator {
public void respond(String id);
}
Activity扩展FragmentActivity并实现SelectFrag.MyCommunicator:
FragmentManager manager;
SelectFrag selectFrag;
InfoFrag infoFrag;
onCreate() {
manager = getSupportFragmentManager();
selectFrag = (SelectFrag) manager.findFragmentById(...);
selectFrag.setCommunicator(this);
}
@Override
public void respond(String id) {
DatabaseHandler db = new DatabaseHandler(this);
try {
CustomObject obj = db.getObj(id);
} finally {
db.close();
}
infoFrag = (InfoFrag) manager.findFragmentById(...);
if(obj != null)
infoFrag.changeObj(id)
setTitle(obj.getName();
}
InfoFrag扩展了Fragment:
ViewPager pager;
Context context;
MyObj obj;
public void changeObj(String id) {
DatabaseHandler db = new DatabaseHandler(context);
this.obj = db.getObj(id);
db.close();
}
protect class MyPagerAdapter extends FragmentPagerAdapter{
public Fragment getItem(int i) {
Fragment frag = null;
switch(i) {
case 0:
frag = new ChildFrag1();
break;
case 1:
frag = new ChildFrag2();
break;
... Goes to case 6 for now.
}
return frag;
}
}
这是我的布局:
select_frag.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectFrag" >
<GridView
android:id="@+id/selectGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="56dp"
android:background="#dd5500"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="10dp"
android:paddingRight="2dp"
android:paddingTop="3dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
</RelativeLayout>
main_activity.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:orientation="vertical" >
<fragment
android:id="@+id/selectFragment"
android:name="my.package.SelectFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3" />
<fragment
android:id="@+id/infoFragment"
android:name="my.package.InfoFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="7" />
</LinearLayout>
info_fragment.xml :
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/my_pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
该应用程序目前将信息从selectFrag传递到Activity,然后再传递到infoFrag。我希望这个ID能够一直传递到子片段。我考虑过在InfoFrag中实现MyCommunicator并将数据传递给子片段,但是未显示的子片段是否会抛出NullPointerException呢?
*注意:selectFrag和InfoFrag在Activity中始终可见。当在selectFrag的GridView中选择新项时,InfoFrag必须刷新。
谢谢您的帮助!
编辑:我尝试使用“通信接口”,但每次都会迷失方向。有人可以简要介绍一下如何做吗?所有子片段是否都可以从selectFrag实现通信器?如果是,那么这样做是正确的吗?我认为InfoFragment应该实现接口并将数据传递给其子元素。但是我不知道如何做到这一点并刷新子元素。另一件事我想做的是将lastItemClicked存储在sharedPreferences中,然后从我的数据库中访问对象。我真的很困惑:/ 再次感谢!