Android自定义TextView不拦截父级的OnClickListener

时间:2013-07-16 13:40:37

标签: android android-layout android-linearlayout textview onclicklistener

我制作了一个自定义TextView,可以为每行文字绘制线条。

此自定义TextView与其他标准TextView和ImageView一起放置在LinearLayout中。

每当我点击普通的TextViews,ImageViews或自定义TextView外部时,点击都会被拦截。
但是,如果我单击自定义TextView,则不会截取单击。

请帮忙!!!

图片:

正常状态(最小化)
normal state(minimized)

最大化状态
Maximized state

点击此处未检测到
Click not detected here

代码:
NoteTextView.java

package harsha.notes.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class NoteTextView extends TextView {
    boolean isMaximized = false;

    public NoteTextView(Context canvas, AttributeSet attributeSet) {
        super(canvas, attributeSet);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();//var2.getDefaultDisplay().getWidth();
        int height = getHeight();//var2.getDefaultDisplay().getHeight();
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int scrollY = getScrollY();
        int scrollX = getScrollX();
        int lineHeight = getLineHeight();

        int grossWidth = width + scrollX;
        int grossHeight = height + scrollY;

        int netViewHeight = lineHeight + (grossHeight - paddingTop - paddingBottom);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        //paint.setColor(1722066084);
        paint.setColor(1711276032);

        float fontAdjustment = 15.826172F - (paint.getFontMetrics().bottom - paint.getFontMetrics().top);
        float baseline = (float)(scrollY + lineHeight - scrollY % lineHeight) - fontAdjustment;
        float linePadding;
        if(width != 800 && height != 480) {
            linePadding = 2;
        } else {
            linePadding = 4;
        }

        while(baseline < (float)netViewHeight) {
            canvas.drawLine((float)paddingLeft, baseline + (float)paddingTop + linePadding, (float)grossWidth, baseline + (float)paddingTop + linePadding, paint);
            baseline += (float)lineHeight;
        }
        super.onDraw(canvas);
    }

    public boolean isMaximized(){
        return isMaximized;
    }

    public void setMaximized(boolean maximized){
        isMaximized = maximized;
        if(!isMaximized){
            setLines(1);
        }else{
            setSingleLine(false);
        }
    }
}

NoteListAdapter.java

package harsha.notes.Items;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import harsha.notes.DB.NotesProvider;
import harsha.notes.NoteEditActivity;
import harsha.notes.R;
import harsha.notes.view.NoteTextView;
import harsha.notes.view.ViewUtils;

import java.util.ArrayList;


public class NoteListAdapter extends ArrayAdapter<Note> {
    ArrayList<Note> mNotes;
    final Context mContext;

    public NoteListAdapter(Context context, ArrayList<Note> notes) {
        super(context, R.layout.note_item, notes);
        mContext = context;
        mNotes = new ArrayList<Note>();
        mNotes = notes;
    }

    public class NoteViewHolder {
        public LinearLayout preview_layout;
        public TextView position;
        public TextView date;
        public TextView time;
        //public CheckBox check;
        public ImageView edit;
        public ImageView delete;
        public NoteTextView content;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NoteViewHolder noteViewHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.note_item, null);

            noteViewHolder = new NoteViewHolder();
            noteViewHolder.preview_layout = (LinearLayout) convertView.findViewById(R.id.preview);
            noteViewHolder.position = (TextView) convertView.findViewById(R.id.position);
            noteViewHolder.date = (TextView) convertView.findViewById(R.id.date);
            noteViewHolder.time = (TextView) convertView.findViewById(R.id.time);
            noteViewHolder.edit = (ImageView) convertView.findViewById(R.id.menu_edit);
            noteViewHolder.delete = (ImageView) convertView.findViewById(R.id.menu_delete);
            //noteViewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
            noteViewHolder.content = (NoteTextView) convertView.findViewById(R.id.preview_text);
        }else{
            noteViewHolder = (NoteViewHolder) convertView.getTag();
        }

        Note note = mNotes.get(position);

        noteViewHolder.content.setMaximized(note.getChecked());
        noteViewHolder.content.setNextFocusUpId(R.id.preview);
        noteViewHolder.preview_layout.setBackgroundResource(ViewUtils.getBackground(note.getColor(), noteViewHolder.content.isMaximized()));
        noteViewHolder.content.setText(note.getText());
        //noteViewHolder.check.setVisibility(View.GONE);
        noteViewHolder.date.setText(note.getDate());
        noteViewHolder.time.setText(note.getTime());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                LinearLayout preview_layout = (LinearLayout) view.findViewById(R.id.preview);
                NoteTextView content = (NoteTextView) view.findViewById(R.id.preview_text);
                Note note = (Note) preview_layout.getTag();

                content.setMaximized(!note.getChecked());
                preview_layout.setBackgroundResource(ViewUtils.getBackground(note.getColor(), !note.getChecked()));
                note.setChecked(!note.getChecked());
            }
        });

        noteViewHolder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout preview_layout = (LinearLayout) view.getRootView().findViewById(R.id.preview);
                Note note = (Note) preview_layout.getTag();

                Intent intent = new Intent(mContext, NoteEditActivity.class);
                intent.setData(Uri.withAppendedPath(NotesProvider.CONTENT_URI, Uri.encode(String.valueOf(note.getNoteID()))));
                mContext.startActivity(intent);
            }
        });

        noteViewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout preview_layout = (LinearLayout) view.findViewById(R.id.preview);
                Note note = (Note) preview_layout.getTag();


            }
        });

        noteViewHolder.position.setText(String.valueOf(position+1));
        noteViewHolder.preview_layout.setTag(note);

        convertView.setTag(noteViewHolder);
        return convertView;
    }
}

