-1

我正在使用 ListFragment 的扩展在屏幕上显示一些数据。我尝试通过 onListItemClick 函数处理列表项点击,但是当我点击列表元素时,不会调用此函数。当我单击时,列表元素甚至不会变成蓝色。看起来它感觉不到我的点击。这是我的代码:

数据列表片段.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> 

(我列表中的每一项都是两个文本视图是一行)

最后是 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

4

1 回答 1

0

你需要使用 itemclicklistener

在那写

parent.getItemAtPosition(position)

如果它是您用于列表视图的字符串,则将上述语句返回的此值转换为字符串。

希望它对你有帮助。

于 2013-04-20T19:55:06.323 回答