我对此列表视图中的项目有疑问。正常行为是当用户点击一个项目时,背景颜色变为橙色。我的应用程序没有发生这种情况,我不知道为什么。
main.xml 只有一个:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
片段的 xml 只有一个:
<ListView
android:id="@android:id/list"
style="@style/MyListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
我在 listView 项中膨胀的视图是:
<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>
我为 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>
list_subject_selector drawable 是:
<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>
我不知道为什么会这样……我正在使用 Android ICS 进行测试。
有任何想法吗?
谢谢大家,费利佩
更新 1 这是 ListFragment 代码:
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;
}
}
}
更新 2 现在可以工作了!那很烦人。我这样做是为了让它工作:
我用来膨胀的 xml 文件有:
<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" >
我改为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
但是,我认为真正做出改变的是:在 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);
}
重要的是行list = getListView(); 我没有通过这段代码获得 ListView。我在 onCreateView() 方法中使用 findViewById 获取 listView。我想我失去了与真实 ListView 对象的链接。
感谢所有帮助过我的人!:)
费利佩});