简体   繁体   中英

Android Fragment's onClickListener not called

I have a fragement that has a header with two buttons and a list on it. I want to get events on those items. The fragment is dynamically added to the UI, and my app is for android 16 and upwards, so I don't have to use the support lib. I decided to implement the click listeners to the both the buttons and the list on the MainActivity since these fire off database look ups, and I would like to keep my db lookups in one place. so, I made a main activity that implements both: OnClickListener and OnItemClickListener and added the following methods:

public void onClick(View v) {
Toast.makeText(getApplicationContext(), "List Click", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "List Click"+arg2, Toast.LENGTH_SHORT).show();
}

To call the fragment I do:

//get a list of the data
Fragment sideFormations=sideFragment.newInstance(list);
sideFormations.setClickListeners(this,this); //<--important bit
Toast.makeText(getApplicationContext(), "listeners set", Toast.LENGTH_SHORT).show();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.side_frame,sideFormations);
ft.commit();

In the frag's setClickListener's method, I set save the values passed in as click listeners.and the relevant sections of the frag are:

onCreateView: //nflate the views, setAdapter for the list, ensure clickability of the views
onStart: //assign click listeners which were passed in to the list, and to the header. 

I still can't catch clicks in the main activity ie. zero but no errors. Any ideas will be highly welcome, and appreciated. Thank you.

EDIT: Adding code where I define the listeners for the views.

//This is the latest iteration. As I mentioned, I have tried to set these at different places in the frag's lifecycle without much success:
public void onStart() {
    super.onStart();
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Log.d("CLICK-VIEW","List Clicked");
            main.onItemClick(arg0,arg1,arg2,arg3);
        }               
    });
    mHeaderView.setOnClickListener(new OnClickListener() {          
        @Override
        public void onClick(View v) {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            Log.d("CLICK-VIEW","Header Clicked");
            //main.onClick(v);
        }
    });
}

You can not put listeners for fragment directly to MainActivity.class because the view that created are owned by fragment and fragment is owned by activity dynamically right? So the same case was with me and I resolve like this :

MainActivity.class

public class MainActivity extends FragmentActivity{

    //Create two public methods

   public void onFragmentListClick(params whtever you want to pass){
     //will call from fragment when list item click
     Toast.makeText(getApplicationContext(), "List Click", Toast.LENGTH_SHORT).show();
   }

   public void onFragmentButtomClick(params whtever you want to pass){
     //will call from fragment when button click
     Toast.makeText(getApplicationContext(), "Button Click", Toast.LENGTH_SHORT).show();
   }
}

Fragment.class

public class MyFragment extends Fragment{
  private Button b;
  private ListView lst;

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

View fragmentView = inflater.inflate(setLayoutId(), container, false);
b = (Button)fragmentView.findViewById()     
lst = (ListView)fragmentView.findViewById()     

b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ((MainActivity) getActivity()).onFragmentButtomClick(..);
            }
        });

        lst.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                                ((MainActivity) getActivity()).onFragmentListClick(..);         
            }
        });

        return fragmentView;
    }
}

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