我需要在onItemClick方法中用另一个片段替换片段。 这就是我想要的:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TipoRestaurante rteActual = (TipoRestaurante) adapter.getItem(position);
String msg = "Has elegido el tipo " + rteActual.getNombre_tipo();
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
Fragment newFragment = new TabFragmentComer();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerView, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
但我得到一个例外:
09-28 23:51:02.310 14895-14895/com.solinpromex.elpasojuarezexperience E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.solinpromex.elpasojuarezexperience, PID: 14895
java.lang.IllegalArgumentException: No view found for id 0x7f0d009b (com.solinpromex.elpasojuarezexperience:id/containerView) for fragment TabFragmentComer{42909b40 #1 id=0x7f0d009b}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
我到达活动的当前片段,如下所示:
if (menuItem.getItemId() == R.id.nav_item_comer) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragmentComerTiposRestaurante()).commit();
}
欢迎任何帮助
编辑:MainActivity xml布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/rojomodesto"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="El Paso - Juárez Experience" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/shitstuff"
app:itemTextColor="@color/black"
app:menu="@menu/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
答案 0 :(得分:1)
您的片段中应该有一个界面,如下所示:
public class MyFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = ...;
return view;
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TipoRestaurante rteActual = (TipoRestaurante) adapter.getItem(position);
String msg = "Has elegido el tipo " + rteActual.getNombre_tipo();
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
Fragment newFragment = new TabFragmentComer();
if(mListener != null){
mListener.onFragmentInteraction(newFragment);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Fragment fragment);
}
}
您的活动应该实现该界面:
MyActivity extends Activity implements OnFragmentInteractionListener{
//your activity methods...
@Override
public void onFragmentInteraction(Fragment fragment) {
//here you have that fragment and can change it with transactions
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerView, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
将对象传递给另一个片段:
如果片段只是您在上面作为newFragment创建的片段,则可以按如下方式向其添加参数:
Fragment newFragment = new TabFragmentComer();
Bundle args = new Bundle();
args.putInt("myIntLabel", 2);
args.putString("myStringLabel", "my sample string");
//and you can add all you want to that bundle like this
newFragment.setArguments(args);
然后在你的片段onCreateView或onCreate方法中你可以得到这样的包:
...
Bundle args = getArguments();
int myInt = args.getInt("myIntLabel", (int defaultValue)); // returns 2
int myString = args.getString("myStringLabel", (String defaultString)); // returns "my sample string"
如果您没有在此片段中创建片段,则可以将该接口方法的另一个参数添加为Bundle args
,并将该包添加到您创建的片段中,如下所示:fragment.setArguments(args)
答案 1 :(得分:0)
尝试关注替换片段。
Products_Fragment df = new Products_Fragment();//this is your fragment.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, df);
ft.commit();