对多个阵列使用一个适配器
Using one adapter for multiple arrays
我有一个情况需要一些建议。我有一个具有可扩展列表视图的应用程序。每次 child 单击都会将用户转到下一个 activity,这是一个带有列表视图的选项卡布局。选项卡布局有 3 个选项卡。我试图弄清楚如何在可扩展列表视图上单击 child 时将数据发送到 3 个选项卡列表视图。
最初我打算像这样在 setOnChildClickListener 中设置它:
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false));
listViewFri.setAdapter(mCustomListViewAdapter);
编辑:这是我设置 3 个选项卡的 tablayout activity。我不确定在哪里可以访问捆绑的附加功能。
public class WorkoutDaysActivity extends BaseActivity {
ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
Bundle extras = getIntent().getBundleExtra("args");
mToolBar = activateToolbarWithHomeEnabled();
setUpNavigationDrawer();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_workout_days, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
return rootView;
case 2: View rootView2 = inflater.inflate(R.layout.fragment_sub_page1, container, false);
TextView textView2 = (TextView) rootView2.findViewById(R.id.textView2);
textView2.setText("Workout 29 Week 1");
return rootView2;
case 3:
View rootView3 = inflater.inflate(R.layout.fragment_sub_page2, container, false);
TextView textView3 = (TextView) rootView3.findViewById(R.id.txtFrag3);
textView3.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView3;
default: View rootView4 = inflater.inflate(R.layout.fragment_workout_days, container, false);
TextView textView4 = (TextView) rootView4.findViewById(R.id.txtFrag1);
textView4.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView4;
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Monday";
case 1:
return "Wednesday";
case 2:
return "Friday";
}
return null;
}
}
}
我认为这部分会像这样:
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, objects);
但是我在 (this, objects) 下收到一个错误说 "cannot be applied to .placeholderfragment .custom object" "PlaceholderFragment cannot be converted to Context"
这是我在 onChildClick 中输入的内容:
Bundle extras = new Bundle();
final ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
objects.add(new CustomObject("Squat", "65%", "6", "150", false));
extras.putSerializable("w29w1", objects);
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("args", extras);
startActivity(intent);
感谢您的帮助!如果您需要查看我的任何代码以更好地理解我正在尝试做的事情,请告诉我!
我很难完全理解这个问题,但这是我目前的想法。
您是否试图在每个列表上设置相同的列表适配器实例?如果您想要相同的一般行为,但要将不同的数据绑定到列表,请确保为每个列表实例化新的适配器实例。
即
myList1.setAdapter(new MyAdapter());
myList2.setAdapter(new MyAdapter());
而不是
MyAdapter myAdapter = new MyAdapter();
myList1.setAdapter(myAdapter);
myList2.setAdapter(myAdapter);
此外,在与另一个 activity 通信时通过意图移动数据是明智的。在您的初始 activity 中,当一个操作提示启动下一个 activity 时,请在调用 startActivity()
之前将额外内容放入意图中。然后,在下一个 activity 中,您将调用 getIntent().getXXXExtra("...")
,其中 XXX
与您正在提取的额外内容类型匹配,而 ...
是您放置的正确键前面的额外内容 activity.
如果您使用活动中包含的片段,请使用 Fragment.setArguments()
(在 activity2 中)和 Fragment.getArguments()
(在片段 2 中)将片段 1 中的额外内容传递给 activity2 到 fragment2.
传递附加值
当您单击一个项目并想要启动一个新的 activity(要显示的新视图)时,一个好的方法是捆绑所需的信息,将其传递给新的 activity,展开所有这些信息然后根据需要将其应用于视图。
例如...
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
Bundle extras = new Bundle();
// Put all the extras you need. This can be primitives like int, boolean, double, or complex objects using the Serializable interface
// For example, say your CustomObject implements Serializable
CustomObject customObject = new CustomObject("Squats", "60%", "6", "150", false);
extras.putSerializable("object_key_here", customObject);
Intent intent = new Intent(getContext(), NextActivityHere.class);
intent.putExtra("args", extras);
startActivity(intent);
}
}
在activity包含片段的列表中:
// Likely in onCreate()
Bundle extras = getIntent.getBundleExtra("args");
MyFragment myFragment = new MyFragment();
myFragment.setArguments(extras);
// Use fragment manager to add the fragment to your view
在包含列表的片段 (myFragment) 中:
// Likely in onCreate()
Bundle extras = getArguments();
CustomObject customObject = (CustomObject) extras.getSerializable("object_key_here");
// Now you can use the object to set up the adapter, etc...
希望这至少有助于说明如何将数据从原始 activity 获取到包含列表的片段。如果我要填充列表,通常我会看到自己传递一个 ArrayList 数据。在不太了解您的场景的情况下,很难更具体。
带标签的布局
您可能想阅读 TabLayouts and ViewPagers。这些是用于创建带有选项卡的分页布局的模型。
实施这些视图应该会大大降低 Activity 的复杂性。在 activity 的 onCreate()
方法中,您将设置 TabLayout 和 ViewPager 来托管星期一、星期三和星期五的片段。
每个星期一、星期三和星期五的片段都是同一片段 class 的实例,其中包含您要填充的列表(如果我对应用程序的布局有正确的认识)。
我的假设列表
- 假设 XML 文件名为 activity_tab_host.xml
- 假设 TabLayout 有一个 id="tab_layout"
- 假设 ViewPager 有一个 id="view_pager"
- 假设 activity 文件名为 TabHostActivity.java
- 假设带有列表的片段被称为WorkoutDetailFragment.java
- 假设您的片段通过其构造函数接收数据。您可以这样做,也可以像您所做的那样通过参数传递它。我只是试图在传达总体思路时尽量减少代码。该片段只需要获取数据! 尽量不要挂在此处显示的具体方法和代码上;您可能会发现通过构造函数等传递数据更容易
- 假设数据列表在CustomObjects的ArrayLists中准备好了,简称为mondayData、wednesdayData和fridayData
- 假设您已经创建了名为 SectionsPagerAdapter 的自定义适配器
指导代码(不是 100% 需要的)
从 XML 视图源文件中获取 TabLayout 和 ViewPager 视图:
// TabHostActivity.java::onCreate()
setContentView(R.layout.activity_tab_host);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
设置您的 ViewPager:
// TabHostActivity.java::onCreate()
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
// Create fragment to populate the tabs
WorkoutDetailFragment mondayFragment = new WorkoutDetailFragment(mondayData);
WorkoutDetailFragment wednesdayFragment = new WorkoutDetailFragment(wednesdayData);
WorkoutDetailFragment fridayFragment = new WorkoutDetailFragment(fridayData);
// Add pages (fragments) to the adapter, passing the fragment and tab title
adapter.addFragment(mondayFragment, "Monday");
adapter.addFragment(wednesdayFragment, "Wednesday");
adapter.addFragment(fridayFragment, "Friday");
// Tie the adapter to your ViewPager
viewPager.setAdapter(adapter);
将 ViewPager 绑定到 TabView:
// TabHostActivity.java::onCreate()
tabLayout.setupWithViewPager(viewPager);
关于在何处实例化新片段的主要要点是...
ViewPager 是一种用于保存片段的设备,允许用户在单个 activity 中在这些片段之间切换。您应该在 TabHostActivity.java activity 中实例化片段并在 ViewPager 中设置片段。
我上面的示例通过 WorkoutDetailFragment 构造函数传递列表数据来简化情况。这是将数据传递给片段的两种简单方法之一。
通过片段构造函数传递数据:
public class WorkoutDetailFragment extends Fragment {
private ArrayList<Object> listData;
public WorkoutDetailFragment(ArrayList<Object> listData) {
this.listData = listData;
}
// Rest of the code for the fragment...
}
通过片段参数传递数据
// TabHostActivity.java
Bundle args = new Bundle(); // May have received this bundle from extras already. If so, use Bundle args = getIntent.getExtra(...
args.putExtra("list_data", listData); // Where listData is an ArrayList<CustomObject> in your case (Monday/Wednesday/Friday data)
WorkoutDetailFragment fragment = new WorkoutDetailFragment();
fragment.setArguments(args);
// Add the fragment to the view pager...
// Inside WorkoutDetailFragment.java retrieve list data from args using getArguments().getSerializable("list_data")
希望这对您有所帮助!
我有一个情况需要一些建议。我有一个具有可扩展列表视图的应用程序。每次 child 单击都会将用户转到下一个 activity,这是一个带有列表视图的选项卡布局。选项卡布局有 3 个选项卡。我试图弄清楚如何在可扩展列表视图上单击 child 时将数据发送到 3 个选项卡列表视图。
最初我打算像这样在 setOnChildClickListener 中设置它:
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false));
listViewFri.setAdapter(mCustomListViewAdapter);
编辑:这是我设置 3 个选项卡的 tablayout activity。我不确定在哪里可以访问捆绑的附加功能。
public class WorkoutDaysActivity extends BaseActivity {
ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
Bundle extras = getIntent().getBundleExtra("args");
mToolBar = activateToolbarWithHomeEnabled();
setUpNavigationDrawer();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_workout_days, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
return rootView;
case 2: View rootView2 = inflater.inflate(R.layout.fragment_sub_page1, container, false);
TextView textView2 = (TextView) rootView2.findViewById(R.id.textView2);
textView2.setText("Workout 29 Week 1");
return rootView2;
case 3:
View rootView3 = inflater.inflate(R.layout.fragment_sub_page2, container, false);
TextView textView3 = (TextView) rootView3.findViewById(R.id.txtFrag3);
textView3.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView3;
default: View rootView4 = inflater.inflate(R.layout.fragment_workout_days, container, false);
TextView textView4 = (TextView) rootView4.findViewById(R.id.txtFrag1);
textView4.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView4;
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Monday";
case 1:
return "Wednesday";
case 2:
return "Friday";
}
return null;
}
}
}
我认为这部分会像这样:
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
View rootView = inflater.inflate(R.layout.fragment_workout_days, container, false);
Bundle extras = getArguments();
CustomObject objects = (CustomObject) extras.getSerializable("w29w1");
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, objects);
但是我在 (this, objects) 下收到一个错误说 "cannot be applied to .placeholderfragment .custom object" "PlaceholderFragment cannot be converted to Context"
这是我在 onChildClick 中输入的内容:
Bundle extras = new Bundle();
final ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
objects.add(new CustomObject("Squat", "65%", "6", "150", false));
extras.putSerializable("w29w1", objects);
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("args", extras);
startActivity(intent);
感谢您的帮助!如果您需要查看我的任何代码以更好地理解我正在尝试做的事情,请告诉我!
我很难完全理解这个问题,但这是我目前的想法。
您是否试图在每个列表上设置相同的列表适配器实例?如果您想要相同的一般行为,但要将不同的数据绑定到列表,请确保为每个列表实例化新的适配器实例。
即
myList1.setAdapter(new MyAdapter());
myList2.setAdapter(new MyAdapter());
而不是
MyAdapter myAdapter = new MyAdapter();
myList1.setAdapter(myAdapter);
myList2.setAdapter(myAdapter);
此外,在与另一个 activity 通信时通过意图移动数据是明智的。在您的初始 activity 中,当一个操作提示启动下一个 activity 时,请在调用 startActivity()
之前将额外内容放入意图中。然后,在下一个 activity 中,您将调用 getIntent().getXXXExtra("...")
,其中 XXX
与您正在提取的额外内容类型匹配,而 ...
是您放置的正确键前面的额外内容 activity.
如果您使用活动中包含的片段,请使用 Fragment.setArguments()
(在 activity2 中)和 Fragment.getArguments()
(在片段 2 中)将片段 1 中的额外内容传递给 activity2 到 fragment2.
传递附加值 当您单击一个项目并想要启动一个新的 activity(要显示的新视图)时,一个好的方法是捆绑所需的信息,将其传递给新的 activity,展开所有这些信息然后根据需要将其应用于视图。
例如...
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
Bundle extras = new Bundle();
// Put all the extras you need. This can be primitives like int, boolean, double, or complex objects using the Serializable interface
// For example, say your CustomObject implements Serializable
CustomObject customObject = new CustomObject("Squats", "60%", "6", "150", false);
extras.putSerializable("object_key_here", customObject);
Intent intent = new Intent(getContext(), NextActivityHere.class);
intent.putExtra("args", extras);
startActivity(intent);
}
}
在activity包含片段的列表中:
// Likely in onCreate()
Bundle extras = getIntent.getBundleExtra("args");
MyFragment myFragment = new MyFragment();
myFragment.setArguments(extras);
// Use fragment manager to add the fragment to your view
在包含列表的片段 (myFragment) 中:
// Likely in onCreate()
Bundle extras = getArguments();
CustomObject customObject = (CustomObject) extras.getSerializable("object_key_here");
// Now you can use the object to set up the adapter, etc...
希望这至少有助于说明如何将数据从原始 activity 获取到包含列表的片段。如果我要填充列表,通常我会看到自己传递一个 ArrayList 数据。在不太了解您的场景的情况下,很难更具体。
带标签的布局
您可能想阅读 TabLayouts and ViewPagers。这些是用于创建带有选项卡的分页布局的模型。
实施这些视图应该会大大降低 Activity 的复杂性。在 activity 的 onCreate()
方法中,您将设置 TabLayout 和 ViewPager 来托管星期一、星期三和星期五的片段。
每个星期一、星期三和星期五的片段都是同一片段 class 的实例,其中包含您要填充的列表(如果我对应用程序的布局有正确的认识)。
我的假设列表
- 假设 XML 文件名为 activity_tab_host.xml
- 假设 TabLayout 有一个 id="tab_layout"
- 假设 ViewPager 有一个 id="view_pager"
- 假设 activity 文件名为 TabHostActivity.java
- 假设带有列表的片段被称为WorkoutDetailFragment.java
- 假设您的片段通过其构造函数接收数据。您可以这样做,也可以像您所做的那样通过参数传递它。我只是试图在传达总体思路时尽量减少代码。该片段只需要获取数据! 尽量不要挂在此处显示的具体方法和代码上;您可能会发现通过构造函数等传递数据更容易
- 假设数据列表在CustomObjects的ArrayLists中准备好了,简称为mondayData、wednesdayData和fridayData
- 假设您已经创建了名为 SectionsPagerAdapter 的自定义适配器
指导代码(不是 100% 需要的)
从 XML 视图源文件中获取 TabLayout 和 ViewPager 视图:
// TabHostActivity.java::onCreate()
setContentView(R.layout.activity_tab_host);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
设置您的 ViewPager:
// TabHostActivity.java::onCreate()
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getFragmentManager());
// Create fragment to populate the tabs
WorkoutDetailFragment mondayFragment = new WorkoutDetailFragment(mondayData);
WorkoutDetailFragment wednesdayFragment = new WorkoutDetailFragment(wednesdayData);
WorkoutDetailFragment fridayFragment = new WorkoutDetailFragment(fridayData);
// Add pages (fragments) to the adapter, passing the fragment and tab title
adapter.addFragment(mondayFragment, "Monday");
adapter.addFragment(wednesdayFragment, "Wednesday");
adapter.addFragment(fridayFragment, "Friday");
// Tie the adapter to your ViewPager
viewPager.setAdapter(adapter);
将 ViewPager 绑定到 TabView:
// TabHostActivity.java::onCreate()
tabLayout.setupWithViewPager(viewPager);
关于在何处实例化新片段的主要要点是... ViewPager 是一种用于保存片段的设备,允许用户在单个 activity 中在这些片段之间切换。您应该在 TabHostActivity.java activity 中实例化片段并在 ViewPager 中设置片段。
我上面的示例通过 WorkoutDetailFragment 构造函数传递列表数据来简化情况。这是将数据传递给片段的两种简单方法之一。
通过片段构造函数传递数据:
public class WorkoutDetailFragment extends Fragment {
private ArrayList<Object> listData;
public WorkoutDetailFragment(ArrayList<Object> listData) {
this.listData = listData;
}
// Rest of the code for the fragment...
}
通过片段参数传递数据
// TabHostActivity.java
Bundle args = new Bundle(); // May have received this bundle from extras already. If so, use Bundle args = getIntent.getExtra(...
args.putExtra("list_data", listData); // Where listData is an ArrayList<CustomObject> in your case (Monday/Wednesday/Friday data)
WorkoutDetailFragment fragment = new WorkoutDetailFragment();
fragment.setArguments(args);
// Add the fragment to the view pager...
// Inside WorkoutDetailFragment.java retrieve list data from args using getArguments().getSerializable("list_data")
希望这对您有所帮助!