我的 getBindingAdapterrPosition 只返回 -1

Posted

技术标签:

【中文标题】我的 getBindingAdapterrPosition 只返回 -1【英文标题】:My getBindingAdapterrPosition only returns -1 【发布时间】:2021-08-01 20:13:16 【问题描述】:

我正在尝试在我的应用程序上实现一项功能,其中 - 如果您单击 RecyclerView 项目 - 它会为该项目打开一个带有图片的对话框 - 有点像 InstaGram。

但是,我正在尝试制作一个 onClickListener,我可以在其中获取适配器的位置。目标是,它会读取我单击的项目,因此它可以为该特定项目打开一个对话框。但是无论我点击recyclerview中的哪个项目,它都会返回位置为-1。

这是我的代码:

UserAdapter.java

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.myndapplication.R;
import com.example.android.myndapplication.model.User;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> 

    private Context mContext;
    private List<User> userList;
    Dialog myDialog;



    public UserAdapter(Context mContext, List<User> userList) 
        this.mContext = mContext;
        this.userList = userList;

    

    @NonNull
    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);

        final ViewHolder ViewHolder = new ViewHolder(view);

         ViewHolder.item.setOnClickListener(new View.OnClickListener() 

             @Override
             public void onClick(View v) 
                 Toast.makeText(mContext, "Test Click"+String.valueOf(ViewHolder.getBindingAdapterPosition()), Toast.LENGTH_SHORT).show();
             
         );
        return new ViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) 
        User user = userList.get(position);
        holder.imageView.setImageResource(userList.get(position).getImage());
        holder.textView.setText(userList.get(position).getTitle());

    

    @Override
    public int getItemCount() 
        return userList.size();
    

    public class ViewHolder extends RecyclerView.ViewHolder 

        private CardView item;
        private CircleImageView iv_profile;
        private TextView tv_username;
        private TextView tv_date;
        private ImageView iv_content;
        private ImageView imageView;
        private TextView textView;


        public ViewHolder(View itemView) 
            super(itemView);

            imageView = itemView.findViewById(R.id.img_bookmarks);
            textView = itemView.findViewById(R.id.title_bookmarks);
            item = (CardView) itemView.findViewById(R.id.profile_item);
            iv_profile = (CircleImageView) itemView.findViewById(R.id.dialog_profile_image);
            tv_username = (TextView) itemView.findViewById(R.id.dialog_profile_username);
            tv_date = (TextView) itemView.findViewById(R.id.dialog_profile_date);
            iv_content = (ImageView) itemView.findViewById(R.id.dialog_content_image);

        
    


UserRecycler1.java

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.android.myndapplication.R;
import com.example.android.myndapplication.adapter.UserAdapter;
import com.example.android.myndapplication.model.User;

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

public class UserRecycler1 extends Fragment 

    private RecyclerView recyclerView;
    private UserAdapter adapter;
    private List<User> userList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        userList = new ArrayList<>();
        User user1 = new User(R.drawable.annapelzer, "Salad");
        User user2 = new User(R.drawable.***elark_1, "Pasta");
        User user3 = new User(R.drawable.***elark_2, "Fruit Salad");
        User user4 = new User(R.drawable.***elark_3, "Smoothies with Fruit");
        User user5 = new User(R.drawable.cala, "Soup");
        User user6 = new User(R.drawable.davide_cantelli, "Lobster Salad");
        User user7 = new User(R.drawable.joseph_gonzales, "Breakfast toast with Berries");


        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);
        userList.add(user7);

    

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

        recyclerView = view.findViewById(R.id.recycler1);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        adapter = new UserAdapter(getActivity(), userList);
        recyclerView.setAdapter(adapter);

        return view;
    

MainFragmentTab.java

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.android.myndapplication.R;
import com.example.android.myndapplication.adapter.TabAdapter;

public class MainFragmentTab extends Fragment 

    private TabAdapter adapter;
    private TabLayout tableLayout;
    private ViewPager viewPager;

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

        viewPager = view.findViewById(R.id.request_orders_view_pager);
        tableLayout = view.findViewById(R.id.request_orders_tabs);

        adapter = new TabAdapter(getFragmentManager());
        adapter.addFragment(new UserRecycler1(), "Public Bookmarks");
        adapter.addFragment(new UserRecycler1(), "Latest Posts");

        viewPager.setAdapter(adapter);
        tableLayout.setupWithViewPager(viewPager);

        return view;
    

recycler_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_
    android:layout_
    app:cardUseCompatPadding="true"
    app:cardCornerRadius="8dp"
    android:id="@+id/profile_item">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img_bookmarks"
            android:layout_
            android:layout_
            android:src="@mipmap/ic_launcher"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/title_bookmarks"
            android:layout_
            android:layout_
            android:gravity="center"
            android:text="@string/title2"
            android:textColor="@color/dark_green"
            android:padding="5dp"
            android:textSize="16sp" />

    </LinearLayout>
</androidx.cardview.widget.CardView>

希望大家帮忙,因为我已经尝试了一段时间,但无论我做什么,都会继续收到相同的输出。

【问题讨论】:

【参考方案1】:

您必须将您的 setOnClickListeneronCreateViewHolder 移动到 onBindViewHolder

您可能想查看this tutorial

编辑

类似这里:

@NonNull
@Override
public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);

    final ViewHolder ViewHolder = new ViewHolder(view);
    return new ViewHolder(view);


@Override
public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) 
    User user = userList.get(position);
    holder.imageView.setImageResource(userList.get(position).getImage());
    holder.textView.setText(userList.get(position).getTitle());
    holder.item.setOnClickListener(new View.OnClickListener() 

         @Override
         public void onClick(View v) 
             Toast.makeText(mContext, "Test Click"+userList.get(position), Toast.LENGTH_SHORT).show();
         
     );


【讨论】:

我把它移到onBindViewHolder,但现在它说:final ViewHolder ViewHolder = new ViewHolder(view) - 无法解析符号'view' 你不需要那行。只需添加holder.item.setOnClickListener 我删除了那行,但现在我的 ViewHolder.getBindingAdapterPosition()) 说 - 不能从静态上下文引用非静态方法 'getBindingAdapterPosition()' 现在的代码: public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, final int position) holder.imageView.setImageResource(userList.get(position).getImage()); holder.textView.setText(userList.get(position).getTitle()); holder.item.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) Toast.makeText(mContext, "Test Click"+String.valueOf(ViewHolder.getBindingAdapterPosition()), Toast.LENGTH_SHORT)。显示(); ); 请看我贴的教程或代码

以上是关于我的 getBindingAdapterrPosition 只返回 -1的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 ListView 使我的应用程序崩溃而不是列出我的条目?

我的 tensorflow 没有检测到我的 gpu 并使用我的 cpu(机器学习)

我的累积快照设计是不是适合我的 DW 和我的需要

2016我的时间我的生存

为啥我的 php 没有将我的联系表单输入发送到我的电子邮件?

2021-2022-2 ACM集训队每周程序设计竞赛(10) - 问题 B: 你的就是我的,我的还是我的 - 题解