我正在使用ListFragment的扩展来在屏幕上显示一些数据。我尝试通过onListItemClick函数处理列表项单击,但是当我单击list元素时,不会调用此函数。单击时,列表元素甚至不会变为蓝色。看起来它感觉不到我的点击。这是我的代码:
DataListFragment.java:
public class DataListFragment extends ListFragment {
private CustomArrayAdapter mAdapter;
public void setAdapter(CustomArrayAdapter adapter) {
this.mAdapter = adapter;
setListAdapter(mAdapter);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText("");
mAdapter = new CustomArrayAdapter(getActivity());
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// I want this function to be called
Log.i("DataListFragment", "Item clicked: " + id);
}
}
single_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/train_info"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textSize="20sp"
android:focusable="true"
android:textIsSelectable="true"
android:focusableInTouchMode="true"
android:freezesText="true">
</TextView>
<TextView
android:id="@+id/time_info"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textIsSelectable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textSize="20sp" >
</TextView>
</LinearLayout>
(我的列表中的每个项目都是两个textview是一行)
最后是CustomArrayAdapter.java:
public class CustomArrayAdapter extends ArrayAdapter<FinalData> {
private final LayoutInflater mInflater;
public CustomArrayAdapter(Context context) {
super(context, android.R.layout.simple_list_item_2);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(ArrayList<FinalData> data) {
clear();
if (data != null) {
for (FinalData appEntry : data) {
add(appEntry);
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.single_item, parent, false);
} else {
view = convertView;
}
FinalData item = getItem(position);
((TextView)view.findViewById(R.id.train_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.train_info)).setSelected(true);
((TextView)view.findViewById(R.id.time_info)).setEllipsize(TruncateAt.MARQUEE);
((TextView)view.findViewById(R.id.time_info)).setSelected(true);
((TextView)view.findViewById(R.id.time_info)).setText(item.getFullData());
((TextView)view.findViewById(R.id.train_info)).setText(item.getTimeData());
return view;
}
}
因此,此代码以我想要的方式显示我想要的所有数据,但不会感觉到我的点击次数。当我触摸它们时,列表中的项目甚至不会变成蓝色......
我只关注一个Android开发人员指南。我的片段里面有一个函数onListItemClick,但它不起作用。为什么?
http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
答案 0 :(得分:0)
你需要使用 itemclicklistener
并写下
parent.getItemAtPosition(position)
将上述语句返回的值转换为String,如果它是您用于列表视图的字符串。
希望它。帮助你。