Android does not see the state of pressing

I made a linear layout on which I add views, simulating a table, because I read that the list in the scroll is not an option :) Therefore, I have a linear layout with wrap_content inside the scroll.

The problem is that for added views, I can’t cause any click state to change the graphics, as before, when it was on the list, it worked fine.

I have the following:

if (arrayWorksWith.size()!=0) { this.listAdapter = new WorksWithListAdapter(this, R.layout.works_with_cell, R.id.lblItemTitle, arrayWorksWith); for (int i=0; i<arrayWorksWith.size(); i++) { View v = this.listAdapter.getView(i, null, layoutTblWorksWith); v.setTag(new Integer(1000 + arrayWorksWith.get(i).getId())); v.setOnClickListener(this); layoutTblWorksWith.addView(v); } } 

The problem is that the click event is fired, but I have no visual feedback. Inside this view, I have an Imageview with the background set to the following drawable:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/r1" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/r1" /> <!-- focused --> <item android:drawable="@drawable/transparent_background" /> <!-- default --> </selector> 

Also, I just tried the touch listener in the view I am adding, but this also fails :(

  v.setOnTouchListener(new OnTouchListener() { public boolean onTouch (View v, MotionEvent event) { TextView lblCategoryTitle = (TextView)v.findViewById(R.id.lblItemTitle); if(event.getAction() == MotionEvent.ACTION_DOWN) { lblCategoryTitle.setTextColor(R.color.white); } else if (event.getAction() == MotionEvent.ACTION_UP) { lblCategoryTitle.setTextColor(R.color.textLabel_blue); } return false; } }); 

So, when this code was inside the ListView, when I tapped on the cell, the image changed the background, giving visual feedback, but when I create it now and add it to LinearLayout, it stops working.

Any ideas? I tried playing with a custom, clickable, duplicated parent state, but no luck.

+4
source share
1 answer

You should set listView to OnItemClickListener, not to OnClickListener.

0
source

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


All Articles