gpt4 book ai didi

java - 将对象从 Fragment 的 GridView 传递到另一个 Fragment 的 ListView

转载 作者:行者123 更新时间:2023-12-01 09:57:33 25 4
gpt4 key购买 nike

我在 FragmentA 中有一个包含所有对象的 GridView。当用户点击每个对象时,这些对象将被添加到 FragmentB 的 ListView 中。

请注意,FragmentA 和 FragmentB 并排位于同一个 Activity 中。

我尝试使用 Bundle 将对象从 FragmentA 发送到 FragmentB,但我被困住了,因为我不知道 FragmentB 如何知道已添加新对象。

Bundle 对象从 ProductGridAdapter(在 FragmentA 下)发送,接收者将是 FragmentB,其中 FragmentB 将更新 ListView 以获取 FragmentA 添加的最新对象

这是我的代码:

ProductGridAdapter.java

public class ProductGridAdapter extends ArrayAdapter<Product> {

private List<Product> productList;
private Context context;

public ProductGridAdapter(Context context, int resource,
List<Product> objects) {
super(context, resource, objects);
this.context = context;
this.productList = objects;
}

@Override
public int getCount() {
return ((null != productList) ?
productList.size() : 0);
}

@Override
public Product getItem(int position) {
return ((null != productList) ?
productList.get(position) : null);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(null == view) {
view = layoutInflater.inflate(R.layout.grid_product, null);
}

final Product product = productList.get(position);

if(product != null) {
final CardView productGridLayout = (CardView) view.findViewById(R.id.product_gridlayout);
final TextView productName = (TextView) view.findViewById(R.id.product_name);
final ImageView productIcon = (ImageView) view.findViewById(R.id.product_icon);

productName.setText(product.getName());
productIcon.setImageDrawable(product.getDrawable());

productGridLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
fragment.setArguments(bundle);
}
});
}

return view;
}
}

FragmentA.java

public class FragmentA extends Fragment {

private GridView gridView;
private ProductGridAdapter productAdapter;

public FragmentA() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);

gridView = (GridView) rootView.findViewById(R.id.gridview);
productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
gridView.setAdapter(productAdapter);

gridView.destroyDrawingCache();
gridView.setVisibility(GridView.INVISIBLE);
gridView.setVisibility(GridView.VISIBLE);

return rootView;
}
}

FragmentB.java

 public class FragmentB extends Fragment {

private ListView listView;
private PaymentListAdapter paymentAdapter;
private LinearLayout button;
private ArrayList<String> arr;

public FragmentB() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_payment, container, false);

listView = (ListView) rootView.findViewById(android.R.id.list);
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, getProductList());
// button = (LinearLayout) rootView.findViewById(R.id.payment);

listView.setAdapter(paymentAdapter);

Bundle bundle = this.getArguments();
Product product = (Product) bundle.getSerializable("product");

return rootView;
}

最佳答案

您可以在主 Activity 的接口(interface)类中使用两个常用方法,以更新两个 fragment 中的 View ,当 FragmentA 中发生某些更改时,您必须通知主 Activity 发生更改,然后主 Activity 应将有关更改的信息与 FragementB 进行通信。这样做,希望这对你有帮助。快乐编码!

关于java - 将对象从 Fragment 的 GridView 传递到另一个 Fragment 的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com