[英]Show a sub item on the recycler view after click on an item in the recycler view
[英]Show buttons when item is click in recycler view
我是android的新手
我有这个适配器类用于列出联系人
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();
}
}
现在有一个图像按钮mMoreButton
只有一个工作显示隐藏图像按钮mSmsButton
, mEmailButton
但是每当我点击时,按钮都没有显示正确的索引(?)
假设我点击索引5,但mEmailButtton
和mSmsButton
会出现在随机索引10上,我不知道我做错了什么
将视图( TextView
和ImageButton
)移动到ViewHolder
类,因此不会始终替换它们的引用。 或者您可以在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));
}
});
}
而你的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();
}
}
希望这对你有用。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.