/*
 * Copyright 2014-2015 ieclipse.cn.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.ieclipse.af.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * // Provide a reference to the views for each data item
 * // Complex data items may need more than one view per item, and
 * // you provide access to all the views for a data item in a view holder
 *
 * @author Jamling
 */
public class AfViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    private AfRecyclerAdapter adapter;
    private AfRecyclerAdapter.OnItemClickListener mOnClickListener;
    private AfRecyclerAdapter.OnItemLongClickListener mOnLongClickListener;

    public AfViewHolder(View view) {
        super(view);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        itemView.setBackgroundResource(android.R.drawable.list_selector_background);
    }

    public Context getContext() {
        return itemView.getContext();
    }

    public void setAdapter(AfRecyclerAdapter adapter) {
        this.adapter = adapter;
    }

    public AfRecyclerAdapter getAdapter() {
        return adapter;
    }

    public void setOnClickListener(AfRecyclerAdapter.OnItemClickListener listener) {
        this.mOnClickListener = listener;
        itemView.setOnClickListener(this);
    }

    public void setOnLongClickListener(AfRecyclerAdapter.OnItemLongClickListener listener) {
        this.mOnLongClickListener = listener;
        itemView.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickListener != null) {
            mOnClickListener.onItemClick(getAdapter(), v, getLayoutPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnLongClickListener != null) {
            mOnLongClickListener.onItemLongClick(getAdapter(), v, getLayoutPosition());
        }
        return true;
    }
}

@@ -660,16 +660,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -751,17 +751,75 @@
 oid.
-view.View
+util.Log;%0Aimport android.view.View;%0A%0Aimport java.lang.reflect.Field
 ;%0A%0A/
@@ -1420,89 +1420,255 @@
    i
-temView.setOnClickListener(this);%0A        itemView.setOnLongClickListener(this);%0A
+f (!hasOnClickListener(view)) %7B%0A            itemView.setOnClickListener(this);%0A        %7D%0A        if (!hasOnLongClickListener(view)) %7B%0A            itemView.setOnLongClickListener(this);%0A        %7D%0A        if (itemView.getBackground() == null) %7B%0A    
     
@@ -1748,16 +1748,26 @@
 round);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2792,11 +2792,1284 @@
 e;%0A    %7D
+%0A%0A    private boolean hasOnClickListener(View view) %7B%0A        if (Build.VERSION.SDK_INT %3E= 15) %7B%0A            return view.hasOnClickListeners();%0A        %7D%0A        try %7B%0A            Field f = View.class.getDeclaredField(%22mListenerInfo%22);%0A            f.setAccessible(true);%0A            Object li = f.get(view);%0A            if (li != null) %7B%0A                f = li.getClass().getDeclaredField(%22mOnClickListener%22);%0A                f.setAccessible(true);%0A                Object l = f.get(li);%0A                return l != null;%0A            %7D%0A        %7D catch (Exception e) %7B%0A            Log.e(%22AfViewHolder%22, %22can't detect OnClickListener under API 15%22, e);%0A        %7D%0A        return false;%0A    %7D%0A%0A    private boolean hasOnLongClickListener(View view) %7B%0A        try %7B%0A            Field f = View.class.getDeclaredField(%22mListenerInfo%22);%0A            f.setAccessible(true);%0A            Object li = f.get(view);%0A            if (li != null) %7B%0A                f = li.getClass().getDeclaredField(%22mOnLongClickListener%22);%0A                f.setAccessible(true);%0A                Object l = f.get(li);%0A                return l != null;%0A            %7D%0A        %7D catch (Exception e) %7B%0A            Log.e(%22AfViewHolder%22, %22can't detect OnLongClickListener%22, e);%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

fix AfViewHolder onClick override issue