onClickListener在片段中无法正常工作

时间:2016-08-30 09:56:25

标签: android android-fragments onclicklistener

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

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文件

这是针对fragmentA

<?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>

这是活动

<?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>

1 个答案:

答案 0 :(得分:0)

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

<?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>