java - 单击按钮后如何将 RecyclerView 适配器中的对话框的详细信息与另一个 fragment 相匹配

标签 java android

我是 Android 编码新手,我已经在这里和 YouTube 上尝试了所有可能的解决方案,但仍然很困难。我只想在按下按钮时如何将 fragment 对话框中的数据与另一个 fragment 进行匹配。我们正在做一个简单的项目。 :)

Here is what we want to do.Kindly watch this link: /image/70NxE.jpg

我们想要更改单击“设置参数”按钮后与对话框匹配的名称

这是我们在网上找到的,区别在于我们有一个对话框按钮 https://www.youtube.com/watch?v=ZXoGG2XTjzU https://www.youtube.com/watch?v=69C1ljfDvl0

Here are the codes RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

Context mContext;
List<specieList> mData;
Dialog myDialog;

public RecyclerViewAdapter(Context mContext, List<specieList> mData) {
    this.mContext = mContext;
    this.mData = mData;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v;
    Button b;

    v = LayoutInflater.from(mContext).inflate(R.layout.row,parent,false);
    final MyViewHolder vHolder = new MyViewHolder(v);

    myDialog = new Dialog(mContext);
    myDialog.setContentView(R.layout.fishpop);
    myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    b = myDialog.findViewById(R.id.toasted);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView toastfish = (TextView) myDialog.findViewById(R.id.dialog_fish_id);
            Toast.makeText(mContext,"Parameters are now set for " + toastfish.getText().toString(), Toast.LENGTH_SHORT).show();
            // here upon clicking this button click we want to match the details in this dialog to another tab. Kindly watch the link above :)
        }
    });



    vHolder.fish_choices.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView dialog_fish = (TextView) myDialog.findViewById(R.id.dialog_fish_id);
            TextView dialog_sciname = (TextView) myDialog.findViewById(R.id.dialog_sciname_id);
            ImageView dialog_image = (ImageView) myDialog.findViewById(R.id.dialog_image_id);
            dialog_fish.setText(mData.get(vHolder.getAdapterPosition()).getFish());
            dialog_sciname.setText(mData.get(vHolder.getAdapterPosition()).getSciname());
            dialog_image.setImageResource(mData.get(vHolder.getAdapterPosition()).getImage());
            myDialog.show();
        }
    });
    return vHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    holder.tv_fish.setText(mData.get(position).getFish());
    holder.tv_sciname.setText(mData.get(position).getSciname());
    holder.img.setImageResource(mData.get(position).getImage());

}


@Override
public int getItemCount() {
    return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

    private LinearLayout fish_choices;
    private TextView tv_fish;
    private TextView tv_sciname;
    private ImageView img;

    public MyViewHolder(View itemView) {
        super(itemView);
            fish_choices = (LinearLayout) itemView.findViewById(R.id.choices);
            tv_fish = (TextView) itemView.findViewById(R.id.textView1);
            tv_sciname = (TextView) itemView.findViewById(R.id.textView2);
            img = (ImageView) itemView.findViewById(R.id.image);

    }
}

}

Code for overview tab(This tab we want to match the content)

public class overview extends Fragment {

View v2;


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

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

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment overview.
 */
// TODO: Rename and change types and number of parameters
public static overview newInstance(String param1, String param2) {
    overview fragment = new overview();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v2 = inflater.inflate(R.layout.fragment_overview, container, false);
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
    String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
    String currentTime = time.format(calendar.getTime());


    TextView textViewDate =(TextView) v2.findViewById(R.id.date_id);
    textViewDate.setText(currentDate);
    TextView textViewTime =(TextView) v2.findViewById(R.id.time_id);
    textViewTime.setText(currentTime);



    return v2;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

Code for Specie tab (this tab we want to refer)

public class specie extends Fragment {

View v;

private RecyclerView myrecyclerview;
private List<specieList> lstspecie;


// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;



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

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment specie.
 */
// TODO: Rename and change types and number of parameters
public static specie newInstance(String param1, String param2) {
    specie fragment = new specie();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

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

    lstspecie = new ArrayList<>();
    lstspecie.add(new specieList("Nile Tilapia", "Oreochromis niloticus", R.drawable.tilapia));
    lstspecie.add(new specieList("Ayungin (Silver Perch)", "Bidyanus bidyanus", R.drawable.ayungin));
    lstspecie.add(new specieList("Sugpo (Tiger Prawn)", "Penaeus monodon", R.drawable.hipon));
    lstspecie.add(new specieList("Hito (Catfish)", "Siluriformes", R.drawable.hito));
    lstspecie.add(new specieList("Giant Gourami", "Osphronemus goramy", R.drawable.giant));
    lstspecie.add(new specieList("Bangus (Milkfish)", "Chanos chanos", R.drawable.bangus));


    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_specie, container, false);
    myrecyclerview = (RecyclerView)v.findViewById(R.id.specie_recycleview);
    RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getContext(), lstspecie);
    myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
    myrecyclerview.setAdapter(recyclerAdapter);
    return v;
}


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

Thank you!

最佳答案

使用面向对象的方法

您可以在 Activity 中编写一个函数,然后可以从中调用 Fragment 的方法,因为当您初始化它时,您的 Activity 中就有 Fragment 的引用

在您的 Activity 中

   class yourActivity ... {

       // your other methods

      public void callFragmentMethod(String params) {

           // here call your fragment's method
           fragment.method(params);
      }

   }

现在在您的 fragment 中

   class yourFragment ... {

       // your other methods

      public void method(String params) {

           // here call your fragment's method
           here do whatever you want to do it with params
      }

   }

现在您可以从另一个 fragment 或从适配器调用 Activity 的方法,无论您想要什么

从 Fragment 你可以这样调用

((yourActivity)getActivity()).callFragmentMethod(params);

来自适配器

((yourActivity)context).callFragmentMethod(params);

关于java - 单击按钮后如何将 RecyclerView 适配器中的对话框的详细信息与另一个 fragment 相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765731/

相关文章:

java - 如何初始化旋转适配器?

Java - 将默认浮点常量类型从 double 更改为 float

android - Cordova 5.0.0 上的地理定位插件总是返回超时过期错误

android - jetpack accompanist 中的 launchPermissionRequest() 不执行任何操作

android - 在 Android Studio 中查看设备上的 SQLite 数据库

java - 如何让 Android 应用程序自动配置调试与发布值?

为音频文件创建 PNG 波形的 Java 程序

java - TestNG - 如何在 testng 自定义可通过电子邮件发送的报告中打印运行时 testng 参数?

java - 如何使用 JOOQ 在 FROM 子查询中添加 jsonb 函数

使用 Activity 实现的 Android Navigation Drawer