如何将变量从适配器传递到片段?

时间:2018-11-03 05:00:04

标签: android android-fragments android-adapter

我有一个recyclerview。一旦用户点击了recyclerview项目的任何项目,它应该打开一个新的片段。但是我想通过它传递一个id,就像我们使用putExtra故意发送一样。

下面是我的适配器onBindViewHolder方法代码-

public void onBindViewHolder(@NonNull DashboardViewHolder holder, int position) {
        final Dashboard product = dashboardList.get(position);

        //loading the image
        Glide.with(mCtx)
                .load(product.getImage())
                .into(holder.imageView);

        holder.rate.setText(product.getRate());
        holder.name.setText(product.getName());
        holder.city.setText(product.getCity());
        //holder.id.setText(String.valueOf(product.getId()));
        holder.boatList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mCtx, ""+String.valueOf(product.getId()), Toast.LENGTH_SHORT).show();
                //Intent intent = new Intent(mCtx, AddNewBoatFragment.class);
                //intent.putExtra("boat_id", product.getId());
                //intent.putExtra("owner_id", product.getOwner_id());
                //mCtx.startActivity(intent);
                //getSupportFragmentManager().beginTransaction().replace(R.id.dahsboard_fragment,
                        //new MyBoatFragment()).commit();
                Fragment fragment = new AddNewBoatFragment();
                FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.dahsboard_fragment, fragment);
                ft.commit();
            }
        });
    }

下面是我的课程-

public Dashboard(int id, int owner_id, String image, String rate, String name, String city){
        this.id = id;
        this.owner_id = owner_id;
        this.image = image;
        this.rate = rate;
        this.name = name;
        this.city = city;
    }

    public int getId(){ return id; }

    public String getImage() {
        return image;
    }

    public String getRate() { return rate; }

    public String getName(){
        return name;
    }

    public String getCity() { return city; }

    public int getOwner_id() { return owner_id; }

3 个答案:

答案 0 :(得分:0)

您可以使用Bundle

发送
        Fragment fragment = new AddNewBoatFragment();
        Bundle bundle = new Bundle();
        bundle.putString("myIDKey",product.getId());
        fragment.setArguments(bundle);
        FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.dahsboard_fragment, fragment);
        ft.commit();

,在OnCreateView的{​​{1}}方法中,您可以获取附加参数并使用它

答案 1 :(得分:0)

我只是将需要传递给片段的变量放在Public Static中 可以从任何其他班级访问

答案 2 :(得分:0)

您应该在AddNewBoatFragment中声明一个静态方法,以便根据给定的参数创建一个Fragment实例。

public class AddNewBoatFragment extends Fragment {

    private static final String ARGUMENT_BOAT_ID = "ARGUMENT_BOAT_ID";
    private static final String ARGUMENT_OWNER_ID = "ARGUMENT_OWNER_ID";

    public static AddNewBoatFragment newInstance(int boatId, String ownerId) {
        Bundle args = new Bundle();
        // Save data here
        args.putInt(ARGUMENT_BOAT_ID, boatId);
        args.putString(ARGUMENT_OWNER_ID, ownerId);
        AddNewBoatFragment fragment = new AddNewBoatFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        // Retrieve the data here
        int boatId = getArguments().getInt(ARGUMENT_BOAT_ID);
        String ownerId = getArguments().getString(ARGUMENT_OWNER_ID);
        return view;
    }
}

然后从Adapter类中的onClick方法中修改块代码。

holder.boatList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = AddNewBoatFragment.newInstance(product.getId(), product.getOwner_id());
        FragmentManager fm = ((AppCompatActivity)mCtx).getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.dahsboard_fragment, fragment);
        ft.commit();
    }
});