1

我有一个 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?任何建议将不胜感激。谢谢

4

1 回答 1

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 将 FragmentA 替换为 FragmentB

由于这不是我一直在寻找的好方法,如果您有更好的想法,请告诉我您的建议。谢谢

于 2013-03-19T09:36:02.087 回答