Recyclerview电话联系Android工作室

时间:2018-04-25 05:02:00

标签: android android-studio android-recyclerview

我有一个Android应用程序,我使用了RecyclerView来显示联系人列表。现在我想要的是,在选择联系人时,请拨打指定的号码。

现在它所做的只是显示联系人,但当我点击它时,它会调用,它不会执行任何操作。我不知道如何标记

我尝试使用onClick()和按钮项目。但是不成功

我的代码:

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

    private static final String TAG = "RecyclerViewAdapter";

    Context mContext;
    List<Contact> mData;
    Dialog myDialog;
    Button button;



    public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v ;
        v = LayoutInflater.from(mContext).inflate(R.layout.single_item_contact,parent,false);
        final MyViewHolder vHolder = new MyViewHolder(v);

        // Dialog ini

        myDialog = new Dialog(mContext);
        myDialog.setContentView(R.layout.dialog_contact);
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));





        vHolder.item_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(mContext,"Text Click item : "+String.valueOf(vHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show();


                TextView dialog_name_tv = (TextView) myDialog.findViewById(R.id.dialog_name_id);
                TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
                ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
                dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
                dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
                dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());


                myDialog.show();
            }
        });


        return vHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_phone.setText(mData.get(position).getPhone());
        holder.img.setImageResource(mData.get(position).getPhoto());



    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout item_contact;
        private TextView tv_name;
        private TextView tv_phone;
        private ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);

            item_contact = (LinearLayout) itemView.findViewById(R.id.contact_item_id);
            tv_name = (TextView) itemView.findViewById(R.id.name_contact);
            tv_phone = (TextView) itemView.findViewById(R.id.phone_contact);
            img = (ImageView) itemView.findViewById(R.id.img_contact);

        }
    }
}

Aquímuestromi XML de como tengo。 请帮忙

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:background="#232323"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/dialog_name_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="90dp"
            android:text="Contact Name"
            android:textColor="@android:color/white"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dialog_phone_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:text="Phone Number"
            android:textColor="@android:color/white"
            android:textSize="23sp" />

        <Button
            android:id="@+id/dialog_btn_call"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginTop="10dp"
            android:background="@android:color/white"
            android:drawableLeft="@drawable/dialog_call_black"
            android:drawablePadding="20dp"
            android:padding="20dp"
            android:text="Llamar"
            android:textAlignment="textStart"
            android:textSize="25dp" />

        <Button
            android:id="@+id/dialog_btn_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:drawableLeft="@drawable/dialog_message_black"
            android:drawablePadding="20dp"
            android:padding="20dp"
            android:text="Mensaje"
            android:textAlignment="textStart"
            android:textSize="25dp" />

    </LinearLayout>

    <ImageView
        android:id="@+id/dialog_img"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/ic_contacts"
        android:padding="10dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你需要将setOnClickListener()放入OnBindViewHolder()。到目前为止我所知道的是你有Recycler View,在项目点击你要打开一个对话框 dialog_contact ,这个对话框包含联系信息。打开此对话框后,您可以通过单击对话框中的呼叫按钮来呼叫该人。

@Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv_name.setText(mData.get(position).getName());
        holder.tv_phone.setText(mData.get(position).getPhone());
        holder.img.setImageResource(mData.get(position).getPhoto());

        holder.item_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             myDialog = new Dialog(mContext);
             myDialog.setContentView(R.layout.dialog_contact);
             myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));    

             myDialog.show();

             TextView dialog_name_tv = (TextView)myDialog.findViewById(R.id.dialog_name_id);
             Button dialog_btn_call= (Button )myDialog.findViewById(R.id.dialog_btn_call);
             TextView dialog_phone_tv = (TextView) myDialog.findViewById(R.id.dialog_phone_id);
             ImageView dialog_contact_img = (ImageView)myDialog.findViewById(R.id.dialog_img);
             dialog_name_tv.setText(mData.get(vHolder.getAdapterPosition()).getName());
             dialog_phone_tv.setText(mData.get(vHolder.getAdapterPosition()).getPhone());
             dialog_contact_img.setImageResource(mData.get(vHolder.getAdapterPosition()).getPhoto());

            dialog_btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //Do your code here
                myDialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" +mData.get(holder.getAdapterPosition()).getPhone()));
                mContext.startActivity(intent);
             }
             }); 


            }
        });


    }

修改: -