简体   繁体   English

获取RecyclerView的选中项position

[英]Get selected item position of RecyclerView

I am trying to get position of the clicked item in RecyclerView.我正在尝试获取 RecyclerView 中单击项目的 position。 In adapter class, I am trying to create intent and send clicked item's contents.在适配器 class 中,我正在尝试创建意图并发送单击项目的内容。 Also I created popup menu for two option.我还为两个选项创建了弹出菜单。 It is in adapter class too.它也在适配器 class 中。 onBindViewHolder position is works for puting contents in the rows of RecyclerView. onBindViewHolder position 用于将内容放入 RecyclerView 的行中。 However I couldn't reach that position from outside of method.但是我无法从方法之外到达那个 position。

This is adapter class.这是适配器 class。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements PopupMenu.OnMenuItemClickListener {

    public ArrayList<MovieModel.Results> resultList;
    public Context context;


    public RecyclerViewAdapter(ArrayList<MovieModel.Results> list, Context context) {
        this.resultList = list;
        this.context = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView posterImage;
        ImageView addIcon;
        TextView movieName;
        TextView date;
        TextView overview;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            posterImage = itemView.findViewById(R.id.movie_poster);
            addIcon = itemView.findViewById(R.id.addImage);
            movieName = itemView.findViewById(R.id.movieNameText);
            date = itemView.findViewById(R.id.dateText);
            overview = itemView.findViewById(R.id.overviewText);

        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_list_2, parent, false);
        return new ViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (position < getItemCount()) {
            String title = resultList.get(position).getTitle();
            String date = resultList.get(position).getRelease_date();
            String overview = resultList.get(position).getOverview();
            String posterPath = resultList.get(position).getPoster_path();

            holder.movieName.setText(title);
            holder.date.setText(date);
            holder.overview.setText(overview);

            if (posterPath == null) {
                holder.posterImage.setImageDrawable(context.getDrawable(R.drawable.no_poster));
            } else {
                Glide.with(context).load(FeedActivity.BASE_PHOTO_URL + posterPath).into(holder.posterImage);
            }


            int finalPosition = position;
            holder.addIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(context, "You clicked : " + finalPosition, Toast.LENGTH_SHORT).show();

                    showPopup(v);
                }
            });
        }
        position++;
    }


    private void showPopup(View v) {

        PopupMenu popup = new PopupMenu(context,v);
        popup.setOnMenuItemClickListener(this);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu, popup.getMenu());
        popup.show();

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        // Here I need to position of clicked item in Recyclerview, and I will get title, date etc.

        int pos;

        if(item.getItemId() == R.id.menuAdd){

            // Find here to get position of clicked movie.
            String title = resultList.get(pos).getTitle();
            String date = resultList.get(pos).getRelease_date();
            String overview = resultList.get(pos).getOverview();
            String posterpath = resultList.get(pos).getPoster_path();

            Intent intent1 = new Intent(context,ListActivity.class);
            intent1.putExtra("title",title);
            intent1.putExtra("date",date);
            intent1.putExtra("overview",overview);
            intent1.putExtra("posterpath",posterpath);
            context.startActivity(intent1);
            return true;

        }
        else if(item.getItemId() == R.id.menuShowDetails){

            int movieId = resultList.get(pos).getMovieId();

            Intent intent1 = new Intent(context, MovieDetails.class);
            intent1.putExtra("movie_id",movieId);
            context.startActivity(intent1);
            return true;

        }

        return false;
    }


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

}

You can use the ViewHolder's getAdapterPosition() to retrieve the item's position within an interface method.您可以使用 ViewHolder 的getAdapterPosition()在接口方法中检索项目的 position。 Then store the clicked position in a member variable.然后将点击的 position 存储在成员变量中。

Additionally, there shouldn't be a need to call position++ from within your onBindViewHolder .此外,不需要从onBindViewHolder中调用position++

// Create a member variable to store the clicked position
public int clickedPos = -1;

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
    // ...
    
    holder.addIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // When you're inside the click listener interface,
            // you can access the position using the ViewHolder.
            // We'll store the position in the member variable in this case.
            clickedPos = holder.getAdapterPosition();
        }
    });
    
    // Remove the 'position++' call as the position should already be handled without explicitly updating it.
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // You can use clickedPos here to perform whatever tasks you need.
    
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM