onItemClickListener не работает в ListFragment и Custom Adaptor

Я совершенно новичок в программировании Android и недавно пытался сделать ListFragment с Adapter. Фрагмент изначально предназначался для отображения списка, содержащего TextView, ImageButton и ImageView. Я успешно заставил ImageButton работать при нажатии. Но этого не произошло, когда я попытался нажать ListView. Я тоже перепробовал так много комбинаций по этому поводу. Как добавление

android: focusable = "false"

ко всем взглядам, и

android: DescendantsFocusability = "blocksDescendants"

в корневой макет ListView, но ни одно из решений у меня не работает. Есть ли другое решение для моего случая?

event_item_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="145dp"
    android:orientation="vertical"
    android:paddingTop="2.5dp"
    android:descendantFocusability="blocksDescendants">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="145dp"
        android:id="@+id/img_item"
        android:scaleType="centerCrop" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:orientation="horizontal"
        android:layout_gravity="bottom"
        android:background="#AA000000">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:gravity="left"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:id="@+id/txt_item_title"
                android:textSize="20sp"
                android:text="Title"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:maxLines="1" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/darker_gray"
                android:id="@+id/txt_item_date"
                android:textSize="15dp"
                android:text="Date"
                android:maxLines="1" />
        </LinearLayout>
        <ImageButton
            android:layout_width="55dp"
            android:layout_height="match_parent"
            android:id="@+id/imageButton"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_right_arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:tint="#ffffffff"
            android:scaleType="fitXY"
            android:drawSelectorOnTop="true"
            android:padding="15dp" />
    </RelativeLayout>
</FrameLayout>

повестка дня.java:

package com.permata.app.myapplication;


import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class agenda extends ListFragment {

    public agenda() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_agenda, container, false);
        super.onCreateView(inflater, container, savedInstanceState);

        ArrayList<agenda_content> konten_agenda = getKonten();

        final ListView lv = (ListView)view.findViewById(android.R.id.list);
        lv.setAdapter(new agenda_adapter(this.getActivity(), konten_agenda));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Intent i = new Intent(getActivity(),agenda_details.class);
                startActivity(i);
            }
        }); //this doesn't work

        return view;
    }

    private ArrayList<agenda_content> getKonten() {
        ArrayList<agenda_content> results = new ArrayList<agenda_content>();

        agenda_content ct = new agenda_content();
        ct.setTitle("Agenda 1");
        ct.setDate("Date 1");
        ct.setImage(R.drawable.img_sample_1);
        results.add(ct);

        ct = new agenda_content();
        ct.setTitle("Agenda 2");
        ct.setDate("Date 2");
        ct.setImage(R.drawable.img_sample_2);
        results.add(ct);

        ct = new agenda_content();
        ct.setTitle("Agenda 3");
        ct.setDate("Date 3");
        ct.setImage(R.drawable.img_sample_3);
        results.add(ct);

        ct = new agenda_content();
        ct.setTitle("Agenda 4");
        ct.setDate("Date 4");
        ct.setImage(R.drawable.img_sample_1);
        results.add(ct);

        ct = new agenda_content();
        ct.setTitle("Agenda 5");
        ct.setDate("Date 5");
        ct.setImage(R.drawable.img_sample_2);
        results.add(ct);

        ct = new agenda_content();
        ct.setTitle("Agenda 6");
        ct.setDate("Date 6");
        ct.setImage(R.drawable.img_sample_3);
        results.add(ct);

        return results;
    }
}

повестка дня_adapter.java:

