代码之家  ›  专栏  ›  技术社区  ›  matip

在viewpager内的fragment中未触发onclick事件

  •  11
  • matip  · 技术社区  · 8 年前

    我的碎片在里面 ViewPager 我想要其中一个可以点击的 RelativeLayout . 我正在使用数据绑定:

    <data>
    
            <variable
                name="handlers"
                type="com.matip.presenters.MyPresenter" />
    </data>
    
    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="@{() -> handlers.onLayoutClick()}">
    
                    ...
                    ...
                    ...
    
    </RelativeLayout>
    

    我的创建片段视图:

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_layout, container, false);
            presenter = new MyPresenter();
            mBind.includedLayout.setHandlers(presenter);
            return mBind.getRoot();
        }
    

    但是 onLayoutClick 没有被呼叫。它与viewpager有什么关系吗?它需要点击而不是点击里面的片段吗?如果有的话,我该怎么解决?

    编辑

    My ViewPager Adapter:
    private class MyViewPagerAdapter extends FragmentStatePagerAdapter {
            public MyViewPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                switch(position) {
                    case 0:
                        return MainInfoFragment.newInstance(parkingId);
                    case 1:
                        return MainDescFragment.newInstance(parkingId);
                    case 2:
                        return MainServicesFragment.newInstance(parkingId);
                    default:
                        return null;
                }
    
    
            }
    
            @Override
            public int getCount(){
                return 3;
            }
    
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch(position){
                    case 0: return getContext().getString(R.string.info_title);
                    case 1: return getContext().getString(R.string.desc_title);
                    case 2: return getContext().getString(R.service_title);
                    default: return null;
                }
            }
        }
    
    4 回复  |  直到 8 年前
        1
  •  2
  •   Bertram Gilfoyle    8 年前

    得到 onClick 属性在要工作的XML中,该方法需要在活动中。对于片段,首选方法是以编程方式设置侦听器。如果该方法在活动中,XML方法将起作用,但是如果该方法需要对不同的片段进行不同的行为,那么它将变得复杂。

    我也不明白你的意思 android:onClick="@{() -> handlers.onLayoutClick()}" . 通常看起来像 android:onClick="onLayoutClick" . 定义如下

    public void onLayoutClick(View view) {
        //Your code
    } 
    

    还有一种可能性:您可能已将OnTouchListener设置为覆盖单击的视图。如果你想同时触摸和点击监听器,那么尝试 GestureDetector .

        2
  •  1
  •   Aishik kirtaniya    8 年前

    可以对onCreateView()方法执行以下操作

    oncreate(....)
    {.....
    View v = inflater.inflate(R.layout.fragment_layout,container,false);
    RelativeLayout l = v.findViewById(R.id.rl);
    l.setOnClickListener(this);
    .....}
    
    @Override
    public void onClick(View v) 
    {
         if(v.getId == R.id.rl) 
         {
         //do your work
         }  
    }
    

    把布局改成这个

    <RelativeLayout
    android:id="@+id/rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    ...
    ...
    ...
    </RelativeLayout>
    

    如果编译器显示错误,请在ClickListener上按Alt+Enter。

        3
  •  0
  •   ezzou    8 年前

    尝试使用 方法参考 而不是 侦听器绑定 符号,你的 XML 会是这样的:

    android:onClick="@{handlers::onLayoutClick}
    

    还有你的 节目主持人 将如下所示:

    public class MyHandlers {
    public void onLayoutClick(View view) { ... }
    }
    

    !\注意,方法的签名应该接受一个视图作为参数,并返回void和public

    编辑 :您还可以通过添加以下内容来确保RelativeLayout的所有子级都没有得到单击而不是单击:

    android:descendantFocusability="beforeDescendants"
    
        4
  •  0
  •   Man    8 年前

    你怎么知道的 包含的布局 绑定类的方法?
    我哪儿也找不到,这就是我犯的错误, 相反,你应该使用 mBind.setHandlers(presenter);
    一开始我的错误是 binding.sethandler 方法,但在构建或运行之后,它得到了解决并按预期工作。
    和
    您的MyPresenter类应该具有精确的方法签名。 对于 处理程序.onlayoutclick()) 应该是:

    public void onLayoutClick(){
    ....}
    

    viewpager没什么问题,我和你一样试过。
    同时检查 MBN 的类型,根据您的代码,我想应该是 碎片分布 .

    片段:

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            FragmentLayoutBinding mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_layout, container, false);
            MyPresenter presenter = new MyPresenter();
            mBind.setHandlers(presenter);
            return mBind.getRoot();
        }
    

    我的演示者:

    public class MyPresenter{
    
    //using listener binding
    public void onLayoutClick(){
    //do something..
    }
    
    
    //using method reference
    public void onButtonClicked(View view){
    
    // do something
    }
    
    }
    

    碎片布局:

    <layout...>
    <data>
    
            <variable
                name="handlers"
                type="com.matip.presenters.MyPresenter" />
    </data>
    
    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="@{() -> handlers.onLayoutClick()}">
    
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:text="Using Method Ref"
                android:onClick="@{handlers::onButtonClicked}"/>            
    
    </RelativeLayout>
    </layout>