note_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@id/preview_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ff4A4A4A" android:padding="5dip" android:measureWithLargestChild="false"
              android:clickable="true" android:focusable="true">
    <LinearLayout android:id="@id/preview" android:focusable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/note_yellow_min"
                  android:focusableInTouchMode="true">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="40dip" android:layout_marginRight="0dp" android:layout_marginBottom="1dip" android:gravity="center_vertical" android:focusable="false"
                      android:layout_marginTop="1dip">
            <ImageView android:id="@id/menu_edit" android:layout_width="37dip" android:layout_height="37dip" android:src="@drawable/menu_edit" android:contentDescription="@string/color_yellow"
                       android:clickable="true" android:layout_marginLeft="2dip"/>
            <TextView android:id="@id/position" android:textSize="15.0dip" android:typeface="sans"  android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MMM" android:layout_gravity="center" android:layout_marginLeft="5dip" android:gravity="right"
                      android:focusable="false"/>
            <TextView android:id="@id/date" android:textSize="17.0dip" android:typeface="sans"  android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="16 AUG 2001" android:layout_weight="1" android:layout_gravity="center" android:layout_marginLeft="8dip"
                      android:textIsSelectable="false" android:focusable="false"/>
            <TextView android:id="@id/time" android:textSize="17.0dip" android:typeface="sans"  android:textStyle="bold" android:textColor="#ff000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20:11" android:layout_gravity="center" android:layout_marginLeft="8dip" android:layout_marginRight="8dip"
                      android:textIsSelectable="false" android:focusable="false"/>
            <ImageView android:id="@id/menu_delete" android:layout_width="37dip" android:layout_height="37dip" android:src="@drawable/menu_delete" android:contentDescription="@string/color_yellow"
                       android:clickable="true" android:layout_marginLeft="2dip" android:layout_marginRight="3dip"
                       android:visibility="visible"/>
            <ImageView android:id="@id/check" android:layout_width="37dip" android:layout_height="37dip" android:src="@drawable/menu_check" android:contentDescription="@string/color_yellow"
                       android:clickable="true" android:layout_marginLeft="0dp" android:layout_marginRight="3dip"
                       android:visibility="gone"/>
        </LinearLayout>
        <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#77000000" android:focusable="false"/>
        <harsha.notes.view.NoteTextView android:scrollbarStyle="outsideOverlay" android:autoLink="all" android:capitalize="sentences" android:lineSpacingExtra="4.0dip"  android:text="123" android:textColor="#ff000000" android:minLines="1" android:id="@id/preview_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22dp" android:layout_marginLeft="20dp" android:layout_marginRight="25dip" android:layout_marginTop="3dip" android:layout_marginBottom="28dip" android:clickable="true"
                                        android:typeface="sans" android:visibility="visible" android:focusable="false"
                                        android:background="@android:color/transparent" android:linksClickable="true"
                                        android:focusableInTouchMode="false" android:longClickable="true"
                                        android:enabled="true"/>
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

尝试将onFocusChanged侦听器设置为edittext。

答案 1 :(得分:1)

您的意思是,当您点击自定义TextView时,您的convertView的OnClickListener没有调用onClick()?

如果是,您可以尝试禁用自定义TextView。

<harsha.notes.view.NoteTextView android:scrollbarStyle="outsideOverlay" android:autoLink="all" android:capitalize="sentences" android:lineSpacingExtra="4.0dip"  android:text="123" android:textColor="#ff000000" android:minLines="1" android:id="@id/preview_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22dp" android:layout_marginLeft="20dp" android:layout_marginRight="25dip" android:layout_marginTop="3dip" android:layout_marginBottom="28dip" android:clickable="true"
                                    android:typeface="sans" android:visibility="visible" android:focusable="false"
                                    android:background="@android:color/transparent" android:linksClickable="true"
                                    android:focusableInTouchMode="false" android:longClickable="true"
                                    android:enabled="false"/>