2016-11-13 103 views
0

我遇到了RecyclerViewGridLayoutManager不適用於onClick()的情況。像ListView,當我點擊/點擊GridView中的圖片時,onClick不會返回單個項目的詳細信息。當我點擊一部電影的海報時,它應該只顯示關於特定電影的詳細信息,而是在一個滾動視圖中顯示多個電影的詳細信息。Android RecyclerView GridLayoutManager onClick現在正在工作

看看下面的代碼:

public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> { 

    private Context context; 
    private List<Movies> movieItems = new ArrayList<>(); 
    CustomItemClickListener listener; 

    public MoviesGridAdapter(Context context, List<Movies> movieItems) { 
     this.context = context; 
     this.movieItems = movieItems; 
    } 


    @Override 
    public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false); 
     final MoviesGridAdapter.PosterViewHolder vh = new MoviesGridAdapter.PosterViewHolder(view); 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       final Context zContext = context; 
       Intent intent = new Intent(zContext, MoviesDetailActivity.class); 
       Movies movies = movieItems.get(vh.getAdapterPosition()); 
       intent.putExtra("Items Selection", movies.getPosterPath()); 
       intent.putExtra("Items Selection 1", movies.getTitle()); 
       intent.putExtra("Items Selection 2", movies.getReleaseDate()); 
       intent.putExtra("Items Selection 3", movies.getVoteAverage()); 
       intent.putExtra("Items Selection 4", movies.getOverview()); 
       zContext.startActivity(intent); 
      } 
     }); 
     return vh; 

//  return new PosterViewHolder(view); 
    } 

    public void setMoviesData(List<Movies> moviesData) { 
     movieItems = moviesData; 
     notifyDataSetChanged(); 
    } 


    @Override 
    public void onBindViewHolder(PosterViewHolder holder, final int position) { 

     Context mcontext = context; 
     Movies movies = movieItems.get(position); 

     String posterUrlPath = movies.getPosterPath(); 

     Picasso.with(mcontext) 
       .load(Constants.MOVIES_POSTER_URL + posterUrlPath) 
       .into(holder.poster); 

    } 

    @Override 
    public int getItemCount() { 
     return movieItems.size(); 
    } 

    public class PosterViewHolder extends RecyclerView.ViewHolder { 
     ImageView poster; 
//  Context zContext = context; 

     public PosterViewHolder(final View itemView) { 
      super(itemView); 

//   zContext = itemView.getContext(); 
      poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit); 
//   itemView.setOnClickListener(this); 
     } 

     /*@Override 
     public void onClick(View view) { 

      Movies movies = movieItems.get(getAdapterPosition()); 
      Intent intent = new Intent(zContext, MoviesDetailActivity.class); 
      intent.putExtra("Intent", movies); 
      zContext.startActivity(intent); 
     }*/ 
    } 

} 

而且下面是片段:你們的關注

public class MoviesFragment extends Fragment { 

    private static final int COLUMN_COUNT = 2; 
    private List<Movies> movieItems = new ArrayList<>(); 
    private RecyclerView recyclerView; 
    private MoviesGridAdapter adapter; 
    private Subscription subscription; 

    public MoviesFragment() { 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.movies_fragment_content, container, false); 

     GridLayoutManager layoutManager = new GridLayoutManager(getContext(), COLUMN_COUNT); 
     recyclerView = (RecyclerView) view.findViewById(R.id.movies_recycler_view); 
     recyclerView.setHasFixedSize(true); 
     recyclerView.setLayoutManager(layoutManager); 

     adapter = new MoviesGridAdapter(getActivity(), movieItems); 
     recyclerView.setAdapter(adapter); 

