Android Listview元素与侦听器重叠
我已经创建了一个列表视图,它可以从sql数据库自动创建其内容。单击时,每个列表项会根据单击的内容打开到另一个页面,但用于单击列表中每个成员的区域似乎会扩展到整个页面,尽管每个成员仅填充页面的一部分,因此,单击区域重叠 在本例中,红色表示第一个列表成员的单击区域的位置,紫色表示实际位置。所有其他成员也是如此,最高的一个似乎优先 列表成员是从中创建的 panel_stream.xml:Android Listview元素与侦听器重叠,android,listview,Android,Listview,我已经创建了一个列表视图,它可以从sql数据库自动创建其内容。单击时,每个列表项会根据单击的内容打开到另一个页面,但用于单击列表中每个成员的区域似乎会扩展到整个页面,尽管每个成员仅填充页面的一部分,因此,单击区域重叠 在本例中,红色表示第一个列表成员的单击区域的位置,紫色表示实际位置。所有其他成员也是如此,最高的一个似乎优先 列表成员是从中创建的 panel_stream.xml: --> 并放置在>活动_board _stream _view.xml内的ListView中: <?
-->
并放置在>活动_board _stream _view.xml内的ListView中:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.company.panels.BoardView"
android:background="@color/background"
android:id="@+id/mainLayout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="@color/colorPanel"
android:id="@+id/relativeLayout"
android:layout_alignParentBottom="false"
android:layout_marginTop="1dp"
android:layout_below="@+id/titleFrame" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorting By"
android:id="@+id/textView"
android:textSize="18dp"
android:textColor="#222222"
android:gravity="center_vertical"
android:layout_margin="10dp"
android:layout_centerVertical="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sortBy"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:spinnerMode="dropdown" />
</RelativeLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/panelHost"
android:footerDividersEnabled="true"
android:background="@color/background"
android:layout_below="@+id/relativeLayout"
android:descendantFocusability="blocksDescendants"
android:elevation="4dp"
android:divider="#00FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="End of Content"
android:id="@+id/textView8"
android:layout_gravity="center"
android:layout_margin="10dp" />
这是我的听众
BoardView.java:
package com.company.panels;
//不包括导入和不相关的方法
公共类BoardView扩展了AppCompative活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mydb=新的数据库管理器(此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。板\流\菜单,菜单);
setContentView(R.layout.activity\u board\u stream\u视图);
ListView ListView=(ListView)findViewById(R.id.panelHost);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
TextView日期戳=(TextView)findViewById(R.id.panelID);
toViewPanel(dateStamp.getText().toString());
}});
返回true;
}}
对于ListView
,您应该使用OnItemClickListener
而不是OnClickListener
。
(更多信息:)对于
列表视图
,您应该使用OnItemClickListener
而不是OnClickListener
。
(更多信息:)也发布屏幕截图。我刚刚为Claritype添加了一个。请发布您的
OnItemClickListener
,以及适配器的getView()
方法,如果您使用的是自定义方法。我刚刚在您的问题是findViewById()
调用中添加了它。该方法将在活动
中搜索它找到的具有该ID的第一个视图
。您的列表视图
将有多个具有相同ID的视图,但第一个视图(即顶部视图)将被返回。相反,您应该从传递给适配器的数据集中获取必要的信息。也发布屏幕截图。我刚刚为clarityPlease添加了一个。请发布您的OnItemClickListener
,以及适配器的getView()
方法,如果您使用的是自定义的。我刚刚在中添加了它,您的问题是findViewById()
调用。该方法将在活动
中搜索它找到的具有该ID的第一个视图
。您的列表视图
将有多个具有相同ID的视图,但第一个视图(即顶部视图)将被返回。相反,您应该从传递给适配器的数据集中获取必要的信息。我使用的是onItemClickListener,我只是忘了澄清一下,您是否也可以发布onItemClickListener代码?我只是在列表视图中添加了它,您必须指定(TextView)的哪个视图找到了ViewById(R.id.panelID)代码>你想获取,否则你会得到最上面的代码。你可以使用view.findViewById(),在onitemclicklistener中有一个视图参数。我正在使用onitemclicklistener,我只是忘了澄清一下,你能不能也发布onitemclicklistener代码?我刚刚在列表视图中添加了它,你必须指定(TextView)的哪个视图findViewById(R.id.panelID)代码>您要获取,否则将获取顶部的。您可以使用view.findViewById(),其中有一个视图参数。
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.company.panels.BoardView"
android:background="@color/background"
android:id="@+id/mainLayout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="@color/colorPanel"
android:id="@+id/relativeLayout"
android:layout_alignParentBottom="false"
android:layout_marginTop="1dp"
android:layout_below="@+id/titleFrame" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorting By"
android:id="@+id/textView"
android:textSize="18dp"
android:textColor="#222222"
android:gravity="center_vertical"
android:layout_margin="10dp"
android:layout_centerVertical="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sortBy"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:spinnerMode="dropdown" />
</RelativeLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/panelHost"
android:footerDividersEnabled="true"
android:background="@color/background"
android:layout_below="@+id/relativeLayout"
android:descendantFocusability="blocksDescendants"
android:elevation="4dp"
android:divider="#00FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="End of Content"
android:id="@+id/textView8"
android:layout_gravity="center"
android:layout_margin="10dp" />
package com.company.panels;
//Imports and irrelevant methods excluded
public class BoardView extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydb = new DBManager(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.board_stream_menu, menu);
setContentView(R.layout.activity_board_stream_view);
ListView listView = (ListView)findViewById(R.id.panelHost);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView dateStamp = (TextView)findViewById(R.id.panelID) ;
toViewPanel(dateStamp.getText().toString());
}});
return true;
}}