Android 在LinearLayout中获取MClick实现的位置

Android 在LinearLayout中获取MClick实现的位置,android,listview,onitemclick,Android,Listview,Onitemclick,我试图显示一个ListView,我的Java类扩展了ListFragment。在ListItem上设置setOnItemClickListener时,单击侦听器似乎不起作用。这是我的密码: ListView\u rssfeeds\u tab.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

ListView\u rssfeeds\u 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>
<?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>

rss\u项目\u列表\u行.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>
<?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类RSSFeedsTab扩展了ListFragment实现了McClickListener{
.
.
.
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
.
.
.
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
.
.
.
SimpleAdapter SimpleAdapter=新的SimpleAdapter(getActivity().getBaseContext(),aList,R.layout.rss_item_list_行,from,to);
setListAdapter(simpleAdapter);
View RootView=getActivity().GetLayoutFlater().inflate(R.layout.fragment\u rssfeeds\u选项卡,容器,false);
ListView RSSFeedsItemLL=(ListView)RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(新的OnItemClickListener(){
公共控件单击(适配器视图适配器,视图v,内部位置,长id){
System.out.println(“*********在单击时到达”);
Toast.makeText(getActivity(),“Item:+位置,Toast.LENGTH_SHORT).show();
}
});
返回super.onCreateView(充气机、容器、savedInstanceState);
}

通过这么多的实现,我能够获得ListView
rssFeeds\u LL\u list\u view
。我不确定如何在这些ListView上使用click事件,以便在ListView中获得
位置

您必须使用listener来获取单击的项目:

****更新****

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

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);
使用这行代码,您创建一个适配器并将此适配器提供给片段的ListView。然后从布局中恢复RSSFeedsItemLL ListView,并在其上设置OnItemClickListener:

ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new AdapterView.OnItemClickListener(){...}
但此ListView从未链接到适配器

您有两种解决方案:

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

  • 在片段中使用rssfeedsitemllistview

对于第二种情况,您只需在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\u item\u list\u row.xml中线性布局定义中的行
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"
    >
    ...

...

在ListView视图上使用
onItemClickListener
RSSFeedsItemLL

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();
}});
RSSFeedsItemLL.setOnItemClickListener(新的OnItemClickListener(){
公共控件单击(适配器视图适配器,视图v,内部位置,长id){
System.out.println(“*********在单击时到达”);
Toast.makeText(getActivity(),“Item:+位置,Toast.LENGTH_SHORT).show();
}});

参考:OnItemClickListener

最后,我通过
重写
方法
getView()

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


邮政编码adapter@ParikshitChalke:更新了代码。如果您已经尝试了“所有选项”--这是否意味着没有什么可以做的?也许您应该说出您实际尝试的内容。我使用
ListView RSSFeedsItemLL=(ListView)RootView.findViewById(R.id.rssFeedsFragment)尝试了上述方法;
但是,我仍然看不到祝酒词。有一个很大的错误。侦听器应该设置在listView上,而不是在LinearLayouts上。我也尝试了,并更新了我的问题代码。仍然没有成功。如果你看到有问题的片段代码,我实际上也在尝试,但没有成功。请注意,我的Java类扩展到了
ListFragment
和not
ListActivity
,由于其他功能依赖性,我无法更改它。@OmSao我用更详细的解释更新了答案感谢您的建议和详细解释!@Stephane Rothen: