0

我正在开发一个利用 ActionBar 选项卡导航来交换片段的应用程序。每个选项卡调用相同的 listfragment 但具有不同的数据集。当用户在我的列表片段中选择一个项目时,会显示一个详细列表片段。在我的详细列表片段中,我有一个按钮,它显示一个对话框,其中包含外部 OnClickListener 中的交叉引用。

我正在尝试在我的根列表片段中实现一个回调,它将使用来自对话框选择的数据显示一个新的细节片段。

我的问题是我只有一个 MainActivity,然后其他一切都是片段。我不确定如何发起回拨。

这是我如何启动我的 OnClickListener 并将其设置为我的详细信息片段的 OnActivityCreate 方法中的按钮:

    // create instance of the OnClickListener
    SeeAlsoOnClickListener seeAlsoListener = new SeeAlsoOnClickListener(this.getActivity(), seeAlsoList, categories);
    button.setOnClickListener(seeAlsoListener);

这是我的 SeeAlsoOnClickListener 类:

public class SeeAlsoOnClickListener implements OnClickListener {

private Context context;
private ArrayList<String> categories;
private String[] seeAlsoList;
private String selection;


public SeeAlsoOnClickListener(Context context, String[] array, ArrayList<String> categories) {
    this.context = context;
    this.seeAlsoList = array;
    this.categories = categories;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    ListAdapter adapter = new ArrayAdapter<String>(context, R.layout.see_also_picker, seeAlsoList) {

        ViewHolder holder;
        Drawable icon;

        class ViewHolder {
            ImageView icon;
            TextView title;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            final LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(
                        R.layout.see_also_picker, null);

                holder = new ViewHolder();
                holder.icon = (ImageView) convertView
                        .findViewById(R.id.icon);
                holder.title = (TextView) convertView
                        .findViewById(R.id.title);
                convertView.setTag(holder);
            } else {
                // view already defined, retrieve view holder
                holder = (ViewHolder) convertView.getTag();
            }       

            int tile = 0;
            switch (Integer.parseInt(categories.get(position))) {
                case 0:
                    tile = context.getResources().getIdentifier("acronyms","drawable",context.getPackageName());
                    break;
                case 1:
                    tile = context.getResources().getIdentifier("algorithm","drawable",context.getPackageName());
                    break;
                case 2:
                    tile = context.getResources().getIdentifier("anatomy","drawable",context.getPackageName());
                    break;
                case 3:
                    tile = context.getResources().getIdentifier("calculator","drawable",context.getPackageName());
                    break;
                case 4:
                    tile = context.getResources().getIdentifier("medicine","drawable",context.getPackageName());
                    break;
                case 5:
                    tile = context.getResources().getIdentifier("treatment","drawable",context.getPackageName());
                    break;
            }

            holder.title.setText(seeAlsoList[position]);
            holder.icon.setImageResource(tile);

            return convertView;
        }
    };

    // display alert dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("See Also");
    builder.setAdapter(adapter,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int item) {                      
                    // get the selection
                    selection = seeAlsoList[item];                  
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}
}

这一切都很好,除了我需要根据选择换出另一个片段。任何想法或建议将不胜感激!先谢谢了。

4

1 回答 1

0

我可以为你推荐两种方法:

1)可以在非列表Fragment中定义回调接口,在MainActivity中实现该回调,在Fragment的onAttach()方法中获取回调实例。与活动相比,使用 findFragmentById() 或 findFragmentByTag() 您可以获得指向 ListFragment 的直接链接并调用公共方法,例如您定义的主题、swapData(datalist)。

有关此方法的更多详细信息 -官方文档

2) 在列表片段中定义广播接收器,并在 onReceive() 方法中交换数据,基于额外数据。从对话框的 onClick() 方法中,您应该在 Extras 中发送带有所需数据的广播。

于 2013-10-13T21:46:52.653 回答