【发布时间】:2019-01-10 12:26:01
【问题描述】:
你能告诉我如何从一个列表片段LearnFragment开始另一个片段TrickFragment吗?
这是listfragment的代码:
public class LearnFragment extends ListFragment {
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.listview_item,R.id.TextBox, getResources().getStringArray(R.array.Titles)));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
mCurCheckPosition = index;
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fg_learn, new TrickFragment())
.commit();
}
}
这是它的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"
android:id="@+id/fg_learn"
tools:context=".LearnFragment">
</FrameLayout>
TrickFragment
public class TrickFragment extends Fragment {
public TrickFragment() {
// Required empty public constructor
}
public static TrickFragment newInstance(int index) {
TrickFragment fragment = new TrickFragment();
Bundle args = new Bundle();
args.putInt("index", index);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_trick, container, false);
return view;
}
}
及其xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fg_trick"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TrickFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fg_trick_textview"/>
</LinearLayout>
我提到我有一个名为 listview_item 的列表视图项(如果重要的话)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlayFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TextBox"/>
</FrameLayout>
【问题讨论】:
-
能否添加activity_layout.xml
-
我在评论中添加了
标签: java android xml listview android-fragments