在 LinearLayout 中获取 onItemClick 实现的位置
getting the position onItemClick implementation in LinearLayout
我正在尝试显示一个 ListView 和我的 Java class extendsListFragment
。在 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 是片段而不是 Activity)
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 上使用点击事件,以便我可以在 ListView 中获得 position
?
您必须使用侦听器 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 从未链接到适配器。
您有两个解决方案:
用 class 定义中的 Fragment 替换 ListFragment,并在布局文件中使用 ListView
在片段中使用 RSSFeedsItemLL ListView
对于第二种情况,您只需覆盖 RSSFeedsTab 中的 onListItemClick 方法 class :
@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"
>
...
在 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();
}});
参考:OnItemClickListener
最后我用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;
}
};
我正在尝试显示一个 ListView 和我的 Java class extendsListFragment
。在 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 是片段而不是 Activity)
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 上使用点击事件,以便我可以在 ListView 中获得 position
?
您必须使用侦听器 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 从未链接到适配器。
您有两个解决方案:
用 class 定义中的 Fragment 替换 ListFragment,并在布局文件中使用 ListView
在片段中使用 RSSFeedsItemLL ListView
对于第二种情况,您只需覆盖 RSSFeedsTab 中的 onListItemClick 方法 class :
@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"
>
...
在 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();
}});
参考:OnItemClickListener
最后我用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;
}
};