     return view; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     if (movieItems != null) { 
      adapter.setMoviesData(movieItems); 
     } 
     loadDate(); 
    } 

    private void loadDate() { 
     MoviesAPI api = RetrofitManager.getMoviesClient().create(MoviesAPI.class); 

     subscription = api.getPopularMovies(APIKeys.MOVIES_DB_API_KEY) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribeOn(Schedulers.io()) 
       .subscribe(
         new Action1<MoviesResponse>() { 
          @Override 
          public void call(MoviesResponse response) { 
           movieItems = response.getMovies(); 
           adapter.setMoviesData(response.getMovies()); 
          } 
         }, new Action1<Throwable>() { 
          @Override 
          public void call(Throwable throwable) { 

          } 
         } 
       ); 
    } 

} 

感謝。我正在尋找的解決方案是:當我點擊網格視圖中的圖片時,我需要關於諸如標題,概述,發佈日期等特定電影的詳細信息。

回答

2

使您的模型可以parcelable或逐個發送模型字段,但使用不同的關鍵字。你將在這裏與不同的值相同的關鍵字:

intent.putExtra("Items Selection", movies.getPosterPath()); 
       intent.putExtra("Items Selection", movies.getTitle()); 
       intent.putExtra("Items Selection", movies.getReleaseDate()); 
       intent.putExtra("Items Selection", movies.getVoteAverage()); 
       intent.putExtra("Items Selection", movies.getOverview()); 

這裏有一個教程鏈接,你如何讓你的模型parcelable。之後,您可以更新您的適配器,如下所示:

public class MoviesGridAdapter extends RecyclerView.Adapter<MoviesGridAdapter.PosterViewHolder> { 

    private Context context; 
    private List<Movies> movieItems = new ArrayList<>(); 
    CustomItemClickListener listener; 

    public MoviesGridAdapter(Context context, List<Movies> movieItems) { 
     this.context = context; 
     this.movieItems = movieItems; 
    } 


    @Override 
    public PosterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_view_item, parent, false); 
     return new PosterViewHolder(view); 
    } 

    public void setMoviesData(List<Movies> moviesData) { 
     movieItems = moviesData; 
     notifyDataSetChanged(); 
    } 


    @Override 
    public void onBindViewHolder(PosterViewHolder holder, final int position) { 

     Movies movie = movieItems.get(position); 

     String posterUrlPath = movie.getPosterPath(); 

     Picasso.with(mcontext) 
       .load(Constants.MOVIES_POSTER_URL + posterUrlPath) 
       .into(holder.poster); 

     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) {    
       Intent intent = new Intent(context, MoviesDetailActivity.class);   
       intent.putExtra("movie", movie) 
       context.startActivity(intent); 
      } 
     }); 

    } 

    @Override 
    public int getItemCount() { 
     return movieItems.size(); 
    } 

    public class PosterViewHolder extends RecyclerView.ViewHolder { 
     ImageView poster; 
     public PosterViewHolder(View itemView) { 
      super(itemView); 
      poster = (ImageView) itemView.findViewById(R.id.movies_image_view_retrofit); 
     } 
    } 

} 
+0

不能。沒有爲我工作。我嘗試了你的建議。 'onClick'本身就很好。我需要像'ListIView'中的'OnItemSelectedClick'方法。 – Ronan

+0

對不起,它應該是onBindViewHolder中的holder.itemView.setOnClickListener。元素的視圖保存在ViewHolder的itemView字段中,因此您可以通過將元素設置爲itemView來將偵聽器添加到元素中。 –

+0

'holder.mView.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(查看視圖){ 意圖意圖=新意圖(上下文,MovieDetailsActivity.class); intent.putExtra( 「標題」, movies.getTitle()); intent.putExtra(「Overview」,movies.getOverview()); context.startActivity(intent); } });' 這是行不通的。當我點擊時,它意圖展示該活動,但它在NestedScrollView中列出了20秒。我只想要關於一部電影的細節。 – Ronan

1

請勿將您的點擊偵聽器設置爲onCreateViewHolder。取而代之的將您的聽衆設置爲onBindViewHolder方法

+0

試過了。不起作用。我在Adapter類上實現了'View.OnClickListener',並在'onBindViewHolder'上添加'holder.poster.setOnClickListener(this)'。不起作用。我再次點擊多個電影細節 – Ronan

相關問題