我有MainActivity和三个片段活动。在MainActivity中,它从REST服务调用中获取数据。我想将REST数据传递给所有三个片段。如何实现呢?
这里是我的代码
MainActivty
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private CustomListAdapter adapter;
private static final String TAG = MainActivity.class.getSimpleName();
public List<MenuItem> mainList = new ArrayList<MenuItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JsonArrayRequest itemReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
JSONArray jsonArray = response.getJSONArray(0);
// Parsing json
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject obj = jsonArray.getJSONObject(i);
MenuItem menuItem = new MenuItem();
menuItem.setName(obj.getString("name"));
menuItem.setImage(obj.getString("image"));
menuItem.setPrice(obj.getString("price"));
menuItem.setDesc(obj.getString("desc"));
mainList.add(menuItem);
} catch (JSONException e) {
e.printStackTrace();
}
}
}catch (JSONException e){
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new First(), "FIRST");
adapter.addFragment(new Second(), "SECOND");
adapter.addFragment(new Third(), "THIRD");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
片段活动
public class First extends Fragment{
private ProgressDialog pDialog;
private GridView gridView;
private CustomListAdapter adapter;
public First() {
}
@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_one, container, false);
gridView = (GridView) view.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), startersList);
gridView.setAdapter(adapter);
return view;
}
}
在这一行 adapter = new CustomListAdapter(getActivity(),startersList); 我想从MainActivity获取列表数据
答案 0 :(得分:1)
如果你可以使你的菜单项类实现Parcelable
How can I make my custom objects Parcelable?
然后,只需将参数传递给片段
即可Bundle args = new Bundle();
args.putParcelableArrayList("list", mainList) ;
Fragment first = new First();
first.setArguments(first);
adapter.addFragment(first, "FIRST");
在片段中,您将使用
getArguments().getParcelableArrayList("list")
或者,您也可以将JSON字符串传递给参数并在Fragment中解析它。
注意:但是,只有在onResponse中移动viewpager设置方法时,这才有效。否则,您的碎片已经创建了空列表。
或者您可以在适配器
中实现上述逻辑@Override
public Fragment getItem(int position) {
Fragment f = mFragmentList.get(position);
// TODO f.setArguments()
return f;
}