当只有一个片段可见时,片段之间进行通信

时间:2014-02-19 14:05:05

标签: java android android-fragments

我有一个问题,在动态插入的片段之间进行通信,我管理使用接口系统来进行通信,如果片段都是可见的,但当我正在查看它们时,我不能使用他们的行为,因为他们没有一个标签,还有什么想法?

主要活动

公共类MainActivity扩展FragmentActivity实现OnClickListener,Communicator {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

if (isSingleFragment()) {

        Button btn_color = (Button) findViewById(R.id.button_color);
        Button btn_text = (Button) findViewById(R.id.button_text);
        Button btn_list = (Button) findViewById(R.id.button_list);
        btn_color.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_text.setOnClickListener(this);

        singleFragActions();

    } else {

        doubleFragActions();
    }


// some more stuff happens

@Override
public void apply(String data) {

    Log.d("data", data+"");
    FragmentManager fm = getSupportFragmentManager();
    Frag_list frag_list = (Frag_list) fm.findFragmentByTag("list");
//i cannot use id because the fragment in 
//MainActivity are in a layout and are enterd dynamicaly

    frag_list.addItem(data);

}
这里有三个片段,当在小屏幕上显示时,顶部的三个按钮会改变它们,如果在平板电脑上,这三个都是显示的,这就是界面

public interface Communicator {

public void apply(String data);

}

frag_text(第一个片段)

public class Frag_text extends Fragment implements OnClickListener{


Communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // stuff
return view;

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    comm = (Communicator) getActivity();
}
@Override
public void onClick(View v) {


    EditText input = (EditText) getView().findViewById(R.id.frag_text_edit);
    TextView output = (TextView) getView().findViewById(R.id.frag_text_show);

    String text_get = input.getText().toString();
    output.setText("hello "+text_get+" how are you today?");

    comm.apply(text_get);
}
}

frag_list(其他片段)

public class Frag_list extends Fragment{



ArrayList<String> list;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_list, container, false);

list = new ArrayList<String>();



ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(getActivity(), 
         android.R.layout.simple_list_item_1, list);

ListView lv = (ListView) view.findViewById(R.id.list_frag);
lv.setAdapter(adapter);

return view;

}

public void addItem(String data){

    list.add(data);
}
}

如果没有标签,如何访问其他片段?

1 个答案:

答案 0 :(得分:1)

如果你碰巧知道你将调用什么片段,那么FragmentActivity在这种情况下可以充当调解者,因为它只是一个“主持人”。您已经拥有此Communicator界面。让它与给定的片段进行通信:

public interface Communicator {
    public void apply(String data, String tag);
}

覆盖:

@Override
public void apply(String data, String tag) {
    FragmentManager fm = getSupportFragmentManager();
    Frag_list frag_list = (Frag_list) fm.findFragmentByTag(tag);
    //Fallback plan in case the fragment has not been created.
    if(frag_list == null) {
        frag_list = new Frag_list();
        //Include the fragment for future reference.
        //This fragment has no view
        fm.beginTransaction().add(frag_list, tag);
    }
    frag_list.addItem(data);
}

修改片段以使列表始终可用:

public class Frag_list extends Fragment{

ArrayList<String> list = new ArrayList<String>();
//...

    public void addItem(String data){
        //Add check to avoid problems
        if(list != null) {
            list.add(data);
        }
    }
}