我正在尝试从活动中调用片段,但片段布局不会替换活动,就像在其上构建片段一样,因此它们都会出现。如何更换它,它不起作用 这是
activity.java
public class AllTasks extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_tasks);
final Button add;
final EditText text;
final ListView list;
add = (Button) findViewById(R.id.addbutton);
list = (ListView) findViewById(R.id.listView);
// public void onClick(View view) {
ParseObject showlist = new ParseObject("ShowAllTasks");
showlist.put("TaskNames", showlist);
showlist.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
Toast.makeText(AllTasks.this, "Saved", Toast.LENGTH_LONG).show();
ParseQuery<ParseObject> query = ParseQuery.getQuery("ShowAllTasks");
// query.whereEqualTo("playerName", "Dan Stemkoski");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> tasklist, ParseException e) {
if (e == null) {
ArrayList<String> array = new ArrayList<String>();
for (ParseObject j : tasklist) {
array.add(j.getString("TaskNames"));
}
ArrayAdapter adapter = new ArrayAdapter(AllTasks.this, android.R.layout.simple_list_item_1, array);
list.setAdapter(adapter);
// Log.d("score", "Retrieved " + scoreList.size() + " scores");
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment frag;
FragmentManager fm = AllTasks.this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
frag = new AddTaskFragment();
//AddTaskFragment llf = new AddTaskFragment();
ft.replace(R.id.alltasks, frag);
ft.addToBackStack(null);
ft.commit();
}
//Intent myIntent = new Intent(AllTasks.this , AddTaskFragment.class);
//startActivity(myIntent);
});
}
// });
}
这是 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="info.androidhive.slidingmenu.AllTasks"
android:id="@+id/alltasks">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="YourTasks :"
android:id="@+id/textViewyourtasks"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="386dp"
android:id="@+id/listView"
android:clickable="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/deletebutton"
android:layout_alignEnd="@+id/deletebutton"
android:layout_gravity="center_horizontal|bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/addbutton"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/deletebutton"
android:layout_gravity="right|top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/deletebutton"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textViewyourtasks"
android:layout_gravity="center_horizontal|top" />
片段
public class AddTaskFragment extends Fragment {
public AddTaskFragment() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_task, container, false);
Button add;
final EditText tasktext;
final EditText taskdate;
final EditText tasknote;
taskdate = (EditText) rootView.findViewById(R.id.editTextDate);
tasktext = (EditText) rootView.findViewById(R.id.editTexttaskname);
tasknote = (EditText) rootView.findViewById(R.id.editTextnote);
add = (Button) rootView.findViewById(R.id.addbutton);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ParseObject showlist = new ParseObject("ShowAllTasks");
showlist.put("TaskNames", tasktext.getText().toString());
showlist.put("Date", taskdate.getText().toString());
showlist.put("Note", tasknote.getText().toString());
showlist.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
taskdate.setText("");
tasknote.setText("");
tasktext.setText("");
Toast.makeText(getActivity(), "Saved", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getActivity(), AllTasks.class);
getActivity().startActivity(myIntent);
}
});
}
}
);
return rootView;
}
}
fragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.slidingmenu.AddTaskFragment"
android:id="@+id/fo">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:longClickable="false">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTextDate"
android:hint="Enter task date "
android:layout_gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextnote"
android:hint="Enter any note "
android:layout_gravity="left"
android:layout_alignBottom="@+id/editTexttaskname"
android:layout_alignEnd="@+id/editTextDate" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editTexttaskname"
android:hint="Enter task name here "
android:layout_below="@+id/editTextDate"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/AddButton"
android:id="@+id/addbutton"
android:layout_gravity="center_horizontal" />
</LinearLayout>
任何人都可以帮我找到我的代码有什么问题吗?
答案 0 :(得分:0)
我希望我能正确理解你的目标。 如果要同时显示Activity和Fragment的当前内容,则应在根RelativeLayout中的Activity布局文件中添加另一个布局,并从代码中将Fragment添加到该布局。