RecyclerView的点击事件运用(android仿电话app)

在上一篇的文章中,主要对适配器与MainActivity进行了修改。

一,在适配器内部进行点击事件的运用

在这里插入图片描述

二,通过接口的方式,让外部调用(推荐)

在这里插入图片描述

在这里插入图片描述

在MainActivity中进行调用。

在这里插入图片描述
完整的ManinActivity文件

package com.example.mydailer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;


import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mydailer.po.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mrecyclerView;
    private Adapter adapter;
    List<User> userList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mrecyclerView = findViewById(R.id.recyclerView_txl);
        mrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


        //这个地方就是请求权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
        } else {
            readContacts();
        }
        //readContacts();

        tocall();
    }


    //读取联系人
    private void readContacts() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, null, null, null);
            while (cursor.moveToNext()) {
                int i_name = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                String displayName = cursor.getString(i_name);
                int i_number = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(i_number);
                userList.add(new User(displayName, number));

            }
            Adapter adapter = new Adapter(MainActivity.this, userList);
            mrecyclerView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    ///跳转拨打
    private void tocall() {

        mrecyclerView.setItemAnimator(new DefaultItemAnimator());
        Log.d("Tag","666666");

        Adapter adapter = new Adapter(MainActivity.this, userList);
        mrecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
            @Override
            public void onItemClick( View view,int position) {
                String  atelphone =userList.get(position).getTelPhone();
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + atelphone));
                startActivity(intent);
                Toast.makeText(MainActivity.this, "clicked " + position, Toast.LENGTH_SHORT).show();
                Log.d("Tag", "onItemClick" + position);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Log.d("Tag", "onItemClick" + position);
            }
        });


    }
}


适配器

package com.example.mydailer;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mydailer.po.User;

import java.util.ArrayList;
import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    private Context mContext;
    private static final String TAG="Adapter";
    private String[] names;
    private int[] icons;
    private String[] introduces;
    private List<User> userList;
    public String Atelphone;



    public Adapter() {
    }

    public Adapter(Context context,List<User> userList) {
        mContext = context;
        this.userList = userList;
    }


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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                User user = userList.get(position);
                 Atelphone=user.getTelPhone();

                Toast.makeText(view.getContext(), "dianjishijian" + user.getTelPhone(), Toast.LENGTH_SHORT).show();
            }
        });
        //ViewHolder holder=new  ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false));
        //return holder;
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.name.setText(userList.get(position).getName());
       // names= user.get(position).getName();
        holder.introduce.setText(userList.get(position).getTelPhone());

        // item click
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(holder.itemView, holder.getAdapterPosition());
                }
            });
        }
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mOnItemClickListener.onItemLongClick(holder.itemView, holder.getAdapterPosition());
                return true;
            }
        });

// item long click

    }




    @Override
    public int getItemCount() {

        return userList == null ? 0 : userList.size();
    }


    private OnItemClickListener mOnItemClickListener;//声明接口

    public  void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        mOnItemClickListener=onItemClickListener;
    };
    public interface OnItemClickListener {
        void onItemClick( View view,int position);
        void onItemLongClick(View view, int position);
    }

    private  LayoutInflater mlayoutInflater;
    public class ViewHolder extends RecyclerView.ViewHolder {

        View fruitView;
        TextView name;
        ImageView iv;
        TextView introduce;
        RelativeLayout relativeLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            fruitView =itemView;
            name = (TextView) itemView.findViewById(R.id.name);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            introduce = (TextView) itemView.findViewById(R.id.introduce);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relative_dhbd);
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (mOnItemClickListener != null) {
//                        mOnItemClickListener.onItemClick(view, getAdapterPosition());
//                    }
//                }
//            });
//            relativeLayout.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    if (mOnItemClickListener != null) {
//                        mOnItemClickListener.onItemClick(view, getAdapterPosition());
//                    }
//                }
//            }
//            );


        }
    }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那天的烟花雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值