I am having an issue with the items in this list view. Normal behaviour is that when user taps in one item, the background color changes to orange. This is not happening to my app, and I can't figure out why.
The main.xml has just a:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
The fragment's xml has only a:
<ListView
android:id="@android:id/list"
style="@style/MyListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
The view that I inflate inside the listView item is:
<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 the ListView is:
<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 drawable is:
<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 with Android ICS.
Any ideas?
Thanks 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 It now works! That was very annoying. This I did to make it work:
The xml file that I was using 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, what I think that really made the change was this: Inside the 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 important is the line list = getListView(); I wasn't getting the ListView through this piece of code. I was getting the listView at the onCreateView() method, with a findViewById. I suppose that I was losing the link with the real ListView object.
Thanks to everyone who helped me! :)
Felipe });