Android:使用触摸框架

时间:2015-05-26 04:35:07

标签: android imageview android-recyclerview ontouchevent ontouch

我正在尝试捕获ImageView项内的RecyclerView点击次数。我已经实施了RecyclerView.OnItemTouchListener并且手势检测器处理正常点击的动作事件并长按RecyclerView项目。

由于我希望在RecylcerView项目中的子视图上使用相同的触摸框架来处理触摸事件,因此我也将View.OnTouchListener设置为图像视图,从而覆盖了onTouch实现返回true,希望点击时ImageView可以使用触摸。

我故意在onInterceptTouchEvent RecyclerView总是从RecyclerView返回false,希望子视图首先处理事件,如果没有人消耗onTouchEvent的项目视图应处理ImageView处理期间点击的手势事件。

根据上述理解,我可以在点击RecyclerView时收到触摸事件。

问题在于,当我点击ImageView以外的RecyclerView项时,onTouchEvent' s RecyclerView根本没有被调用。我对触摸框架的理解是,如果子视图不处理触摸事件,onTouchEvent ERROR org.atmosphere.cpr.AsynchronousProcessor - Invalid request state. AsyncContext#startAsync not supported.应该有机会处理相同的事件。然而,这不是我能够实现的。

任何人都可以建议背后发生的事情以及处理上述点击事件的正确方法是什么?

1 个答案:

答案 0 :(得分:4)

这将解决您的问题:

public class OnItemClickListener implements View.OnClickListener {
    private int position;
    private OnItemClickCallback onItemClickCallback;

    public OnItemClickListener(int position, OnItemClickCallback onItemClickCallback) {
        this.position = position;
        this.onItemClickCallback = onItemClickCallback;
    }

    @Override
    public void onClick(View view) {
        onItemClickCallback.onItemClicked(view, position);
    }

    public interface OnItemClickCallback {
        void onItemClicked(View view, int position);
    }
}

获取' OnItemClickCallback'的实例接口并将其放入您的活动或片段中:

private OnItemClickListener.OnItemClickCallback onItemClickCallback = new OnItemClickListener.OnItemClickCallback() {
    @Override
    public void onItemClicked(View view, int position) {
        // switch(view.getId()) ...
    }
};

然后,将该回调传递给您的recyclerView:

recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(Arrays.asList("1", "2", "3"), onItemClickCallback));

最后,这将是你的适配器:

public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
    private List<String> mValues;
    private OnItemClickListener.OnItemClickCallback onItemClickCallback;

    public SimpleStringRecyclerViewAdapter(List<String> items, OnItemClickListener.OnItemClickCallback onItemClickCallback) {
        mValues = items;
        this.onItemClickCallback = onItemClickCallback;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mTextView = (TextView) view.findViewById(R.id.txt_title);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mTextView.setText(mValues.get(position));
        holder.mView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
        holder.mTextView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }
}