2016-08-30 55 views
0

我在我的活动中添加了两个片段,fragment1包含一个Button,fragment2包含一个TextView,当我点击fragment1中的按钮时,它的计数器递增并在fragment2的TextView中显示结果。 我正在处理saveInstanceState并在屏幕旋转的情况下保存计数器和文本。但仍然有错误..............错误是当我运行应用程序它工作正常,但只要我旋转屏幕它的onclick方法根本不会调用。我卡在这里请帮忙。onClickListener在片段中无法正常工作

public class FragmentA extends Fragment{ 

Button button; 
int counter; 
Communicator communicator; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_a,container,false); 
    button = (Button) view.findViewById(R.id.button); 
    if(savedInstanceState == null) 
     counter = 0; 
    else{ 
     counter = savedInstanceState.getInt("Counter",0); 
    } 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      counter++; 
      communicator.respond("Button was clicked "+counter+ " times"); 
     } 
    }); 
    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("Counter",counter); 
} 

public void setCommunicator(Communicator communicator){ 
    this.communicator = communicator; 
} 

interface Communicator{ 
    public void respond(String data); 
} 

}

我已经试过这两个选项中onCreateView()和onActivityCreate()按钮,监听绑定按钮的问题是存在的。

public class FragmentB extends Fragment{ 

TextView textView; 
String data; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_b, container, false); 
    if (savedInstanceState != null) { 
     textView = (TextView) view.findViewById(R.id.textView); 
     data = savedInstanceState.getString("text"); 
     textView.setText(data); 
    } 
    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    textView = (TextView) getActivity().findViewById(R.id.textView); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("text", data); 
} 

public void changeData(String data){ 
    this.data = data; 
    textView.setText(data); 
} 

}

这就是我使用这些片段的活性....

public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{ 

FragmentManager fragmentManager; 
FragmentTransaction fragmentTransaction; 
FragmentA fragmentA; 
FragmentB fragmentB; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    fragmentManager = getFragmentManager(); 
    fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentA = new FragmentA(); 
    fragmentB = new FragmentB(); 

    fragmentTransaction.add(R.id.fragment_container1,fragmentA,"Najam"); 
    fragmentTransaction.add(R.id.fragment_container2,fragmentB,"Najam"); 
    fragmentTransaction.commit(); 
    fragmentA.setCommunicator(this); 
} 

@Override 
public void respond(String data) { 
    //set the text of fragment_B 
    fragmentB.changeData(data); 
} 

}

下面是简单的XML文件

这是对于片段A

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00FFB0"> 

    <Button 
     android:layout_margin="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Click me" 
     android:id="@+id/button" /> 
</LinearLayout> 

这是fragmentB

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#AAA333"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_margin="10dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="you clicked the button 0 times" 
     android:id="@+id/textView" /> 
</LinearLayout> 

这是活动的FrameLayout

<?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:orientation="vertical" 
    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" 
    android:background="#FFBB00" 
    android:id="@+id/main_layout" 
    tools:context="com.najam.fragments1.MainActivity"> 

    <FrameLayout android:id="@+id/fragment_container1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <FrameLayout android:id="@+id/fragment_container2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

[方法在Android屏幕旋转来电]可能的复制(http://stackoverflow.com/questions/36112869/method-calls-on-android-screen-rotation) – karan

+0

你如何初始化'沟通'?找不到执行此操作的代码。 – Egor

+0

谢谢@karan但我恐怕解决方案并非如此。 – Najam

回答

0

使用片段。尝试以下操作。您可以适当调整帧和片段的位置。我有完整的工作来源相同的问题。如果需要,请告诉我。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    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.najam.fragments1.MainActivity"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:background="#99CC00" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="102dp" 
     android:layout_below="@+id/frameLayout2"> 

     <fragment 
      android:layout_width="match_parent" 
      android:layout_height="152dp" 
      android:name="com.najam.fragments2" 
      android:id="@+id/fragmentb" 
      android:layout_alignParentStart="true" 
      android:layout_marginTop="66dp" 
      tools:layout="@layout/fragment_container2" /> 
    </FrameLayout> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_blue_light"> 

     <fragment 
      android:layout_width="match_parent" 
      android:layout_height="228dp" 
      android:name="com.najam.fragments1" 
      android:id="@+id/fragmenta" 
      android:layout_centerVertical="true" 
      android:layout_alignParentStart="true" 
      tools:layout="@layout/fragment_container1" /> 
    </FrameLayout> 
</RelativeLayout>