没有调用Android Fragment onClickListener

时间:2013-08-26 03:22:37

标签: java android android-fragments onitemclicklistener

我有一个包含两个按钮和一个列表的标题。我想在这些项目上获取活动。该片段被动态添加到UI,我的应用程序适用于Android 16及更高版本,因此我不必使用支持库。 我决定将Click监听器实现到MainActivity上的按钮和列表,因为这些会触发数据库查找,我想将我的数据库查找保存在一个地方。所以,我做了一个实现两者的主要活动: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();
}

要调用我做的片段:

//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();

在frag的setClickListener方法中,我设置保存作为click侦听器传入的值。并且frag的相关部分是:

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. 

我仍然无法捕捉主要活动中的点击次数,即。零,但没有错误。任何想法都将受到高度欢迎,并表示赞赏。谢谢。

编辑:添加代码,我为视图定义了侦听器。

//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);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

您不能将片段的侦听器直接放到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;
    }
}