ListView OnItemClickListener无法在片段中工作

时间:2016-08-23 06:21:29

标签: android listview android-fragments

我已经回顾了有关此问题的许多问题,但我仍然没有找到适合我的解决方案。我一定会错过一些东西。
我的MainActivity处理了我的Fragments。默认Fragment包含显示附近WIFI接入点的ListView 与其他类似问题一样,当我点击ListView中的项目时,我没有得到任何回复。

这是我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

        <ImageView
            android:id="@+id/wifi_icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_action_wifi"
            android:tint="@color/ws_green" />

        <TextView
            android:id="@+id/device_id"
            android:layout_width="fill_parent"
            android:layout_height="24dp"
            android:layout_toRightOf="@+id/wifi_icon"
            android:layout_toEndOf="@+id/wifi_icon"
            android:textAlignment="center"
            android:gravity="bottom"
            android:text="00:00:00:00:00:00"
            android:textColor="@color/ws_blue"
            android:textStyle="bold"
            android:layout_alignParentTop="true" />

        <TextView
            android:id="@+id/wifi_power"
            android:layout_width="fill_parent"
            android:layout_height="24dp"
            android:textAlignment="center"
            android:gravity="top"
            android:text="Power goes here"
            android:textColor="@color/ws_blue"
            android:layout_marginLeft="@dimen/text_margin"
            android:layout_marginRight="@dimen/text_margin"
            android:layout_alignParentBottom="true"
            android:layout_toEndOf="@+id/wifi_icon" />

</RelativeLayout>

我的列表项类:

public class WifiItem extends RelativeLayout {

    WifiItem rootView;
    TextView lblPrimary;
    TextView lblSecondary;
    ImageView icon;

    public WifiItem(Context context) {
        super(context);
        init(context);

    }

    public WifiItem(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
        init(context);

    }

    private void init(Context context) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = (WifiItem) inflater.inflate(R.layout.item_wifi, this);
        lblPrimary = (TextView) rootView.findViewById(R.id.device_id);
        lblSecondary = (TextView) rootView.findViewById(R.id.wifi_power);
        icon = (ImageView) rootView.findViewById(R.id.wifi_icon);

    }

    public void setTvPrimaryText(String text){
        lblPrimary.setText(text);
    }

    public void setTvSecondaryText(String text){
        lblSecondary.setText(text);
    }
}

onCreateView()包含Fragment的{​​{1}}方法:

Listview

任何建议都将不胜感激。

修改

我的 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_wifi_list, container, false); Context context = view.getContext(); RelativeLayout relativeLayout = (RelativeLayout) view; ListView listView = (ListView) relativeLayout.findViewById(R.id.lst_wifi_items); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.i("WIFI_FRAGMENT_ONCLICK", "ITEM SELECTED: " + i); mListener.onListFragmentInteraction(view); } }); listView.setAdapter(new WifiItemListAdapter(getContext(), new ArrayList<ScanResult>())); return view; }

WifiListAdapter

2 个答案:

答案 0 :(得分:0)

问题在于:

`listView.setOnItemClickListener(new ListView.OnItemClickListener() `

您需要改为使用new AdapterView.OnItemClickListener()

所以它会是这样的

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("WIFI_FRAGMENT_ONCLICK", "ITEM SELECTED: " + i);
        mListener.onListFragmentInteraction(view);
    }
});

希望有所帮助

答案 1 :(得分:0)

错误发生在BroadcastReceiver 在扫描WIFI网络后,我这样做了:

listView = new ListView(context);
container.addView(listView);

当我应该这样做时:

listView = (ListView) container.findViewById(android.R.id.list);

我似乎在旧版本上创建并显示新的ListView,但新版本没有附加任何Listeners