ListView inside snippet used in ViewPager does not behave as expected

I have a problem with items in this list. The normal behavior is that when a user clicks on one element, the background color changes to orange. This does not happen with my application, and I cannot understand why.

The main.xml file has only:

<android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> 

The xml snippet only has:

 <ListView android:id="@android:id/list" style="@style/MyListView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> 

The view that I am inflating inside the listView element:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textViewName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/imageView1" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignLeft="@+id/textViewName" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="10dp" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/bottom_arrow" /> </RelativeLayout> 

The style I defined for ListView:

 <style name="MyListView" parent="@android:style/Widget.ListView.White"> <item name="android:listSelector">@drawable/list_subject_selector</item> <item name="android:cacheColorHint">@android:color/transparent</item> <item name="android:background">@drawable/list_subject_selector</item> </style> 

And the list_subject_selector output list:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- touch down --> <item android:drawable="@drawable/list_subject_focused" android:state_pressed="true"/> <!-- selected --> <item android:drawable="@drawable/list_subject_unfocused" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> </selector> 

I have no idea why this is happening ... I am testing Android ICS.

Any ideas?

Thanks to everyone, Felipe

UPDATE 1 This is the ListFragment code:

 public class MyFragment extends ListFragment { private ListAdaptor adapter; private InternalDB db; Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = getActivity(); db = new InternalDB(getActivity()); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayList<EventVO> listVO = db.getSummaryEvents(true); adapter = new ListAdaptor(mContext, R.layout.event_inflate, listVO); setListAdapter(adapter); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout rl = (LinearLayout) inflater.inflate(R.layout.general_list, container, false); return rl; } private class ListAdaptor extends ArrayAdapter<EventVO> { private ArrayList<EventVO> listVO; public ListAdaptor(Context context, int textViewResourceId, ArrayList<EventVO> items) { super(context, textViewResourceId, items); this.listVO = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { v = ViewGroup.inflate(getContext(), R.layout.event_inflate, null); } final EventVO o = listVO.get(position); TextView fullName = (TextView) v.findViewById(R.id.textViewName); fullName.setText(o.getUserName()); //removed part of the code for brevity v.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent(mContext, EventDetailsActivity.class); i.putExtra("USER_ID", o.getUserId()); startActivity(i); } }); return v; } } } 

UPDATE 2 Now it works! It was very unpleasant. I did this to make it work:

The xml file I used to inflate had:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > 

I changed to:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> 

But, I think it really changed that: Inside onActivityCreated ()

 list = getListView(); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ListAdaptor la = (ListAdaptor)arg0.getAdapter(); EventVO vo = la.getItem(arg2); Intent i = new Intent(mContext, EventDetailsActivity.class); i.putExtra("USER_ID", vo.getUserId()); i.putExtra("IS_BORROWED", true); startActivity(i); } 

The list of strings is important = getListView (); I did not get ListView through this piece of code. I was getting listView in onCreateView () method with findViewById. I believe that I have lost touch with the actual ListView object.

Thanks to everyone who helped me! :)

Felipe});

+4
source share
2 answers

Try changing your list. Currently, the second option can be used only when the item in your ListView is not focused or pressed and not selected. I really don’t understand how you want your selector to look, but if "@drawable/list_selector_unfocused" is how you want the list selector to look normal, just remove all other attributes, which means that "@drawable/list_selector_focused" will be used only when the item.

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- touch down --> <item android:drawable="@drawable/list_subject_focused" android:state_pressed="true"/> <!-- selected --> <item android:drawable="@drawable/list_subject_unfocused" /> 

+2
source

I found that when I added ListView layouts to pure Fragment , you would lose a lot of the free old ListActivity templates and the one provided in the background. I would advise you to stop using pure Fragment to use ListFragment , and you can watch all this awesomeness without having to play with select lists.

The Swell and Tide tabs in our application are ListFragments inside the ViewPager and look like this:

ListFragment within a pager with highlighting (original ICS is a bit darker than shown here)

+1
source

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


All Articles