Android中Activity和Fragment之间的通信
- Fragment启动Activity传数据到Fragment
举例:城市选择列表。一个Fragment启动Activity,Activity再把城市选择数据回传到Fragment中。
Fragment中方法
iv_city.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(), CityActivity.class);
startActivityForResult(intent,0);
}
});
重写onActivityResult方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("tag", "返回结果"+resultCode);
if (data != null) {
String stringExtra = data.getStringExtra("city");
cityid= data.getStringExtra("cityId");
Log.d("tag", "城市ID"+cityid);
getData(10);
if (resultCode==1) {
tv_city.setText(stringExtra);
}
}
}
CityActivity
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final TextView textView = (TextView) holder.itemView
textView.setText(getItem(position).getCityName())
// final String cityId = getItem(position).getCityId()
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(CityActivity.this, "选择城市:"+textView.getText(), Toast.LENGTH_SHORT).show()
// Intent intent =new Intent()
Intent intent = getIntent()
intent.putExtra("city",textView.getText().toString())
//
for (int i = 0
if (textView.getText().toString().equals(cities.get(i).getCityName())){
cityId = cities.get(i).getCityId()
}
}
intent.putExtra("cityId",cityId)
setResult(1,intent)
finish()
}
})
}
- Fragment传数据到Fragment
举例:左边是电影列表的Fragment,右边是一个电影的具体内容的Fragment。点击左边的Fragment传数据给右边的Fragment显示。可以通过Fragment的fragment.setArguments(bundle)传。
MovieListFragment
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), names[position]+":"+files[position], Toast.LENGTH_LONG).show();
MovieContentFragment fragment=new MovieContentFragment();
Bundle bundle=new Bundle();
bundle.putString("fileName", files[position]);
fragment.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.flayout, fragment);
transaction.commit();
}
MovieContentFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
String name = bundle.getString("fileName");
- Activity传数据到Fragment
举例:新闻头部标签加载对应的Fragment. Activity通过传不同的url地址构建不同的Fragment。
Activity
/**
* 初始化List<Fragment>
*/
private void initFragment() {
fragments = new ArrayList<>();
int len = titles.length;
for (int i = 0; i < len; i++) {
ContentFragment cf = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("key", urls[i]);
cf.setArguments(bundle);
fragments.add(cf);
}
}
Fragment
Bundle bundle = getArguments();
String values = bundle.getString("key");
MyTask myTask=new MyTask(new MyTask.CallBack() {
@Override
public void getData(List<NewsBean> list) {
data.addAll(list);
adapter.notifyDataSetChanged();
}
});
myTask.execute(values);