ListView: textIsSelectable and onItemClick

Context

I have a list view in which a string basically consists of two TextViews (title and content).

The second TextView may have long text, so I set maxLines="6" . When the user clicks on the line , I get rid of maxLines 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

It works fine on code. I would also like to be able to select the second TextView (content) to set android:textIsSelectable="true" (also tried setting it programmatically).

But I can not expand / collapse my TextView because onItemClick no longer called

This is because textIsSelectable catches all click events ... From Android doc:

When you call this method to set the value of textIsSelectable, it sets the flags with focus, 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 the state you set earlier, 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 was not able to get it to work.

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

PS: Support only Android> 4.0.

+6
source share
1 answer

Use this code below. It will work 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); 
-4
source

Source: https://habr.com/ru/post/953221/


All Articles