Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setMovementMethod on a TextView inside a ListView

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;
}
like image 473
Laura Avatar asked Feb 19 '14 11:02

Laura


1 Answers

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;
}
like image 161
Laura Avatar answered Sep 20 '22 13:09

Laura