ListView的奇怪行为

时间:2015-06-18 19:19:11

标签: java android listview

当我在“ADD ITEM BELOW”评论下方添加项目时,如下所示,我的onItemLongClickListItemClick无效。但是,一旦我删除了“ADD ITEM BELOW”下面的项目,它就会起作用。怎么了?

        <?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
    }

2 个答案:

答案 0 :(得分:2)

  

如果你想使onItemClick工作添加

     

机器人:descendantFocusability =&#34; blocksDescendants&#34;

     
    

到父布局之类的东西: -

  
<?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>
  

当你阻止后代时,你就无法获得onClick事件   ListView行中的按钮。基本上你可以在任一行上获得事件   或按钮连续。

答案 1 :(得分:1)

尝试更改:

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

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

}