-1

我正在構建一個使用RecyclerView和CardView的應用程序。由於RecyclerView沒有onItemClickListener方法,我找到了一個這樣做的類實現,並且能夠將此類插入到我的項目中。將RecyclerView放在圖像前

在每張卡片中,我將一個imageView(我正在使用Glide加載圖像)和一對TextViews。當TextView低於圖像時,我可以完美地使用onItemClickListener,但我決定將圖像放在背景爲黑色透明的RelativeLayout中。

問題是,在此更改後onItemClickListener停止工作。圖像佔據整個cardView並且TextView位於其前面。通過點擊這些元素中的任何一個,該方法都不起作用。我認爲我需要將recyclerView放在活動的前面,但我不知道如何去做。我試圖使用recyclerView.bringToFront(),但它不起作用。

這是(在白色區域點擊時,點擊監聽器工作)前,把卡:

enter image description here

這是卡現在(當我點擊任何地方點擊監聽器不工作):

enter image description here

這是我ItemClickSupport類:

public class ItemClickSupport { 
    private final RecyclerView mRecyclerView; 
    private OnItemClickListener mOnItemClickListener; 
    private OnItemLongClickListener mOnItemLongClickListener; 
    private View.OnClickListener mOnClickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (mOnItemClickListener != null) { 
       RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 
       mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v); 
      } 
     } 
    }; 
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      if (mOnItemLongClickListener != null) { 
       RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 
       return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v); 
      } 
      return false; 
     } 
    }; 
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener 
      = new RecyclerView.OnChildAttachStateChangeListener() { 
     @Override 
     public void onChildViewAttachedToWindow(View view) { 
      if (mOnItemClickListener != null) { 
       view.setOnClickListener(mOnClickListener); 
      } 
      if (mOnItemLongClickListener != null) { 
       view.setOnLongClickListener(mOnLongClickListener); 
      } 
     } 

     @Override 
     public void onChildViewDetachedFromWindow(View view) { 

     } 
    }; 

    private ItemClickSupport(RecyclerView recyclerView) { 
     mRecyclerView = recyclerView; 
     mRecyclerView.setTag(R.id.item_click_support, this); 
     mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener); 
    } 

    public static ItemClickSupport addTo(RecyclerView view) { 
     ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support); 
     if (support == null) { 
      support = new ItemClickSupport(view); 
     } 
     return support; 
    } 

    public static ItemClickSupport removeFrom(RecyclerView view) { 
     ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support); 
     if (support != null) { 
      support.detach(view); 
     } 
     return support; 
    } 

    public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) { 
     mOnItemClickListener = listener; 
     return this; 
    } 

    public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) { 
     mOnItemLongClickListener = listener; 
     return this; 
    } 

    private void detach(RecyclerView view) { 
     view.removeOnChildAttachStateChangeListener(mAttachListener); 
     view.setTag(R.id.item_click_support, null); 
    } 

    public interface OnItemClickListener { 

     void onItemClicked(RecyclerView recyclerView, int position, View v); 
    } 

    public interface OnItemLongClickListener { 

     boolean onItemLongClicked(RecyclerView recyclerView, int position, View v); 
    } 
} 

這是我從我的活動稱之爲:

ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() { 
      @Override 
      public void onItemClicked(RecyclerView recyclerView, int position, View v) { 

       Intent intent = new Intent(getBaseContext(), MovieDetails.class); 
       intent.putExtra("movie_name", movies.get(position).getTitle()); 
       intent.putExtra("movie_rating", movies.get(position).getVoteAverage().toString()); 
       intent.putExtra("movie_description", movies.get(position).getOverview()); 
       intent.putExtra("movie_cover", movies.get(position).getPosterPath()); 
       intent.putExtra("movie_date", movies.get(position).getReleaseDate()); 
       intent.putExtra("movie_language", movies.get(position).getOriginalLanguage()); 
       intent.putExtra("movie_backdrop", movies.get(position).getBackdropPath()); 
       //intent.putExtra("album_name", albumList.get(position).getName()); 
       //intent.putExtra("album_thumbnail", Integer.toString(albumList.get(position).getThumbnail())); 
       //intent.putExtra("number_songs", Integer.toString(albumList.get(position).getNumOfSongs())); 

       startActivity(intent); 
      } 
     }); 

這是我的適配器:

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> { 

    private Context mContext; 
    private List<MovieItem> movieList; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView title, count; 
     public ImageView thumbnail, overflow, star1, star2, star3, star4, star5; 

     public MyViewHolder(View view) { 
      super(view); 
      title = (TextView) view.findViewById(R.id.title); 
      count = (TextView) view.findViewById(R.id.count); 
      thumbnail = (ImageView) view.findViewById(R.id.thumbnail); 
      star1 = (ImageView) view.findViewById(R.id.rating_image1); 
      star2 = (ImageView) view.findViewById(R.id.rating_image2); 
      star3 = (ImageView) view.findViewById(R.id.rating_image3); 
      star4 = (ImageView) view.findViewById(R.id.rating_image4); 
      star5 = (ImageView) view.findViewById(R.id.rating_image5); 
      //overflow = (ImageView) view.findViewById(R.id.overflow); 
     } 
    } 


    public MoviesAdapter(Context mContext, List<MovieItem> movieList) { 
     this.mContext = mContext; 
     this.movieList = movieList; 
    } 

    @Override 
    public MoviesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.album_card, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final MoviesAdapter.MyViewHolder holder, int position) { 
     MovieItem movie = movieList.get(position); 
     holder.title.setText(movie.getTitle()); 
     holder.count.setText("R: " + movie.getVoteAverage().toString() + "/10"); 

     Glide.with(mContext).load("https://image.tmdb.org/t/p/w500" + movie.getPosterPath()).into(holder.thumbnail); 

    } 


    @Override 
    public int getItemCount() { 
     return movieList.size(); 
    } 
} 
+2

嘗試onItemClickListener,它會在你點擊cardview不論TextView的或imageview的我希望它會幫助你可以分享你的適配器代碼工作。 。 – Sahil

+0

? –

+0

感謝您的答覆!我只是將我的適配器添加到代碼中。 –

回答

0

我設法做什麼薩赫勒建議我解決我的問題。我把onClickListener放在圖片上而不是回收站視圖中。要做到這一點,我不得不把代碼放在我的Adapter中的onBindViewHolder方法(我不知道我可以這樣做)。

它是這樣的: (我不得不聲明變量int位置最終以訪問它在onClickListener並設置標誌Intent.FLAG_ACTIVITY_NEW_TASK因爲我開始從一個活動之外的另一個活動

在cardView而不是回收視圖
@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) { 

holder.thumbnail.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(mContext, MovieDetails.class); 
       intent.putExtra("movie_name", movieList.get(position).getTitle()); 
       intent.putExtra("movie_rating", movieList.get(position).getVoteAverage().toString()); 
       intent.putExtra("movie_description", movieList.get(position).getOverview()); 
       intent.putExtra("movie_cover", movieList.get(position).getPosterPath()); 
       intent.putExtra("movie_date", movieList.get(position).getReleaseDate()); 
       intent.putExtra("movie_language", movieList.get(position).getOriginalLanguage()); 
       intent.putExtra("movie_backdrop", movieList.get(position).getBackdropPath()); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       //intent.putExtra("album_name", albumList.get(position).getName()); 
       //intent.putExtra("album_thumbnail", Integer.toString(albumList.get(position).getThumbnail())); 
       //intent.putExtra("number_songs", Integer.toString(albumList.get(position).getNumOfSongs())); 

       mContext.startActivity(intent); 
      } 
     }); 
}