如何从具有recycleview的片段中使用startactivity(intent)?

时间:2018-08-04 10:04:45

标签: android recycler-adapter

我在包含联系人列表的片段中有recyckeview,我想将onclick侦听器设置为使用recycleview电话号码并使用ACTION_DIAL将其发送到拨号程序屏幕的recycleview。

myfragment:

public class HomeFragment extends Fragment {
        ArrayList<ExampleItem> exampleList = new ArrayList<>();
    Context context;
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_home,container,false);
            context=getContext();
            RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycleview_home);
            RecycleViewadapter recycleViewadapter =new RecycleViewadapter(getActivity(),exampleList);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(recycleViewadapter);
            setuprecycleview();

            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

            return view;
        }
        private void setuprecycleview() {
       exampleList.add(new ExampleItem("sagar","student","************","*******.com"));
          exampleList.add(new ExampleItem("sagar","student","************","*******.com"));
exampleList.add(new ExampleItem("sagar","student","************","*******.com"));    
        }

    }

myrecyclevieeadapter:

public class RecycleViewadapter extends RecyclerView.Adapter<RecycleViewadapter.ViewHolder>  {
        private ArrayList<ExampleItem> mexampleList = new ArrayList<>();
    Context mcontext;
        RecyclerView recyclerView;
        public RecycleViewadapter(Context mcontext, ArrayList<ExampleItem> mexampleList) {
            this.mcontext = mcontext;

            this.mexampleList = mexampleList;

        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_cardview,viewGroup,false);
            ViewHolder viewHolder=new ViewHolder(view);
            return viewHolder;
        }
        public RecycleViewadapter(ArrayList<ExampleItem> exampleList){
            mexampleList= exampleList;
        }
        @Override
        public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
            final ExampleItem currentItem = mexampleList.get(i);
    int lastPosition=-1;
    viewHolder.name.setText(currentItem.getName());
            viewHolder.phone.setText(currentItem.getPhone());
            viewHolder.email.setText(currentItem.getEmail());
            viewHolder.profession.setText(currentItem.getProfession());
           viewHolder.phone.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   String phoneno =viewHolder.phone.getText().toString();
                   Intent intent = new Intent(Intent.ACTION_DIAL);
                   intent.setData(Uri.parse(phoneno));
                  // mcontext.startActivity(intent);

               }
           });
           if (i>lastPosition){
               Animation animation= AnimationUtils.loadAnimation(mcontext,R.anim.contacts_anim);
               viewHolder.itemView.startAnimation(animation);
               lastPosition=i;
           }
        }
        @Override
        public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
            super.onViewDetachedFromWindow(holder);
            holder.itemView.clearAnimation();
        }
        @Override
        public int getItemCount() {
            return mexampleList.size();
        }
        public class ViewHolder extends RecyclerView.ViewHolder{
            TextView name;
            CircleImageView image;
            TextView phone;
            TextView profession;
            TextView email;

            ImageView whatsapp;
    CardView cardView;
            public ViewHolder(View itemView) {
                super(itemView);
                whatsapp =itemView.findViewById(R.id.image_phone);
                profession=itemView.findViewById(R.id.profession);
                email =itemView.findViewById(R.id.email);
                image = itemView.findViewById(R.id.image);
                name = itemView.findViewById(R.id.name);
                phone=itemView.findViewById(R.id.phone_no);
                recyclerView= itemView.findViewById(R.id.recycleview_home);
                cardView=itemView.findViewById(R.id.cardview);
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String phoneno =phone.getText().toString();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(phoneno));
            //mcontext.startActivity(intent);
        }
    });
     }}}

我尝试对ACTION_DIAL使用意图,但是当我单击recycleview卡视图时,应用程序崩溃。如何从片段recycleview使用ACTION_DIAL。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

更改为此:

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneno));
mcontext.startActivity(intent);

您的清单中必须包括:

<uses-permission android:name="android.permission.CALL_PHONE" />