我试图通过一个片段来传播一个相同的片段并传递参数,它的工作方式,但传递的参数应用于前一个片段,但不应用于新片段。
该片段包含TextView和Spinner。
片段类:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.myfragment,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle b = getArguments();
qust = b.getString("Question","");
id = b.getInt("id");
final connection con = (connection) getActivity();
question = (TextView) getActivity().findViewById(R.id.question);
spinner = (Spinner) getActivity().findViewById(R.id.spinner);
question.setText(qust);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),id,R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i!=0)
con.inflate();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
当用户选择前一个片段微调器中的任何项目时,我正在对片段进行膨胀。
MainActivity类:
public class MainActivity extends AppCompatActivity implements connection {
int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragments mF = new MyFragments();
Bundle b = new Bundle();
b.putString("Question","Where are you?");
b.putInt("id",R.array.planets_array);
mF.setArguments(b);
FragmentManager manage = getFragmentManager();
FragmentTransaction trans = manage.beginTransaction();
trans.add(R.id.root,mF,"frag");
trans.commit();
}
@Override
public void inflate() {
MyFragments f = new MyFragments();
Bundle b = new Bundle();
b.putString("Question","what are you doing?");
b.putInt("id",R.array.colours);
f.setArguments(b);
FragmentTransaction tran = getFragmentManager().beginTransaction();
tran.add(R.id.root,f,"added");
tran.commit();
}
}
用于传递黑白片段的界面:
public interface connection {
void inflate();
}
答案 0 :(得分:0)
我认为已经发生了,因为你实例化了新的片段 你的inflate()方法没有唯一的TAG。设置唯一的TAG或使用不同的添加(@IdRes int containerViewId,Fragment fragment)。