当触摸监听器激活项目点击不工作时

时间:2016-01-09 14:23:30

标签: android listview ontouchlistener

我有一个实现OnItemClickListenerOnItemLongClickListener并显示列表视图的活动。我在列表适配器类中添加了OnTouchListener,以便用户可以在列表视图项上滑动。但是,当我运行我的应用时,列表视图项上的滑动有效,但onItemClickonItemLongClick不再有效。但是,如果我从适配器中删除OnTouchListener,则itemclick和itemlongclick可以正常工作。

这是我的代码

myactivity

public class InterActivity extends Activity implements 
OnItemClickListener, OnItemLongClickListener
{

    ListView listview;
    List<ParseObject> ob;
    AlertDialog dialog;
    ProgressDialog mProgressDialog;
    FinalAdapter adapter;
    TestAdapter tstapter;
     List<CodeList> codelist = null;
     SharedPreference shrdPreference;

     private int limit = 15;

     GestureDetectorCompat mDetector;
    View footerView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inter_layout);
        shrdPreference = new SharedPreference();

        //Execute RemoteDataTask AsyncTask

        new RemoteDataTask().execute();
    }

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        //my remote data task to perform async task//
    }

    @Override
    public void onItemClick(AdapterView<?> p1, View view, int position, long p4)
    {
        intent.putExtra("codetext",
                                    (codelist.get(position).getFinalCodeText()));
                    // Start SingleItemView Class
                     startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3)
    {
        //my method to perform when item is long pressed
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
    }

}

我的列表适配器的getview()

@Override
public View getView(final int position, View view, ViewGroup parent)
{
    final ViewHolder holder;
    if(view == null){
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listitem,null);
        holder.contaainer = (RelativeLayout) view.findViewById(R.id.container);
        holder.listHeading = (TextView) view.findViewById(R.id.beg_list_itemTextView);

        holder.listHash = (TextView) view.findViewById(R.id.listview_hashtags);
        holder.alphabetList = (ImageView) view.findViewById(R.id.beg_list_itemImageView);

        holder.favariteImage = (ImageView) view.findViewById(R.id.favbtn);
        holder.mDetector = new GestureDetectorCompat(context, new MyGestureListener(context, view));
        view.setTag(holder);

    }else{
        holder = (ViewHolder) view.getTag();
    }
    CodeList codes = (CodeList) getItem(position);
    holder.listHeading.setText(codeList.get(position).getListHeading());
    holder.listHash.setText(codeList.get(position).getListHashText());
    imageLoader.DisplayImage(codeList.get(position).getAlphabetimg(),
                             holder.alphabetList);

    holder.contaainer.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                holder.mDetector.onTouchEvent(event);
                return true;
            }
    });

    return view;
}

1 个答案:

答案 0 :(得分:2)

当您设置自定义OnTouchListener时,您正在拦截触摸事件,从而覆盖处理正常点击事件的默认触摸处理程序。

如果您想允许默认点击事件,则必须在return false实施中onTouch(),让事件传递到默认处理程序。

holder.contaainer.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        holder.mDetector.onTouchEvent(event);
        return false;
    }
});