简体   繁体   中英

setting onClickListener after layout change not working

I have created 2 different layouts for the same Fragment. One is for Portrait and second for Landscape . All the buttons ids are the same. My fragment implements onClickListener . When I start my application, the onClick is working great. Doesn't matter if I start in landscape or portrait. When I change the orientation, the layout switches, but the onClick isn't working anymore. It only works at start up.

public class Navigation extends Fragment implements View.OnClickListener {

private ImageButton addButton;
private ImageButton viewButton;
private ImageButton albumButton;
private ImageButton infoButton;

private Communicator comm;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.navigation_fragment_layout, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    addButton = (ImageButton) getActivity().findViewById(R.id.add_button);
    addButton.setOnClickListener(this);
    viewButton = (ImageButton) getActivity().findViewById(R.id.view_button);
    viewButton.setOnClickListener(this);
    albumButton = (ImageButton) getActivity().findViewById(R.id.album_button);
    albumButton.setOnClickListener(this);
    infoButton = (ImageButton) getActivity().findViewById(R.id.info_button);
    infoButton.setOnClickListener(this);

    comm = (Communicator) getActivity();
    }

@Override
public void onStart() {
    super.onStart();

}

@Override
public void onClick(View v) {
    comm.onFragmentTouch(v.getId());
}
}

使用onViewCreated回调代替onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addButton = (ImageButton) getActivity().findViewById(R.id.add_button);
addButton.setOnClickListener(this);
viewButton = (ImageButton) getActivity().findViewById(R.id.view_button);
viewButton.setOnClickListener(this);
albumButton = (ImageButton) getActivity().findViewById(R.id.album_button);
albumButton.setOnClickListener(this);
infoButton = (ImageButton) getActivity().findViewById(R.id.info_button);
infoButton.setOnClickListener(this);

comm = (Communicator) getActivity();
}

here you are taking views from getActivity(); why youa re getting them from getActivity(). Take them from the below way

public class Navigation extends Fragment implements View.OnClickListener {

 private ImageButton addButton;
 private ImageButton viewButton;
 private ImageButton albumButton;
 private ImageButton infoButton;

private Communicator comm;
private View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

view = inflater.inflate(R.layout.navigation_fragment_layout, container, `false);`
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addButton = (ImageButton) view.findViewById(R.id.add_button);
addButton.setOnClickListener(this);
viewButton = (ImageButton) view.findViewById(R.id.view_button);
viewButton.setOnClickListener(this);
albumButton = (ImageButton) view.findViewById(R.id.album_button);
albumButton.setOnClickListener(this);
infoButton = (ImageButton) view.findViewById(R.id.info_button);
infoButton.setOnClickListener(this);

comm = (Communicator) this;
}

Let me know this works or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM