ListView: textIsSelectable and onItemClick
Asked Answered
B

2

7

Context

I have a list view where the row is basically composed of two TextView (a title and a content).

The second TextView can have a long text so I set maxLines="6". When user click on the row I get rid of the maxLines in order to show the full text.

public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        TextView content = (TextView ) view.findViewById(R.id.content);

        int limit = getResources().getInteger(R.integer.default_max_lines);

        if (content.getMaxLines() > limit) {
            content.setMaxLines(limit);
        }
        else {
            content.setMaxLines(Integer.MAX_VALUE);
        }
    }

Problem

Above code works great. I would like to be able to select my second TextView (content) too so I set android:textIsSelectable="true" (also tried setting programmatically).

But I can't expand/collapse my TextView because onItemClick is no longer called

That's because textIsSelectable catches all click event... From Android doc:

When you call this method to set the value of textIsSelectable, it sets the flags focusable, focusableInTouchMode, clickable, and longClickable to the same value. These flags correspond to the attributes android:focusable, android:focusableInTouchMode, android:clickable, and android:longClickable. To restore any of these flags to a state you had set previously, call one or more of the following methods: setFocusable(), setFocusableInTouchMode(), setClickable() or setLongClickable().

I tried to set these flags to false after setTextIsSelectable(true) but I didn't manage to make it work.

So, any ideas how to use both textIsSelectable and onItemClick?

PS: Supporting only Android > 4.0.

Bean answered 5/9, 2013 at 9:10 Comment(0)
E
0

I stumbled to the same problem and didn't fine any answer to this now 8 years old question. My solution was as follows:

  1. Set android:textIsSelectable="true" in the TextView
  2. Don't set onItemClickListener to the ListView
  3. Set OnClickListener in getView of the adapter to the TextView:
    override fun getView(position: Int, view: View?, parent: ViewGroup): View {
        ...
        val listener = View.OnClickListener {
            ...
        }
        viewHolder.textView.setOnClickListener(listener)
    }

That enabled click on the TextView as well as selection of its text content.

Embellishment answered 2/3, 2022 at 14:29 Comment(0)
F
-4

Use this below code It will works 100%

public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
    super(context, resource);
    this.mlist = mlist;
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

@Override
public int getCount() {
    return mlist.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.listitem, null);
    TextView text1 = (TextView) convertView.findViewById(R.id.item1);
    TextView text2 = (TextView) convertView.findViewById(R.id.item2);
    text1.setText(mlist.get(position).getListitem1());
    text2.setText(mlist.get(position).getListitem2());
    text2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // you just put your Logic here And use this custom adapter to
            // load your Data By using this particular custom adapter to
            // your listview

        }
    });
    return convertView;
}

  }

you just use this code in your Mainactivity

mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
  mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
mPullRefreshListView.setAdapter(mAdapter);
Freesia answered 1/3, 2014 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.