Android setMovementMethod on a TextView inside a ListView
Asked Answered
M

1

6

Inside a list view I have on each row a text which is truncated because it is too long. So I set the setMovementMethod() on the textView in order to make it scrollable. But now the ListView cannot be clicked. How can I solve this problem?

Below is the getView() method from the adapter.

 @Override
public View getView(int position, View convertView, final ViewGroup parent) {       
    final ViewHolder holder;

    if (convertView == null) {          
        convertView = mInflater.inflate(R.layout.list_row, null);

        holder = new ViewHolder();
        holder.nameLabel = (TextView) convertView.findViewById(R.id.name);
        convertView.setTag(holder);
        holder.nameLabel.setMovementMethod(ScrollingMovementMethod.getInstance());
    } else {
        holder = (ViewHolder) convertView.getTag();
      }

    return convertView;
}
Middlebreaker answered 19/2, 2014 at 11:51 Comment(6)
Added the getView method from the adapter. It's not too different from what I explained in the post.Middlebreaker
How do you detect click on the list view ?Brinson
Is your view clickable ? https://mcmap.net/q/1915630/-android-listview-39-s-onitemclick-event-not-getting-called/693752Brinson
Inside the activity I use this: mListView.setOnClickListener(). It is working only if I click the row while I am not touching the textView which has the setMovemenMethod() set. If the touch is taken by the text view I think the movement method intercept it and not the list view.Middlebreaker
Try to add the attribute addStatesFromChildren developer.android.com/reference/android/view/…Brinson
I had already tried that but is was not working. I managed to find a workaround for this issue and I posted my answer below. Anyway, thank you very much for your help :)Middlebreaker
M
3

I managed to solve this issue by myself after all. I implemented the OnTouchListener inside the adapter and set it on the text view. The logic for touch event is: I check if the touch event is a tap or a swipe. If it is a swipe the swipe/scroll will be performed and if it a tap I call the method I use for the listView's click event.

 @Override
public boolean onTouch(View v, MotionEvent motionEvent) {

    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mIsScrolling = false;
            mDownX = motionEvent.getX();
            break;
        case MotionEvent.ACTION_MOVE:
            float deltaX = mDownX - motionEvent.getX();
            if ((Math.abs(deltaX) > mSlop)) { // swipe detected
                mIsScrolling = true;
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (!mIsScrolling) {
                openNewScreen(v); // this method is used for click listener of the ListView
            }
            break;

    }

    return false;
}
Middlebreaker answered 21/2, 2014 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.