【问题标题】:onClickListener is not working properly in fragmentonClickListener 在片段中无法正常工作
【发布时间】:2016-08-30 09:56:25
【问题描述】:

我在我的活动中添加了两个片段,片段 1 包含一个按钮,片段 2 包含一个 TextView,当我单击片段 1 中的按钮时,它的计数器会增加并在片段 2 的 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 文件

这是给片段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>

这是给片段B的

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

【问题讨论】:

  • 如何初始化communicator?找不到执行此操作的代码。
  • 感谢@karan,但我担心解决方案并非如此。
  • 我已经在MainActivity中初始化了communicator

标签: android android-fragments onclicklistener


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多