在LinearLayout中获取onItemClick实现的位置

时间:2018-10-08 09:32:11

标签: android listview onitemclick

我试图显示一个ListView并且我的Java类扩展了ListFragment。在ListItem上设置setOnItemClickListener时,单击侦听器似乎不起作用。这是我的代码:

ListView_rssfeeds_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/relativeLayout"
    tools:context=".interestingReads.RSSFeedsTab">
    <ListView
        android:id="@+id/rssFeedsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/diffBackgroundWhite"
        />

</RelativeLayout>

rss_item_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rssFeeds_LL_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="12dp"
    android:layout_marginBottom="12dp"
    android:orientation="vertical"
    android:clickable="true"
    >
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="6dp"
    >

    <ImageView
        android:id="@+id/rssFeeds_Image_list_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/app_name"
        android:padding="0dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="7dp">

        <TextView
            android:id="@+id/rssFeeds_Title_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/rssFeeds_Description_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:textSize="12sp" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

RSSFeedsTab.java (请注意:RSSFeedsTab是片段而不是活动)

public class RSSFeedsTab extends ListFragment implements  OnItemClickListener {
.
.
.
@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
.
.
.
}

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
        setListAdapter(simpleAdapter);

        View RootView =  getActivity().getLayoutInflater().inflate(R.layout.fragment_rssfeeds_tab, container, false);
        ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);




        RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                System.out.println("********Reached onClick*********");
                Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return super.onCreateView(inflater, container, savedInstanceState);


    }

有了这么多的实现,我就能得到ListView rssFeeds_LL_list_view。我不确定如何在这些ListView上使用click事件,以便可以在ListView中获得position

3 个答案:

答案 0 :(得分:1)

您必须使用侦听器OnItemClickListener来获取被单击的项目: OnItemClickListener on Android developer

****更新****

在您的代码中,您使用的是已经包含Listview的ListFragment。

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);

在这行代码中,您将创建一个适配器并将该适配器提供给Fragment的ListView。然后,从布局中恢复RSSFeedsItemLL ListView,并在其上设置OnItemClickListener:

ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new AdapterView.OnItemClickListener(){...}

但是此ListView从未链接到适配器。

您有两种解决方案:

  • 在类定义中用片段替换ListFragment,并在布局文件中使用ListView

  • 在片段中使用RSSFeedsItemLL ListView

对于第二种情况,您只需要覆盖RSSFeedsTab类中的onListItemClick方法:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    System.out.println("********Reached onClick*********");
    Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
    super.onListItemClick(l, v, position, id);
}

您还必须在rss_item_list_row.xml的LinearLayout定义中删除行android:clickable="true"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rssFeeds_LL_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
>
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="6dp"
    >
    ...

答案 1 :(得分:0)

在ListView视图上使用onItemClickListenerRSSFeedsItemLL

RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
      System.out.println("********Reached onClick*********");
      Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}});

参考:OnItemClickListener

答案 2 :(得分:0)

最后,我已经通过overriding方法getView()

解决了该问题

因此,我只更改了这一行: SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to){
    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
        View v = super.getView(position, convertView, parent);
        final int finalposition = position;
        LinearLayout RSSFeed_singleItem=(LinearLayout) v.findViewById(R.id.rssFeeds_LL_list_view);
        RSSFeed_singleItem.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"Link:==>"+localLinkArray[finalposition],Toast.LENGTH_SHORT).show();
            }
        });
        return v;
    }
};