gpt4 book ai didi

java - 两个fragment之间如何通信

转载 作者:行者123 更新时间:2023-12-02 09:04:22 24 4
gpt4 key购买 nike

我正在尝试创建一个包含两个 fragment 的 Android 应用程序。一个包含各个城市的列表,另一个包含这些城市的详细信息。我是移动开发新手,很难弄清楚如何在它们之间进行通信。到目前为止,我已将城市列表存储在 fragment 中,但仍需要弄清楚如何在单击其中一个列表项时显示描述。另外,我不确定描述将存储在哪里或如何访问它。任何帮助都会非常感谢。

城市详细信息文件:

public class cityDetails extends Fragment {
// 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 cityDetails() {
// 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 cityDetails.
*/
// TODO: Rename and change types and number of parameters
public static cityDetails newInstance(String param1, String param2) {
cityDetails fragment = new cityDetails();
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) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_city_details, container, false);
}

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

}

cityListFragment 文件:

public class citiesFragment extends Fragment {

// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;

/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public citiesFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static citiesFragment newInstance(int columnCount) {
citiesFragment fragment = new citiesFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}

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

if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cities_list, container, false);

if (view instanceof RecyclerView) {
Context context = view.getContext();

String[] cities = context.getResources().getStringArray(R.array.Cities);
List<DummyItem> mValues = new ArrayList<>();
for(int i = 0; i < cities.length; i++) {
mValues.add(new DummyItem(String.valueOf(i + 1), cities[i], ""));
}

RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}

recyclerView.setAdapter(new MycitiesRecyclerViewAdapter(mValues, mListener));
}

return view;
}


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

@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 OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(DummyItem item);
}
}

主 Activity 文件:

public class MainActivity extends AppCompatActivity implements citiesFragment.OnListFragmentInteractionListener, cityDetails.OnFragmentInteractionListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onListFragmentInteraction(DummyContent.DummyItem item) {

}

@Override
public void onFragmentInteraction(Uri uri) {

}
}

最佳答案

这些是由 CodingInFlow 制作的精彩视频,他通过示例解释了如何操作

https://www.youtube.com/watch?v=ACK67xU1Y3s

https://www.youtube.com/watch?v=i22INe14JUc

关于java - 两个fragment之间如何通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59938970/

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