My Singleton Class:这是我在官方文件中找到的Singleton课程。
public class Singleton {
private static Singleton uniqInstance;
private Singleton() {
}
public static Singleton getInstance() {
if (uniqInstance == null) {
{
if (uniqInstance == null)
uniqInstance = new Singleton();
}
}
return uniqInstance;
}
}
我的第一个片段:这是我的第一个片段,我在这里添加具有对象类名称的ArrayList项目。
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState){
View mView = inflater.inflate(R.layout.fragment_first, container,false);
RecyclerView rvFirst = mView.findViewById(R.id.rv_first_layout);
rvFirst.setHasFixedSize(true);
rvFirst.setLayoutManager(new LinearLayoutManager(mView.getContext()));
mArrayList = new ArrayList<Names>();
mArrayList.add(new Names("Hello"));
mArrayList.add(new Names("This"));
mArrayList.add(new Names("Is"));
mArrayList.add(new Names("Android"));
// HERE I WANT TO SEND ARRAY-LIST DATA TO THE SECOND FRAGMENT
mAdapter = new NameAdapter(mArrayList);
rvFirst.setAdapter(mAdapter);
return mView;
}
我的第二个片段:这是我的第二个片段,在这里我试图从First Fragment获取ArrayList项目。
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState){
View mView = inflater.inflate(R.layout.fragment_second, container,false);
rvSecond = (RecyclerView) mView.findViewById(R.id.rv_second_layout);
rvSecond.setHasFixedSize(true);
rvSecond.setLayoutManager(new LinearLayoutManager(getContext()));
mArrayList = new ArrayList<Names>();
// HERE I WANT TO GET ARRAY-LIST FROM FIRST FRAGMENT
mAdapter = new NameAdapter(mArrayList);
rvSecond.setAdapter(mAdapter);
return mView;
}
答案 0 :(得分:2)
为什么要使用singleton,如果你的两个片段都连接到同一个活动,那么你可以这样做,
首先在你的acitivity class中创建一个公共列表变量
然后通过像
这样的上下文从片段类中引用它((YourActivityClass)getActivity()).myArrayList.get(i);
答案 1 :(得分:2)
首先在单身类中设置arraylist,如下所示
你的单身人士课应该是这样的: -
if (Yii::$app->request->post('Submit') == 'insertdata') {
在列表中添加值后的第一个片段中: -
<?=Html::a('Insert', ['view', 'id' => $model->id], [
'class' => 'btn btn-primary',
'data' => [
'method' => 'POST',
'params' => ['Submit' => 'insertdata'],
],
]
)?>
在你的第二个片段中,如下所示: -
public class Singleton {
private static Singleton uniqInstance;
public ArrayList<Names> names = new ArrayList<Names>();;
private Singleton() {
}
public static Singleton getInstance() {
if (uniqInstance == null) {
{
if (uniqInstance == null)
uniqInstance = new Singleton();
}
}
return uniqInstance;
}
public setArrayList(ArrayList<Names> names)
{
this.names = names;
}
public getArrayList()
{
return this.names;
}
}
答案 2 :(得分:0)
您可以将其传递给托管活动或将其存储在扩展的Application类中。 Official documentation
其他选项包括EventBus,Intent,MessageLooper。
EventBus很简单,但如果你过度使用它,代码就会变成意大利面。
答案 3 :(得分:0)
要在片段之间传递数据,请按照以下步骤操作:
Bundle bundle = new Bundle();
putParcelableArrayList("ArrayListKey", mArrayList);
Fragment fragment = new MasterSearchTabFragment();
fragment.setArguments(bundle);
//Add Fragment while starting it
getFragmentManager()
.beginTransaction()
.add(R.id.frame_layout_root_container, fragment)
.commit();
在第二篇片段中:
Bundle mBundle = new Bundle();
mBundle = getArguments();
if (null != mBundle) {
ArrayList arrayList = getParcelableArrayList("ArrayListKey");
}