我是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上,我不知道我做错了什么
答案 0 :(得分:0)
将您的观看次数(<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/splash_color"
android:weightSum="100">
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50">
<com.rey.material.widget.ImageView
android:id="@+id/welcome_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/welcome"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/welcomeRelativeTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:padding="@dimen/universal_margin4"
>
<com.rey.material.widget.TextView
android:id="@+id/Ready"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_ready"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="@dimen/universal_margin2"
android:layout_centerInParent="true"
/>
<com.rey.material.widget.TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_descrition"
android:layout_below="@+id/Ready"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/universal_margin"
/>
<com.rey.material.widget.Button
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/welcomeBtn"
app:rd_enable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:text="@string/welcome_btntext"
android:textColor="@color/splash_color"
android:background="@color/colorAccent"
android:layout_marginTop="@dimen/universal_margin2"
/>
</RelativeLayout>
<!-- Your RelativeLayout Items -->
</LinearLayout>
和TextView
)移至ImageButton
课程,以便不会始终替换他们的观点。或者您可以在ViewHolder
方法下设置按钮单击侦听器,这是我在创建自定义Recyclerview适配器时始终执行的操作。
onBindViewHolder
,您的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
希望这适合你。 :)