package com.permata.app.myapplication;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.media.Image;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class agenda_adapter extends BaseAdapter {
    private static ArrayList<agenda_content> content_array;
    private LayoutInflater mInflater;
    public agenda_adapter(Context context, ArrayList<agenda_content> result) {
        content_array = result;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return content_array.size();
    }

    @Override
    public Object getItem(int position) {
        return content_array.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.event_item_list, null);
            holder = new ViewHolder();
            holder.title = (TextView)convertView.findViewById(R.id.txt_item_title);
            holder.date = (TextView)convertView.findViewById(R.id.txt_item_date);
            holder.image = (ImageView)convertView.findViewById(R.id.img_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.title.setText(content_array.get(position).getTitle());
        holder.date.setText(content_array.get(position).getDate());
        holder.image.setImageResource(content_array.get(position).getImage());

        ImageButton domore = (ImageButton)convertView.findViewById(R.id.imageButton);
        domore.setFocusable(false);
        domore.getBackground().setColorFilter(0xAAAC7805, PorterDuff.Mode.MULTIPLY);
        domore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Snackbar snackbar = Snackbar.make(v, "HEY!", Snackbar.LENGTH_LONG)
                        .setAction("Action", null);
                snackbar.show();
            }
        });
        return convertView;
    }
    static class ViewHolder {
        TextView title;
        TextView date;
        ImageView image;
    }
}

благодаря.

РЕДАКТИРОВАТЬ: fragment_agenda.xml содержит:

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parrent"
    android:id="@id/android:list"
    android:drawSelectorOnTop="true"
</ListView>

РЕШЕНО: изменен повестка дня .java на extends Fragment вместо ListFragment, установите Listview id в fragment_agenda на @+id/lv_item_agenda и установите ListView lv, чтобы найти R.id.lv_item_agenda вместо android.R.id.list.

Но я до сих пор не знаю, почему такое сложное решение работает. Интересно, есть ли что-то, связанное с android.R.id.list, из-за чего у меня возникла эта проблема. Любая идея?


person rahadi    schedule 23.01.2016    source источник
comment
Опубликуйте свой макет fragment_agenda   -  person Brendon    schedule 23.01.2016
comment
Что именно должно произойти? Вы хотите, чтобы каждый элемент списка имел onClick?   -  person TejjD    schedule 23.01.2016
comment
@Brendon: опубликовал   -  person rahadi    schedule 23.01.2016
comment
@TejjD: да. Я хочу, чтобы элемент списка имел onClick и ImageButton одновременно   -  person rahadi    schedule 23.01.2016


Ответы (1)


Очевидно, этот трюк focusable работает только для кнопок и не работает для кнопок ImageButtons. См. Комментарии в ответе здесь.

В исходном коде Android для решения этой проблемы обычно используется класс DontPressWithParentImageView. Этот класс используется для кнопки вызова в приложении "Контакты Android".

/**
 * Special class to to allow the parent to be pressed without being pressed itself.
 * This way the line of a tab can be pressed, but the image itself is not.
 */
public class DontPressWithParentImageView extends ImageView {

    public DontPressWithParentImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setPressed(boolean pressed) {
        // If the parent is pressed, do not set to pressed.
        if (pressed && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }
}

Here is an example of how to use it in Java code:

    if (mCallButton == null) {
        mCallButton = new DontPressWithParentImageView(mContext, null);
        mCallButton.setId(id);
        mCallButton.setOnClickListener(mCallButtonClickListener);
        mCallButton.setBackgroundResource(R.drawable.call_background);
        mCallButton.setImageResource(android.R.drawable.sym_action_call);
        mCallButton.setPadding(mCallButtonPadding, 0, mCallButtonPadding, 0);
        mCallButton.setScaleType(ScaleType.CENTER);
    }

    mCallButton.setTag(tag);
    mCallButton.setVisibility(View.VISIBLE);

Для вас это будет примерно так:

    DontPressWithParentImageView domore = (DontPressWithParentImageView)convertView.findViewById(R.id.imageButton);
    domore.setFocusable(false);
    //..............

Вот пример noreferrer. a> о том, как использовать его в макете xml:

<com.permata.app.myapplication.DontPressWithParentImageView android:id="@+id/call_button"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:paddingLeft="14dip"
                android:paddingRight="14dip"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:src="@android:drawable/sym_action_call"
                android:background="@drawable/call_background"
            />
person Daniel Nugent    schedule 23.01.2016
comment
Спасибо за твой ответ. Кстати, есть ли вероятность, что проблема onitemclicklistener вызвана чем-то другим, а не ImageButton? поскольку проблема существует, даже когда я удаляю ImageButton - person rahadi; 23.01.2016