我为一个应用程序制作了这个代码,它假设给出了一个像胳膊和腿一样的身体部位的列表,你点击第一个片段中的一个,一个练习列表应该出现在第二个片段中,这个列表假设要改变用户按下另一个选项,是否有问题它显示第一个选项自动但在用户按下另一个选项后它没有改变请如何更改第二个片段中的内容。
主要课程
public class MainActivity extends Activity
implements WorkoutListFragment.WorkoutListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WorkoutDetailFragment frag = (WorkoutDetailFragment)
getFragmentManager().findFragmentById(R.id.detail_frag);
frag.setWorkoutId(1);
}
public void itemClicked(long id){
int itemSelected = (int)id;
String message = String.format("ID selected %d", itemSelected);
Log.v("DEBUG", message);
}
}
第二课
public class Workout {
private String name;
private String description;
public Workout(String name, String description) {
this.name = name;
this.description = description;
}
public static final Workout[] workouts= {
new Workout("Chest Workout", "3 Bench Press\n10Flys\nWide Dips"),
new Workout("Legs", "5 Squats\n3x12 Leg Curl\n3x10 Leg Press"),
new Workout("Back", "10 Pullups\n 3x8 Dumbell Rows"),
new Workout("Arms", "10 Biceps Curls\n10 Dips\n3x10 Preacher Curls")
};
public String getName() {
return name;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return this.name;
}
}
第三课
public class WorkoutDetailFragment extends Fragment {
private final String TAG = "DEBUG";
private long workoutId;
public WorkoutDetailFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
@Override
public void onStart(){
super.onStart();
View view = getView();
if(view !=null){
TextView title = (TextView)view.findViewById(R.id.TextTitle);
Workout workout = Workout.workouts[(int)workoutId];
title.setText(workout.getName());
Log.v(TAG, workout.getName());
TextView description = (TextView)view.findViewById(R.id.TextDescription);
description.setText(workout.getDescription());
Log.v(TAG, workout.getDescription());
}
}
public void setWorkoutId(long id){
this.workoutId = id;
}
}
第四课
public class WorkoutListFragment extends ListFragment {
static interface WorkoutListener {
void itemClicked(long id);
}
private WorkoutListener listener;
public WorkoutListFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
this.listener = (WorkoutListener)activity;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
if (listener != null){
listener.itemClicked(id);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// create a string array with workout names
String[] names = new String [Workout.workouts.length];
for (int i=0; i < names.length; i++){
names[i] = Workout.workouts[i].getName();
}
// Create an array adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
inflater.getContext(),
android.R.layout.simple_list_item_1,
names);
// bind the adapter to the control
setListAdapter(adapter);
// this call gives you the default layout view for the
// ListFragment
return super.onCreateView(inflater, container, savedInstanceState);
}
}
答案 0 :(得分:0)
在onClickListener或任何听取点击的内容中,您应该在活动中写下这个:
FragmentManager fragementManager = getFragmentManager();
fragementManager.beginTransaction().replace(R.id.layout, new NextFragment()).commit();
在此示例中,R.id.layout应该是放置片段的布局的ID。 NextFragment应该是要显示的片段的类。 希望这可以帮助! :)