我有两个碎片, 我想将列表视图数据从片段1发送到片段2。
当用户点击Fragment1_listview.xml
上的按钮时,fragment2_listview
将会出现,并且它与fragment1具有相同的数据。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
CartFragment();
FragmentManager manager = getSupportFragmentManager();
final int commit =
manager.beginTransaction().replace(R.id.fragment_container, cartFragment,
cartFragment.getTag()).commit();
}
}
Fragment1.java
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_cart, container, false);
return view;
}
@Override
public void processFinish(String s) {
try {
itemList = new JsonConverter<Cart>().toArrayList(s, Cart.class);
} catch (Exception e) {
e.printStackTrace();
}
BindDictionary dic = new BindDictionary();
dic.addStringField(R.id.tvName, new StringExtractor<Cart>() {
@Override
public String getStringValue(Cart item, int position) {
return item.orderName;
}
});
dic.addStringField(R.id.tvDesc, new StringExtractor<Cart>() {
@Override
public String getStringValue(Cart item, int position) {
return item.description;
}
}).visibilityIfNull(View.GONE);
adapter = new FunDapter<>(CartFragment.this.getActivity(), itemList,
R.layout.cart_row, dic);
lv = (ListView)view.findViewById(R.id.lvCart);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
Fragment2.java
public View onCreateView(LayoutInflater inflater, final ViewGroup
container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_payment, container, false);
return view;
}
但我不知道该怎么做。请帮帮我。
答案 0 :(得分:0)
我认为最适合您的解决方案是将变量放在主要活动中并从片段中访问它们。我的意思是,如果你必须在所有片段中做同样的事情,你可以在活动中编写代码,然后调用你需要的方法。