尽管Android有ListView无法点击:Clickable =" true"

时间:2016-06-16 06:41:58

标签: android listview

我已经按照各种链接描述了这个问题的解决方案,到目前为止还没有解决办法,或者在我的案例中不适用。我想知道我的代码/自定义适配器是否存在问题?除了OnItemClickListener之外,我还尝试过使用XML进行点击。我尝试了各种可点击的配置(单个元素,线性布局和列表视图本身),并且没有此属性的归属。

我创建了一个包含预告片对象的ListView,它只是创建用于在列表视图中显示的字符串,以及用于youtube视频的查询参数。当您点击应用程序中的ListItem时,它意味着在YouTube中打开预告片,给出youtube视频的查询参数(由API提供)。

链接到显示的内容;我指的是底部的ListView: http://i.imgur.com/djM6r1G.png

这是我的片段类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));
                    }
                });
}

这是我的适配器:

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

预告片类:

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

创建了电影预告片的ListView,但ListView并不对应任何点击;然而,该应用程序确实记录了点击次数(LogCat显示的是0和1对应的)。这也是XML:

ListView本身:

<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项目:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你有焦点问题。刚刚把android:focusable =&#34; false&#34;机器人:focusableInTouchMode =&#34;假&#34;在你的所有ListView项目(包括ImageView和TextView)&#34;只需尝试这个xml&#34;

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