简体   繁体   English

在回收站视图中单击项目时显示按钮

[英]Show buttons when item is click in recycler view

I'm new to android 我是android的新手

I've this adapter class for listing contacts 我有这个适配器类用于列出联系人

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

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

    public ImageButton mCallButton, mMoreButton, mSmsButton, mEmailButton;
    public TextView mContactName;
    public List<Contact> mContactList;
    public Context mContext;

    public class MyViewHolder extends RecyclerView.ViewHolder implements OnClickListener{

        public MyViewHolder(View view) {
            super(view);
            mContactName = (TextView) view.findViewById(R.id.tv_contact_name);
            mCallButton = (ImageButton) view.findViewById(R.id.imgbtn_call);
            mMoreButton = (ImageButton) view.findViewById(R.id.imgbtn_more);
            mSmsButton = (ImageButton) view.findViewById(R.id.imgbtn_sms);
            mEmailButton = (ImageButton) view.findViewById(R.id.imgbtn_email);
            mContext = view.getContext();
            mCallButton.setOnClickListener(this);
            mMoreButton.setOnClickListener(this);
            mSmsButton.setOnClickListener(this);
            mEmailButton.setOnClickListener(this);
        }

        public void onClick(View v) {
            int index = getAdapterPosition();
            String number = mContactList.get(index).getContactNumber();
            String email = mContactList.get(index).getContactEmail();
            String name = mContactList.get(index).getContactName();
            if(v.getId() == mCallButton.getId()) {
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:"+number));
                mContext.startActivity(callIntent);
            }
            if(v.getId() == mMoreButton.getId()) {
                Log.d("Index kya hai?", index + "");
                mSmsButton.setVisibility(View.VISIBLE);
                mEmailButton.setVisibility(View.VISIBLE);
            }
            if(v.getId() == mSmsButton.getId()) {
                Intent smsIntent = new Intent(Intent.ACTION_VIEW);
                smsIntent.setType("vnd.android-dir/mms-sms");
                smsIntent.putExtra("address", number);
                mContext.startActivity(Intent.createChooser(smsIntent, "Send Sms To " + name + " (" + number + ")"));
            }
            if(v.getId() == mEmailButton.getId()) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
                mContext.startActivity(Intent.createChooser(emailIntent, "Send Email to " + email));
            }
        }
    }



    public ContactAdapter(List<Contact> contactList) {
        mContactList = contactList;
    }

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_contact_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    public void onBindViewHolder(MyViewHolder holder, int position) {
        Contact contact = mContactList.get(position);
        mContactName.setText(contact.getContactName());
    }

    public int getItemCount() {
        return mContactList.size();
    }
}

now there is a image button mMoreButton is has only one job of showing hidden image button mSmsButton , mEmailButton but whenever i click, the button do not show in correct index(?) 现在有一个图像按钮mMoreButton只有一个工作显示隐藏图像按钮mSmsButtonmEmailButton但是每当我点击时,按钮都没有显示正确的索引(?)

Let's say i clicked on index 5, but the mEmailButtton and mSmsButton will appear on random index say 10, I don't know what I'm doing wrong 假设我点击索引5,但mEmailButttonmSmsButton会出现在随机索引10上,我不知道我做错了什么

Move your views ( TextView and ImageButton ) to ViewHolder class so their reference will not always replaced. 将视图( TextViewImageButton )移动到ViewHolder类,因此不会始终替换它们的引用。 OR you can instead set the button click listener under the onBindViewHolder method, that's what I always do when creating custom recyclerview adapter. 或者您可以在onBindViewHolder方法下设置按钮单击侦听器,这是我在创建自定义recyclerview适配器时始终执行的操作。

ImageButton mSmsButton_last, mEmailButton_last;
public void onBindViewHolder(MyViewHolder holder, int position) {
    Contact contact = mContactList.get(position);
    holder.mContactName.setText(contact.getContactName());

    holder.mCallButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:"+number));
            mContext.startActivity(callIntent);
         }
    });

    holder.mMoreButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Log.d("Index kya hai?", index + "");
            holder.mSmsButton.setVisibility(View.VISIBLE);
            holder.mEmailButton.setVisibility(View.VISIBLE);
            if (mSmsButton_last != null && mEmailButton_last != null) {
                mSmsButton_last.setVisibility(View.GONE);
                mEmailButton_last.setVisibility(View.GONE);
            }
            mSmsButton_last = holder.mSmsButton;
            mEmailButton_last = holder.mEmailButton;
         }
    });

    holder.mSmsButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Intent smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", number);
            mContext.startActivity(Intent.createChooser(smsIntent, "Send Sms To " + name + " (" + number + ")"));
         }
    });

    holder.mEmailButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
            mContext.startActivity(Intent.createChooser(emailIntent, "Send Email to " + email));
         }
    });
}

and your ViewHolder will just look like this 而你的ViewHolder就像这样

public class MyViewHolder extends RecyclerView.ViewHolder{
    public ImageButton mCallButton, mMoreButton, mSmsButton, mEmailButton;
    public TextView mContactName;
    public MyViewHolder(View view) {
        super(view);
        mContactName = (TextView) view.findViewById(R.id.tv_contact_name);
        mCallButton = (ImageButton) view.findViewById(R.id.imgbtn_call);
        mMoreButton = (ImageButton) view.findViewById(R.id.imgbtn_more);
        mSmsButton = (ImageButton) view.findViewById(R.id.imgbtn_sms);
        mEmailButton = (ImageButton) view.findViewById(R.id.imgbtn_email);
        mContext = view.getContext();
    }
}

Hope this works for you. 希望这对你有用。 :) :)

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

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