在单击按钮时,如何将对话框的详细信息与另一个片段相匹配



我是Android编码的新手,我在这里和YouTube上尝试了所有可能的解决方案,但仍在努力。我只想在按下按钮时如何将片段对话框的数据与另一个片段匹配。我们正在研究一个简单的项目。:(

这是我们想做的。 https://i.stack.imgur.com/70nxe.jpg

我们想在单击"设置参数"按钮

时更改将匹配对话框的名称

这是我们在线发现的区别是我们有一个对话框按钮https://www.youtube.com/watch?v=zxogg2xtjzuhttps://www.youtube.com/watch?v=69c1ljfdvl0

这是代码 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);
    }
}

}

概述选项卡的代码(此选项卡我们要匹配内容(

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);
}

}

Specie Tab的代码(我们要引用此选项卡(

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);
}

}

谢谢!

使用面向对象的方法

您可以在活动中写下函数,可以从中调用片段的方法,因为您在活动初始化时将片段的引用如

在您的活动中

   class yourActivity ... {
       // your other methods
      public void callFragmentMethod(String params) {
           // here call your fragment's method
           fragment.method(params);
      }
   }

现在在片段中

   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
      }
   }

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

从片段中,您可以像

一样称呼
((yourActivity)getActivity()).callFragmentMethod(params);

来自适配器

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

在下面的方法下方调用按钮单击..

     public void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = 
        fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameContainer, fragment);
        fragmentTransaction.addToBackStack(fragment.toString());
        fragmentTransaction.commit();
    }

Pass FRAMECONTAINER 在您的 Adapter的构造函数中

new RecyclerViewAdapter(context, list, R.id.frameContainer);

最新更新