简体   繁体   English

Android Fragment的onClickListener未被调用

[英]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. 该片段是动态添加到UI的,并且我的应用程序适用于android 16及更高版本,因此我不必使用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. 我决定对MainActivity上的按钮和列表实现单击侦听器,因为这些触发了数据库查找,因此我希望将数据库查找放在一个位置。 so, I made a main activity that implements both: OnClickListener and OnItemClickListener and added the following methods: 因此,我进行了同时实现这两项的主要活动:OnClickListener和OnItemClickListener并添加了以下方法:

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: 在片段的setClickListener方法中,我设置了将传入的值保存为单击侦听器。片段的相关部分为:

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? 您不能将片段的侦听器直接放置到MainActivity.class中,因为创建的视图由片段拥有,而片段由活动动态拥有,对吗? So the same case was with me and I resolve like this : 所以我遇到了同样的情况,我这样解决:

MainActivity.class 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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