我有一个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?任何建议将不胜感激。感谢
答案 0 :(得分:0)
我通过界面以凌乱的方式解决了我的问题。所以,我将适配器更改为类似的东西(接口和添加的新方法)
public class MyMessagesAdapter extends BaseAdapter {
public interface OnViewButtonClickedListener {
public void OnPerson(String id);
}
private static 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;
private OnViewButtonClickedListener listener;
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(final 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) {
listener.OnPerson("" + position);
}
});
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;
}
public void setOnViewButtonClickedListener(OnViewButtonClickedListener listener) {
this.listener = listener;
}
static class ViewHolder {
TextView tvUserName;
TextView tvDescription;
ImageView ivPicture;
Button btnView;
}
}
然后我像这样在fragmentA中实现它。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Setup adapter in order to be assigned to list view later
adapter = new MyMessagesAdapter(context);
adapter.setOnViewButtonClickedListener(new MyMessagesAdapter.OnViewButtonClickedListener() {
@Override
public void OnPerson(String id) {
Toast.makeText(context, id, Toast.LENGTH_SHORT).show();
}
});
// Assign adapter to list view
listView.setAdapter(adapter);
Log.i(TAG, "Fragment created");
}
因此,我需要为此活动添加另一个接口(如前所述)并在FragmentActivity中实现它。然后FragmentActivity将用FragmentB替换FragmentA
由于这不是我想要的好方法,如果你有更好的想法,请告诉我你的建议。感谢