4

我正在尝试OnItemClickListener在 a 中使用方法,ListView但它没有被调用,因为它ViewPager拦截了触摸事件。

我试过了

mPager.requestDisallowInterceptTouchEvent(false);

在我的FragmentActivity, 和

lista.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

在我的 onCreate 方法中,ListFragment但没有任何反应。(“lista”是我的ListView)。

这是OnItemClickListener方法代码:

lista = (ListView) view.findViewById(id.list);

lista.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //It's not being called
    }

});

几天前,我只使用Activities而不是Fragments(现在我需要它们,因为ViewPager)并且代码运行良好,所以我认为不需要 xml。

编辑添加更多代码:

片段活动

public class TabsFacturasActivity extends SherlockFragmentActivity {

private static class MyAdapter extends FragmentPagerAdapter {

    private String[] titles = { "VER FACTURAS", "VER CONSUMO", "INTRODUCIR LECTURA" };
    private final FragmentManager mFragmentManager;
    private Fragment mFragmentAtPos0;

    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        mFragmentManager = fragmentManager;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0: // Fragment # 0
            return new FacturasFragment();
        ...
        }
        return null;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public int getItemPosition(Object object)
    {
        if (object instanceof FacturasFragment && mFragmentAtPos0 instanceof DetallesFacturaFragment)
            return POSITION_NONE;
        return POSITION_UNCHANGED;
    }
}

列表片段

public class FacturasFragment extends ListFragment {

private ListView lista;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.activity_facturas, container, false);

        ...

        lista = (ListView) view.findViewById(id.list);

        lista.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
            //It's not being called
        }

        });

        return view;

    }

}
4

1 回答 1

1

我认为这个问题可能是由 listView 项目引起的。如果您的 listView 项目包含可点击的视图(按钮、复选框等),您的OnItemClickListener将不会被调用。

于 2013-09-02T20:07:37.880 回答