我想知道是否可以向多个类发送额外的意图,并使用switch语句查看发送的额外内容,并根据更多内容更改回收站视图数据。
例如,我的应用程序有一个包含12个选项的可扩展列表视图。每个选项都使用tablayout和recycler视图将其发送到新类。使用tablayout,我为每个选项卡分配了3个片段,其中当前为每个回收站视图设置了数据。这很好用,但我想知道如何在不为12种不同的选项制作大量课程的情况下做到这一点。
这是我的可扩展列表视图代码。在第一个孩子的位置,你可以看到我正在谈论的putExtra。
expandableListView.setAdapter(mAdapter);
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) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("value", "w29w1");
startActivity(intent);
}
if (childPosition == 1) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 2) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 3) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
} else return false; }
这是我的WorkoutDaysActivity,其中intent在点击可展开列表视图时发送给用户:
public class WorkoutDaysActivity extends BaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbar();
setUpNavigationDrawer();
ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
this.addPages(vp);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(listener(vp));
}
//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new MondayFragment());
adapter.addPage(new WedFragment());
adapter.addPage(new FridayFragment());
//SET ADAPTER to pager
pager.setAdapter(adapter);
}
private TabLayout.OnTabSelectedListener listener(final ViewPager pager) {
return new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
}
这是我的标签片段之一。其他两个基本相同:
public class WedFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.wed_fragment, null);
// //RECYCLERVIEW
RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout()));
return view;
}
private ArrayList<Workout> getWedWorkout() {
//COLLECTION OF MONDAY WORKOUT
ArrayList<Workout> workout = new ArrayList<>();
//SINGLE EXERCISE
Workout workouts = new Workout("Bench", "60", "6", "155", false);
//ADD TO COLLECTION
workout.add(workouts);
workouts = new Workout("Bench", "70", "5", "175", false);
workout.add(workouts);
return workout;
}
@Override
public String toString() {
return "Wednesday";
}
}
这三个片段类在理论上是我想放置一个开关盒来查看可扩展列表视图中的哪个项目被点击,并根据点击的内容填充3个选项卡中的数据。
感谢您的帮助!
答案 0 :(得分:0)
您可以保存在列表或散列图中的意图内发送的数据。
ArrayList<String> list = new ArrayList<String>();
list.add("w29w1");
list.add("w29w2");
....
在onChildClick:
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("value", list.get(childPosition));
startActivity(intent);
}
或 HashMap:
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "w29w1");
map.put(1, "w29w2");
...
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
intent.putExtra("value", map.get(childPosition));
startActivity(intent);
编辑:在 WorkoutDaysActivity.class
中使用该值Intent intent = getIntent();
String value = intent.getStringExtra("value");
将数据发送到片段:
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(value);
adapter.addPage(mondayFragment);
...
pager.setAdapter(adapter);
}