Can you show me please how to start another fragment TrickFragment
from a listfragment LearnFragment
?
this is the code for 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();
}
}
And this is its xml
(i dont think it gets executed)
<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;
}
}
and its 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>
I mention that i have a listview item ( if it matters) named 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>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateContainerFrame(new LearnFragment()); // show ListFragment.
}
public void updateContainerFrame(Fragment fragment){
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_frame, fragment)
.commit();
}
}
LearnFragment
public class LearnFragment extends ListFragment {
int mCurCheckPosition = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
inflater.getContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.Titles));
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
private void showDetails(int index) {
mCurCheckPosition = index;
if (getActivity() instanceof MainActivity)
((MainActivity) getActivity()).updateContainerFrame(TrickFragment.newInstance(index));
}
}
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;
}
}
activity_main.xml
<!--ViewPager -->
<!--<android.support.v4.view.ViewPager-->
<!--android:layout_height="fill_parent"-->
<!--android:layout_width="match_parent"-->
<!--android:id="@+id/view_pager"/>-->
<!--Use frame container instead of ViewPager-->
<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/container_frame"
tools:context=".MainActivity"/>
Complete example on Github .
Use this :
getSupportFragmentManager().beginTransaction()
.replace(R.id.fg_learn, TrickFragment.newInstance(index))
.commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.