stackyyflow
stackyyflow

Reputation: 763

Weird behaviour of ListView

When I add an item below the "ADD ITEM BELOW" comment as shown below, my onItemLongClick and ListItemClick cannot work. However once I've removed the items below the "ADD ITEM BELOW", it will work. What's wrong?

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/caldroid_transparent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/event_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/event_list_margin"
            android:layout_marginBottom="@dimen/event_list_margin"
            android:orientation="horizontal">

            <RelativeLayout
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:orientation="vertical"
                android:layout_marginLeft="@dimen/standard_margin_size">

                <TextView
                    android:id="@+id/event_day"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="20"
                    android:gravity="center"
                    android:layout_centerHorizontal="true"
                    android:textColor="@color/caldroid_black"
                    android:textSize="45dp" />

                <TextView
                    android:id="@+id/event_month_year"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="JAN 2015"
                    android:textSize="12dp"
                    android:gravity="center"
                    android:layout_below="@id/event_day"
                    android:textColor="@color/caldroid_black" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="@dimen/standard_padding_size"
                android:paddingBottom="@dimen/standard_padding_size"
                android:paddingLeft="@dimen/feed_item_profile_info_padd"
                android:paddingRight="@dimen/feed_item_profile_info_padd"
                android:layout_marginLeft="@dimen/feed_item_profile_info_padd"
                android:layout_marginRight="@dimen/feed_item_profile_info_padd"
                android:id="@+id/event_set_colour">

                <TextView
                    android:id="@+id/event_title"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:textStyle="bold"
                    android:textColor="@color/caldroid_black"
                    android:textSize="@dimen/standard_text_size" />

                <TextView
                    android:id="@+id/event_location"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="local"
                    android:layout_below="@id/event_title"
                    android:textColor="@color/caldroid_black"
                    android:textSize="@dimen/feed_item_profile_name" />

                <TextView
                    android:id="@+id/event_start_end_time"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/event_location"
                    android:text="28 May 2015, Saturday"
                    android:textColor="@color/caldroid_black"
                    android:textSize="@dimen/feed_item_profile_name" />
            </RelativeLayout>


        </LinearLayout>

<!-- ADD ITEM BELOW -->
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"/>
    </LinearLayout>

EventFragment.java

public class EventFragment extends ListFragment {

    //Set database to allow user to retrieve data to populate EventFragment.java
    private AppointmentController appointmentDatabase;
    //List to get all the appointments
    private List<Appointment> allAppointment;
    EventAdapter adapter;
    Appointment selected_appointment;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Get all the appointment
        appointmentDatabase = new AppointmentController(getActivity());
        appointmentDatabase.open();
        allAppointment = appointmentDatabase.getAllAppointment();
        //Initialise ArrayAdapter adapter for view
        adapter = new EventAdapter(getActivity(), R.layout.row_event, allAppointment);
        setListAdapter(adapter);


        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) {
                // remove code for simplicity

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_listfragment, container, false);

        return rootView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // remove code for simplicity
    }

Upvotes: 0

Views: 54

Answers (2)

Hradesh Kumar
Hradesh Kumar

Reputation: 1805

If you wants to make onItemClick work add

android:descendantFocusability="blocksDescendants"

to the parent layout something like it:-

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/caldroid_transparent"
        android:orientation="vertical"
android:descendantFocusability="blocksDescendants">

</LinearLayout>

when you blocksDescendants then you can not get onClick event on Button in row of ListView. Basically you can get event on either row or Button in a row.

Upvotes: 2

Kevin Cronly
Kevin Cronly

Reputation: 1398

Try changing:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // remove code for simplicity
}

to

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public boolean onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            // whatever code would have been above put in here

}

Upvotes: 1

Related Questions