Reputation: 47
I've created a list view that automatically created its content from an sql DB. When clicked, each list item opens into another page based on what was clicked, but the area for clicking on each member of the list seems to extend across the entire page, despite each member only filling part of the page, as a result the click areas overlap.
In this example, the red represents where the click area should be for the first list member, and the purple represents where it actually is. The same is happening with all the other members, the highest one up just appears to take precedence.
The list members are created from
panel_stream.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:elevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/panel_header_shape"
android:elevation="4dp"
android:id="@+id/header"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Title Here"
android:id="@+id/panelTitle"
android:layout_gravity="center_vertical"
android:elevation="4dp"
android:textColor="#eeeeee"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:typeface="sans"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Unattributed"
android:id="@+id/panelAuthor"
android:textColor="#dddddd"
android:textStyle="italic"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:background="#ffffff"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/relativeLayout2"
android:layout_alignParentBottom="false">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:background="@drawable/panel_shape"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/panelContent"
android:layout_margin="5dp"
android:text="Content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/panel_footer_shape"
android:elevation="4dp"
android:layout_below="@+id/relativeLayout2"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="0"
android:id="@+id/commentCount"
android:textColor="#000000"
android:textStyle="italic"
android:maxLines="1"
android:layout_toLeftOf="@+id/imageButton"
android:layout_toStartOf="@+id/imageButton"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:background="@mipmap/ic_comment_black_24dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:contentDescription="comment" />-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2016-04-20 04:20:00"
android:id="@+id/dateStamp"
android:textColor="@color/colorAccent"
android:textStyle="italic"
android:maxLines="1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/panelID"
android:textColor="#00FFFFFF"
android:textStyle="italic"
android:maxLines="1" />
</RelativeLayout>
And are placed in the ListView inside >activity_board_stream_view.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: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" />
Here is the onItemClickListener
BoardView.java:
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;
}}
Upvotes: 0
Views: 49
Reputation: 946
You should use OnItemClickListener
instead of OnClickListener
for ListView
.
(More info: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html)
Upvotes: 2