单击recyclerview中的textview不起作用,单击按钮起作用

pbpqsu0x  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(435)

现在我的recyclerview工作了,我可以在我的活动和适配器之间通信了,我想创建一个onclicklistener,我成功地做到了。
但不知怎么的,它只对按钮有效,对其他项目无效。
在我的recyclerview的布局中,我有以下内容:

<TextView
        android:id="@+id/tvWebsite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:singleLine="true"
        android:text="tvWebsite" />

        <Button
            android:id="@+id/btnWebsite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />

所以文本视图是可点击的。按钮也是,但这是默认行为。
在我的适配器类中,我有以下内容:

public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.ViewHolder>
{

    private Context mContext;
    private ArrayList<Company> companyname;

    public CompanyAdapter (Context context, ArrayList<Company> list)
    {
        this.mContext = context;
        companyname = list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        TextView tvCompanyName;
        TextView tvVatID;
        TextView tvPostalCode;
        TextView tvPhoneNumber;
        TextView tvProprietorName;
        TextView tvWebsite;
        TextView tvEmail;
        TextView tvFirstAvailableDate;
        TextView tvFirstAvailableTimeBlock;
        TextView tvAvailableInPostalCode;
        TextView tvAvailableNumberOfPersons;
        Button btnWebsite;

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

            tvCompanyName = itemView.findViewById(R.id.tvCompanyName);
            tvVatID = itemView.findViewById(R.id.tvVatNo);
            tvPostalCode = itemView.findViewById(R.id.tvPostalCode);
            tvPhoneNumber = itemView.findViewById(R.id.tvPhoneNumber);
            tvProprietorName = itemView.findViewById(R.id.tvProprietorName);
            tvWebsite = itemView.findViewById(R.id.tvWebsite);
            tvEmail = itemView.findViewById(R.id.tvEmail);
            tvFirstAvailableDate = itemView.findViewById(R.id.tvFrstAvailableDate);
            tvFirstAvailableTimeBlock = itemView.findViewById(R.id.tvFirstAvailableTimeBlock);
            tvAvailableInPostalCode = itemView.findViewById(R.id.tvPostalCodeAvailable);
            tvAvailableNumberOfPersons = itemView.findViewById(R.id.tvAvailableNumberOfPersons);
            btnWebsite = itemView.findViewById(R.id.btnWebsite);
            tvWebsite.setClickable(true);

            tvWebsite.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View view) {
                    if (mContext instanceof SearchForHelp) {
                        ((SearchForHelp) mContext).ClickWebsite();
                    }

                }
            });

            btnWebsite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mContext instanceof SearchForHelp) {
                        ((SearchForHelp) mContext).ClickButton();
                    }
                }
            });

        }
        @Override
        public void onClick(View view) {

        }

    }

    @NonNull
    @Override
    public CompanyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull CompanyAdapter.ViewHolder viewHolder, int i) {

        viewHolder.itemView.setTag(companyname.get(i));

        viewHolder.tvCompanyName.setText(companyname.get(i).getStrCompanyName());
        viewHolder.tvVatID.setText(companyname.get(i).getStrVatID());
        viewHolder.tvPostalCode.setText(companyname.get(i).getStrPostalCode());
        viewHolder.tvPhoneNumber.setText(companyname.get(i).getStrPhoneNumber());
        viewHolder.tvProprietorName.setText(companyname.get(i).getStrProprietorName());
        viewHolder.tvWebsite.setText(companyname.get(i).getStrWebsite());
        viewHolder.tvEmail.setText(companyname.get(i).getStrEmail());
        viewHolder.tvFirstAvailableDate.setText(companyname.get(i).getStrFirstAvailableDate());
        viewHolder.tvFirstAvailableTimeBlock.setText(companyname.get(i).getStrFirstAvailableTimeBlock());
        viewHolder.tvAvailableInPostalCode.setText(companyname.get(i).getStrAvailableInPostalCode());
        viewHolder.tvAvailableNumberOfPersons.setText(companyname.get(i).getStrAvailableNumberOfPersons());

        viewHolder.tvWebsite.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View view) {
                //Toast.makeText(getClass(),"Test",Toast.LENGTH_SHORT).show();

                //viewHolder.tvCompanyName.setText("Changed");

            }
        });

    }

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

最后,在我的活动中:

public void ClickButton()
{
    Toast.makeText(getApplicationContext(),"Clicked on Button",Toast.LENGTH_LONG).show();
}

public void ClickWebsite()
{
    Toast.makeText(getApplicationContext(),"Clicked on TextView",Toast.LENGTH_LONG).show();
}

请注意

tvWebsite.setClickable(true);

已经添加了,看看它是否有什么不同,但它没有。
如果我点击这个按钮,它会显示“点击按钮”,但是如果我点击文本视图,什么都不会发生。
那么,为了让textview电视网站可以点击,我错过了什么呢?

0sgqnhkj

0sgqnhkj1#

没有必要这样做 tvWebsite.setClickable(true); 您正在为设置click listener tvWebsiteonBindViewHolder 再次回拨。
您需要从中删除此代码 onBindViewHolder 方法。

viewHolder.tvWebsite.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View view) {
                //Toast.makeText(getClass(),"Test",Toast.LENGTH_SHORT).show();

                //viewHolder.tvCompanyName.setText("Changed");

            }
        });

---------休息很好。

相关问题