ListView: TextView with LinkMovementMethod makes list item unclickable?

前端 未结 11 738
野的像风
野的像风 2020-11-29 17:33

What I want to do: A list with messages like this:

and here is the mnessage the user writes, that will wrap nicely to the next line.

相关标签:
11条回答
  • 2020-11-29 18:19

    You have to place this line in your adapter item parent view android:descendantFocusability="blocksDescendants"

    0 讨论(0)
  • 2020-11-29 18:22

    This is happened because when we press on list item it sends the press event to all its children, so the child's setPressed calls rather than the list item. Hence for clicking the list item, you have to set the child's setPressed to false. For this, you have to make custom TextView class and override the desired method. Here is the sample code

    public class DontPressWithParentTextView extends TextView {

    public DontPressWithParentTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    public void setPressed(boolean pressed) {
        // If the parent is pressed, do not set to pressed.
        if (pressed && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }
    

    }

    0 讨论(0)
  • 2020-11-29 18:24

    The problem is in that LinkMovementMethod indicates that are going to manage the touch event, independiently the touch is in a Spannable or in normal text.

    This should work.

    public class HtmlTextView extends TextView {
    
        ...
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (getMovementMethod() == null ) {
                boolean result = super.onTouchEvent(event); 
                return result;
            }
    
            MovementMethod m = getMovementMethod();     
            setMovementMethod(null);
    
            boolean mt = m.onTouchEvent(this, (Spannable) getText(), event);
            if (mt && event.getAction() == MotionEvent.ACTION_DOWN) {
                event.setAction(MotionEvent.ACTION_UP);
                mt = m.onTouchEvent(this, (Spannable) getText(), event);
                event.setAction(MotionEvent.ACTION_DOWN);
            }
    
            boolean st = super.onTouchEvent(event);
    
            setMovementMethod(m);
            setFocusable(false);
    
            return mt || st;
        }
    
        ...
    }
    
    0 讨论(0)
  • 2020-11-29 18:29

    I have another solution use the list item layout with two different text:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >
    
    <com.me.test.DontPressWithParentTextView
        android:id="@+id/text_user"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="(Message Text)" />
    
    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="(Message Text)" />
    

    and the adapter code as:

    DontPressWithParentTextView text1 =  (DontPressWithParentTextView) convertView.findViewById(R.id.text_user);
    
                    TextView text2 = (TextView) convertView.findViewById(R.id.text_message);
                    text2.setText(message);
    
                    SpannableStringBuilder f = new SpannableStringBuilder();
                    CharSequence username = names1[position];
                    f.append(username );
                    f.setSpan(new InternalURLSpan(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getBaseContext(), "Clicked User", Toast.LENGTH_SHORT).show();
                        }
                    }), f.length() - username.length(), f.length(),
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                    f.append(" ");
    
                    text1.setText(f);
                    text1.setMovementMethod(LinkMovementMethod.getInstance());
                    text1.setFocusable(false);
    

    This will work.. :-)

    0 讨论(0)
  • 2020-11-29 18:31

    apply this textview in layout. https://gist.github.com/amilcar-andrade/e4b76840da1dc92febfc

    0 讨论(0)
提交回复
热议问题