我正在开发一个应用程序,其中我有详细活动,其中包含一个详细信息片段,一个详细信息片段包含一个4按钮,当我点击该按钮时,我想要移动到新片段,但我的片段没有替换因为我使用片段事务替换方法,请求为我提供更好的方法,并期待我的方法
这是细节片段
public class DetailActivityFragment extends Fragment{
TextView textView;
ImageView imageView;
TextView details;
Button button;
public DetailActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_detail, container, false);
Bundle getBundle = getActivity().getIntent().getExtras();
String name = getBundle.getString("NAME");
textView= (TextView) view.findViewById(R.id.bioname);
textView.setText(name);
imageView= (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(getBundle.getInt("IMAGE"));
Log.v("test", "images are coming");
button= (Button) view.findViewById(R.id.button_quickfacts);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new QuickFacts();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
Log.v("replace","fragment repalacd");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
fragment detail.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rahul.famousbiography.DetailActivityFragment"
tools:showIn="@layout/activity_detail"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bioname"
android:textSize="20dp"
android:gravity="center_vertical"
android:textColor="#141715"
android:layout_alignEnd="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/imageView" />
<ImageButton
android:layout_width="150dp"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:id="@+id/imageView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/bioname" />
<Button
android:id="@+id/button_quickfacts"
android:text="Quick Facts"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/biodetail"
android:text="Biography"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/imageView"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/exit"
android:text="Exit"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignTop="@+id/quotes"
android:layout_alignStart="@+id/biodetail"
android:layout_alignParentEnd="true" />
<Button
android:id="@+id/quotes"
android:text="Quotes"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_width="150dp"
android:layout_height="60dp"
android:background="@drawable/quick_facts"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginTop="43dp"
android:layout_below="@+id/button_quickfacts"
android:layout_alignParentStart="true" />
</RelativeLayout>
快速事实片段类,将通过点击
替换 public class QuickFacts extends Fragment {
TextView detailText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_quick_facts, container, false);
Bundle getBundle = getActivity().getIntent().getExtras();
String details=getBundle.getString("DETAIL");
detailText= (TextView) view.findViewById(R.id.fragment_add);
detailText.setText(details);
return view;
}
} 我的托管活动
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:minHeight="?actionBarSize"
/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_detail" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:1)
因为您在片段中使用此代码,这是不正确的。如果您在片段中有onClick,则必须将活动连接到您的片段。您可以在活动中创建界面和实现。你可以使用这些代码:
你的接口类:
public interface IViewClickDetailQuick {
public void onClickFragmentDetail();
public void onClickFragmentQuick();
}
在你的fragment类中添加以下方法:
private IViewClickDetailQuick mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener =
(IViewClickDetailQuick) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement IViewClickDetailQuick");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
在片段中的onClick方法实现中添加此代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClickFragmentQuick();
}
});
在您的活动类中,您必须实现IViewClickDetailQuick接口并在那里添加您的代码,如下所示:
@Override
public void onClickFragmentDetail() {
}
@Override
public void onClickFragmentQuick() {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new QuickFacts()).addToBackStack(null).commit();
}
答案 1 :(得分:0)
试试这个: -
Fragment frag= new QuickFacts();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, frag);
// when back button pressed come back to this Fragment...
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();