当显示第二个片段时,如果在第一个片段中的按钮存在的位置(完全位置) - 那个旧按钮onClick被调用,类似透明按钮:),这应该不会发生在至少使用replace()。注意:第一个片段是TabLayout下的片段。
public class first_Fragment extends Fragment {
public first_Fragment ()
{
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview=inflater.inflate(R.layout.profile_fragment, container, false);
final View fragmentContainer = rootview.findViewById(R.id.container);
Button new_frament_btn=
(Button)rootview.findViewById(R.id.new_frament_btn);
new_frament_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment newFragment = new Second_Fragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
Button btn_toast=
(Button)rootview.findViewById(R.id.btn1);
btn_toast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "this is first fragment",
Toast.LENGTH_LONG).show();
}
});
return rootview;
}
}
第一个片段xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/profile_background_yellow1"
android:id="@+id/container"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/new_frament_btn"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@+id/setting"
android:layout_centerHorizontal="true"
android:src="@drawable/profile_friends" />
<Button
android:id="@+id/btn1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/profile_account_setting"/>
.............
/>
第二个片段代码
public class Second_Fragment extends Fragment {
public Second_Fragment ()
{
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
第二个片段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:background="#ffffff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="second_fragment"
android:textSize="40dp"
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:1)
在第二个片段布局的xml文件中添加android:clickable="true"
。
我想这会有所帮助。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />