SOUser
SOUser

Reputation: 1

ListView unclickable in spite of Android:Clickable="true"

I've followed various links describing solutions to this problem, and no solution has worked thus far, or is non-applicable in my case. I'm wondering if there is a problem in my code/with a custom adapter? I've tried making the XML clickable, in addition to an OnItemClickListener. I've tried various configurations of clickable (individual elements, linear layout, and the listview itself), and no attribution of this property works.

I'm creating a ListView full of trailer objects, which simply create strings for displaying in the listview, and a query parameter for youtube videos. When you click on a ListItem in the app, it is meant to open up a trailer in YouTube, given the query parameter for the youtube video (provided by the API).

Link to what is displayed; I'm referring to the bottom ListView: https://i.sstatic.net/DZNMI.png

Here is my fragment class onCreateView:

public  View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//....... (other views that function)
    ListView trailerView = (ListView) rootView.findViewById(R.id.trailer_listview);
    trailerView.setClickable(true);
    trailerView.setAdapter(mTrailerAdapter);
    trailerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Log.d(LOG_TAG, "item click");
        Trailer trailer = mTrailerAdapter.getItem(position);
        final String YT_BASE_URL = "http://www.youtube.com";
        final String YT_OPTION_PATH = "watch";
        final String YT_API_KEY_PARAM = "v";
        Uri builtUri = Uri.parse(YT_BASE_URL).buildUpon()
             .appendEncodedPath(YT_OPTION_PATH)
             .appendQueryParameter(YT_API_KEY_PARAM, trailer.getURLkey())
             .build();
        startActivity(new Intent(Intent.ACTION_VIEW, builtUri));
                    }
                });
}

Here is my Adapter:

public class TrailerAdapter extends ArrayAdapter<Trailer> {
    final String LOG_TAG = TrailerAdapter.class.getSimpleName();

    public TrailerAdapter(Activity context, List<Trailer> trailers){
        super(context, 0, trailers);
        //gets created
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        Trailer trailer = getItem(position);

        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_trailer, parent, false);
        }

        TextView trailerText = (TextView) convertView.findViewById(R.id.trailer_textview);
        trailerText.setText(trailer.getDisplayString());
        return convertView;
    }
}

Trailer class:

public class Trailer {
    private String DisplayString;
    private String URLkey;

    public Trailer(String DisplayString, String URLkey){
        this.DisplayString = DisplayString;
        this.URLkey = URLkey;
    }

    public String getDisplayString(){
        return DisplayString;
    }

    public String getURLkey(){
        return URLkey;
    }
}

A ListView of the trailers for the movies gets created, but the ListView doesn't correspond to any clicks; the app does register clicks, however (LogCat show's the 0's and 1's corresponding). Here is the XML as well:

ListView itself:

<LinearLayout>
....
    </ScrollView>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/trailer_listview"
        android:layout_marginLeft="25dp">
    </ListView>

</LinearLayout>

ListView Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <ImageView
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:id="@+id/trailer_playbutton"
        android:src="@drawable/video_play_button"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Trailer 1"
        android:gravity="center"
        android:layout_marginLeft="15dp"
        android:id="@+id/trailer_textview"/>
</LinearLayout>

THANKS!

Upvotes: 0

Views: 65

Answers (1)

Elgoph
Elgoph

Reputation: 63

I think you have focus issues problem. just put android:focusable="false" android:focusableInTouchMode="false" in all your ListView Item (both ImageView and TextView) "just try this xml"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true">

    <ImageView
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:id="@+id/trailer_playbutton"
        android:src="@drawable/video_play_button"
        android:layout_gravity="center"
        android:focusable="false" 
        android:focusableInTouchMode="false"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Trailer 1"
        android:gravity="center"
        android:layout_marginLeft="15dp"
        android:id="@+id/trailer_textview"
        android:focusable="false" 
        android:focusableInTouchMode="false"
        />
</LinearLayout>

Upvotes: 1

Related Questions