赞
踩
在上一篇的文章中,主要对适配器与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()); // } // } // } // ); } }}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。