我知道这个问题已经被提出但我找不到合适的答案来解决我的问题。我希望你们能帮助我。
我试图创建两个片段。 Fragment01包含文本视图,编辑文本和按钮。 Fragment02包含listview。单击Fragment01中的按钮时,此片段的编辑文本中的字符串将发送到Fragment02。 Fragment02中的列表将获取此字符串并将其添加到列表中。
这是我的代码: main_screen.xml
val url = routes.Application.index().absoluteURL
wsClient.url(url).post[JsValue](json)
fragment_01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.example.user.myapplication.Fragment01"/>
<fragment
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.example.user.myapplication.Fragment02"/>
</LinearLayout>
fragment_02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Enter Names: "
android:textColor="@android:color/white"/>
<EditText
android:id="@+id/entered_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:focusable="true"
android:background="@android:color/white"
android:textColor="@android:color/black"/>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add To List"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Fragment01.java
public class MainScreen extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
}
}
Fragment02.java
public class Fragment01 extends Fragment {
private EditText editText;
private Button button;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_01, container, false);
editText = (EditText)view.findViewById(R.id.entered_text);
button = (Button)view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment02 fragment02 = new Fragment02();
fragment02.addNewItem(editText.getText().toString());
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.list, fragment02);
transaction.commit();
}
});
return view;
}
答案 0 :(得分:0)
在fragment02.xml中,在ListView中更改:
android:id="@+id/list"
到
android:id="@android:id/list"
查看docs会显示:
ListFragment具有由单个列表视图组成的默认布局。但是,如果需要,可以通过从onCreateView(LayoutInflater,ViewGroup,Bundle)返回自己的视图层次结构来自定义片段布局。为此,您的视图层次结构必须包含一个ID为#34; @android:id / list&#34;的ListView对象。 (或列出代码中的代码)
试一试