简体   繁体   中英

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM