我有一个 FragmentActivity 作为我的片段的持有者。
public class MainScreen extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame, new FragmentA())
.commit();
}
}
FragmentA 包括一个列表,每一行都有一个按钮。
public class MyMessagesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create view
View view = inflater.inflate(R.layout.fragment_my_messages, container, false);
// Getting context
context = getActivity().getApplicationContext();
// Add views to fragment
listView = (ListView) view.findViewById(R.id.listView);
Log.i(TAG, "View created");
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Setup adapter in order to be assigned to list view later
adapter = new MyMessagesAdapter(context);
// Assign adapter to list view
listView.setAdapter(adapter);
Log.i(TAG, "Fragment created");
}
}
这是适配器的代码:
public class MyMessagesAdapter extends BaseAdapter {
private final String TAG = "*** MyMessagesAdapter ***";
private static final String[] TEMP_ITEMS = {"Person 1", "Person 2", "Person 3", "Person 4", "Person 5", "Person 6"};
private static final String TEMP_DESCRIPTION = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
private LayoutInflater myInflater;
private ImageDownloader imageDownloader;
public MyMessagesAdapter(Context context) {
myInflater = LayoutInflater.from(context);
imageDownloader = ImageDownloader.getInstance(context);
}
@Override
public int getCount() {
return TEMP_ITEMS.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_my_messages_row, null);
holder = new ViewHolder();
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
holder.ivPicture = (ImageView) convertView.findViewById(R.id.ivPicture);
holder.btnView = (Button) convertView.findViewById(R.id.btnView);
holder.btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO implement view action
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvUserName.setText(TEMP_ITEMS[position]);
holder.tvDescription.setText(TEMP_DESCRIPTION);
imageDownloader.displayImage(holder.ivPicture, "");
return convertView;
}
static class ViewHolder {
TextView tvUserName;
TextView tvDescription;
ImageView ivPicture;
Button btnView;
}
}
每当用户点击 holder.btnView 按钮时,我都想用 FragmentB 替换 FragmentA。我不确定什么是最好的方法。我可以在这个适配器中创建一个接口并在 FragmentActivity 类中实现它,但我不确定这是一个正确的方法。
如何告诉 FragmentActivity 用 FragmentB 替换 FragmentA?任何建议将不胜感激。谢谢