我有一个名为Frags的MainActivity和Fragment,它本身有一个名为CardFrontFragment的片段。
我在这个片段(子片段)上注册一个OnClickListener,在这个OnClickListener的OnClick方法中,我用CardbackFragment片段替换这个片段(CardFrontFragment)。我也在这个片段(CardbackFragment)上注册一个OnClickListener,以便在这个片段和子片段之间再次切换。
OnClickListener在第一个片段(CardfrontFragment)工作,第二个片段显示但现在通过点击第二个片段没有任何反应,OnClickListener不能与第二个片段一起工作,这是杀了我,请帮助我!
代码:
MainActivity.java
public class MainActivity extends ActionBarActivity {
public static Frags frag=new Frags();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, frag)
.commit();
}
}
}
Frags.java
public class Frags extends Fragment{
CardBackFragment back=new CardBackFragment();
CardFrontFragment front=new CardFrontFragment();
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view=inflater.inflate(R.layout.frag_layout, container, false);
getChildFragmentManager().beginTransaction()
.add(R.id.frag_container, back)
.commit();
return view;
}
public void Onflip(boolean iSback)
{
if(iSback)
{
getChildFragmentManager().beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_right_in, R.animator.card_flip_right_out)
.replace(R.id.frag_container, front)
.commit();
}
else
{
getChildFragmentManager().beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_right_in, R.animator.card_flip_right_out)
.replace(R.id.frag_container, back)
.commit();
}
}
}
CardFrontFragment
public class CardFrontFragment extends Fragment {
public CardFrontFragment() {
}
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_card_front, container, false);
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
MainActivity.frag.Onflip(false);
Log.i("front","onclick");
}
});
return view;
}
}
CardBackFragment
public class CardBackFragment extends Fragment {
public CardBackFragment() {
}
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_card_back, container, false);
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
MainActivity.frag.Onflip(false);
Log.i("back","onclick");
}
});
return view;
}
}
XML:
fragment_card_front.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0000ff" >
<Button
android:id="@+id/front_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
fragment_card_back.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<Button
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
frag_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/frag_container">
</LinearLayout>
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.demokhar.MainActivity"
tools:ignore="MergeRootFrame" />