fragment_text.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TextFragment_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TextFragment">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/welcome" />
</FrameLayout>
TextFragment.java
package com.example.tourguide;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
* Use the {@link TextFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TextFragment extends Fragment
{
TextView textView;
// 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;
public TextFragment() {
// 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 TextFragment.
*/
// TODO: Rename and change types and number of parameters
public static TextFragment newInstance(String param1, String param2)
{
TextFragment fragment = new TextFragment();
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)
{
View view = inflater.inflate(R.layout.fragment_text, container,
false);
textView = view.findViewById(R.id.textView1);
return view;
}
public void updateTextView(int index)
{
if(index==0)
{
textView.setText(getResources().getString(R.string.set_Richmond));
}
else if(index==1)
{
textView.setText(getResources().getString(R.string.set_Vancouver));
}
else if(index==2)
{
textView.setText(getResources().getString(R.string.set_Toronto));
}
else if(index==3)
{
textView.setText(getResources().getString(R.string.set_New_West));
}
else if(index==4)
{
textView.setText(getResources().getString(R.string.set_Montreal));
}
else if(index==5)
{
textView.setText(getResources().getString(R.string.set_Aldergrove));
}
}
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction(int index);
}
}
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ListFragment_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListFragment">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="32dp" />
</FrameLayout>
package com.example.tourguide;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ListFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ListFragment extends Fragment implements
AdapterView.OnItemClickListener
{
ListView listView;
String[] array;
String listStr;
private OnFragmentInteractionListener listener;
// 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;
public ListFragment() {
// 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 ListFragment.
*/
// TODO: Rename and change types and number of parameters
public static ListFragment newInstance(String param1, String param2)
{
ListFragment fragment = new ListFragment();
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)
{
View view = inflater.inflate(R.layout.fragment_list, container,
false);
listView = view.findViewById(R.id.listView1);
array =
getResources().getStringArray(R.array.string_array_cities);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
index, long id)
{
if(listener != null)
{
listener.onFragmentInteraction(index); //pass index of array
}
}
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction(int index);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout_1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView1_list"
android:name="com.example.tourguide.ListFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2_text"
android:name="com.example.tourguide.TextFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_text" />
</LinearLayout>
package com.example.tourguide;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements
TextFragment.OnFragmentInteractionListener,
ListFragment.OnFragmentInteractionListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onFragmentInteraction(int index)
{
FragmentManager manager = getSupportFragmentManager();
TextFragment textFragment =
(TextFragment)manager.findFragmentById(R.id.fragmentContainerView2_text);
textFragment.updateTextView(index);
}
}
strings.xml
<resources>
<string name="app_name">Tour Guide</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="text_box">default</string>
<string name="welcome">Welcome to the Tour Guide App</string>
<string name="set_Richmond">Richmond is a quite city with many nice
hidden scenic places. A small town named Steveston is found on the
south
west part
of the city and is well known for having good seafood and nice
places to visit like Garry Point and the west and south dykes.
</string>
<string name="set_Vancouver">Vancouver is one of Canadas biggest
cities and has a lot to offer like the malls and many towers,
beaches,
parks.</string>
<string name="set_Toronto">Toronto is Canadas biggest city on the
east coast and home to the TSX and Maple Leafs.</string>
<string name="set_New_West">New West is a city east of Vancouver but
close by and has nice parks and ice rinks like Moody Park and Queens
Park.
Queens Park home of the New West Royals.</string>
<string name="set_Montreal">Monteal is famous for their smoked meat
and they speak french.</string>
<string name="set_Aldergrove">Aldergrove is a quite town and is east
of Langley not too far and is a nice place.</string>
<string-array name="string_array_cities">
<item>Richmond</item>
<item>Vancouver</item>
<item>Toronto</item>
<item>New West</item>
<item>Montreal</item>
<item>Aldergrove</item>
</string-array>
</resources>
这是我的android应用程序的代码,它是一个演示如何在一个活动中与多个片段通信的应用程序。但是,在选择列表视图的一个项目后,文本片段中的文本视图不会被更新。当列表视图中的一个项目被选中时,文本视图应该更新,但它没有更新,也没有崩溃。
一切似乎都很好,只需要更改几行代码。
在TextFragment中,您需要将textView初始化从onCreate(…({}方法删除到onCreateView(…(,如下所示:
@Override
public void onCreate(....){
.......
.....
textView = (TextView)getActivity().findViewById(R.id.textView1); //remove this line of code.
}
@Override
public View onCreateView(......){
View view = inflater.inflate(R.layout.fragment_text, container, false);
textView = view.findViewById(R.id.textView1); //you need to initialize textview here.
return view;
}
并且,在ListFragment中,您需要删除从onCreate(..({}到onCreateView(..(}的所有初始化代码,如下所示:
@Override
public void onCreate(...){
.......
....
...
//remove all this code to another method.
/*listView = (ListView)getActivity().findViewById(R.id.listView1);
//Resources res = getResources();
array = getResources().getStringArray(R.array.string_array_cities);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);*/
}
@Override
public View onCreateView(.....){
View view = inflater.inflate(R.layout.fragment_list, container, false);
listView = view.findViewById(R.id.listView1);
array = getResources().getStringArray(R.array.string_array_cities);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
return view;
}
// Add this code in your updated code.you need to override onAttach method
@Override
public void onAttach(@NonNull @NotNull Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener){
listener = (OnFragmentInteractionListener) context;
}
else {
throw new RuntimeException(context.toString());
}
}