clickListener在Android应用程序中的GridView中不起作用

时间:2016-09-08 01:27:38

标签: android gridview android-adapter

我正在实现一个有GridView的Android应用程序,其中每个项目都是ImageView,下面有TextView,我实现了适配器并且GridView正确显示,但是我认为当我点击图像时,其他屏幕(活动已启动)但不幸的是,这不是我的代码:

gridView_layout:

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

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/videosGrid"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:numColumns="auto_fit"
        android:background="#000"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:columnWidth="120dp"
        android:stretchMode="spacingWidthUniform"></GridView>


</GridLayout>

grid_item_layout:

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


    <ImageView
        android:id="@+id/video_thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:minWidth="170dp"
        android:minHeight="140dp"
        android:scaleType="fitXY"
        android:clickable="true"/>
    <!-- android:src="@drawable/alternative_rock_music" -->

    <TextView android:id="@+id/video_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:textColor="@color/red"
        android:textStyle="bold"
        />


</LinearLayout>

适配器:

package com.app.utils;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.R;
import com.app.entities.VideoEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;



public class MyGridViewAdapter extends BaseAdapter {


    private static final String TAG =  MyGridViewAdapter.class.getSimpleName();

    private Context context;
    private List<VideoEntity> videosList;


    public MyGridViewAdapter(Context context) {
        this.context = context;


    }

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

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

    }

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

    public List<VideoEntity> getVideosList() {
        return videosList;
    }

    public void setVideosList(List<VideoEntity> videosList) {
        this.videosList = videosList;
    }

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


        Log.d(TAG," getView(int, View, viewGroup - Ini ");

        View row  =  convertView;
        ViewHolder holder = null;

        if(row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.video_item ,parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) row.getTag();
        }
        VideoEntity tmp = videosList.get(position);
        Picasso.with(context).load(tmp.getThumbnailURL()).into(holder.myVideo);
        holder.title.setText(tmp.getTitle());
        Log.d(TAG, " getView(int, View, viewGroup - Fi ");
        return row;

    }

    class ViewHolder {

        ImageView myVideo;
        TextView  title;

        public ViewHolder(View v) {

            Log.d(TAG," ViewHolder(View) - Ini ");
                myVideo = (ImageView) v.findViewById(R.id.video_thumbnail);
                title = (TextView) v.findViewById(R.id.video_title);
            Log.d(TAG," ViewHolder(View) - Fi ");
        }
    }

}

这是我应该在gridView的每个项目上捕获click事件的活动:

package com.app.activities

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;

import com.app.entities.VideoEntity;
import com.app.utils.MyGridViewAdapter;

import java.util.ArrayList;

import static android.widget.AdapterView.*;




public class VideosCatalogActivity extends Activity implements AdapterView.OnItemSelectedListener, AdapterView.OnClickListener{


    private static final String TAG =  VideosCatalogActivity.class.getSimpleName();

    private GridView videosGrid;

    private ArrayList<VideoEntity> videosList;

    @Override
    protected void onCreate(Bundle savedInstanceState){

        Log.d(TAG, " onCreate(Bundle) - Ini ");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.videos_catalog_layout);
        Bundle bundle =  getIntent().getExtras();
        videosList = bundle.getParcelableArrayList("com.app.entities.VideoEntity" );

        videosGrid = (GridView) findViewById(R.id.videosGrid);
        MyGridViewAdapter adapter = new MyGridViewAdapter(this);
        adapter.setVideosList(videosList);
        videosGrid.setAdapter(adapter);



        videosGrid.setOnItemSelectedListener(this);


        Log.d(TAG, " onCreate(Bundle) - Fi ");
    }


    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Ini ");

        Intent intent = new Intent(getApplicationContext(),YoutubePlayerActivity.class);
        intent.putExtra("VIDEO_ID",videosList.get(position).getId());
        startActivity(intent);

        Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Fi ");


    }


    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Ini ");

        Intent intent = new Intent(getApplicationContext(),YoutubePlayerActivity.class);
        intent.putExtra("VIDEO_ID",videosList.get(position).getId());
        startActivity(intent);

        Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Fi ");

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onClick(View v) {

    }

    /*@Override
    public void onNothingSelected(AdapterView<?> parent) {

    } */
}

我尝试实现两个不同的onClickListeners方法但没有一个工作。 我不知道我做错了什么???

任何帮助将非常感谢。

注意:我可以确认这不是一个明显的问题,因为我调试了,当我点击图片时没有任何反应。

3 个答案:

答案 0 :(得分:0)

更改

`implements AdapterView.OnItemSelectedListener, AdapterView.OnClickListener`

`implements AdapterView.OnItemClickListener`

更改

`videosGrid.setOnItemSelectedListener(this)`

`videosGrid.setOnItemClickListener(this)`

在ImageView中删除android:clickable="true"

答案 1 :(得分:0)

删除两个接口 AdapterView.OnItemSelectedListener AdapterView.OnClickListener和各自的方法。

并实施AdapterView.OnItemClickListener

答案 2 :(得分:0)

您需要澄清 [0, period] x = 21 * np.pi / 8 x_equiv = x % period # Now within [0, period] interp_points = np.hstack(( (x_equiv * np.ones(4))[:, np.newaxis], yp[:, np.newaxis] )) result = interpn((xp, yp), fp, interp_points) # >>> result # array([-0.5, 0.5, 1.5, 2.5]) interp_points 的区别。

您的活动应实施 AdapterView.OnItemClickListener ,覆盖它,您将获得商品点击事件。请务必添加以下行: AdapterView.OnItemSelectedListener