【问题标题】:How To Make a Phone Call with Button click in Recyclerview如何使用 Recyclerview 中的按钮单击拨打电话
【发布时间】:2017-02-02 05:20:44
【问题描述】:

我想使用 ACTION_CALL 方法。问题是当我单击第一个卡片视图的按钮时,应用程序会拨打电话号码11111111。 & 当我点击第二张卡片的按钮时,应用程序也会调用相同的号码1111111。 我想要的是当我单击它调用111111 的第一个卡片视图的按钮时 当我点击第二个卡片视图的按钮时,它调用22222222

项目布局xml:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@android:color/transparent"
app:contentPaddingBottom="50dp"
android:paddingBottom="50dp"
card_view:cardElevation="6dp"

>

<RelativeLayout
    android:longClickable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="7dp">


    <ImageView

        android:id="@+id/profileImage"
        android:layout_width="70dp"
        android:layout_height="50dp"
        app:civ_border_color="#7f89e9"
        android:layout_marginLeft="5dp"
        android:background="@drawable/contact1"
        android:layout_alignTop="@+id/txtCelebName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_marginTop="8dp"
        android:id="@+id/txtCelebName"
        android:textSize="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/profileImage"
        android:text="Large Text"
        android:layout_marginLeft="18dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_marginLeft="18dp"
        android:textSize="13dp"
        android:id="@+id/txtCelebMovie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtCelebName"
        android:layout_toRightOf="@+id/profileImage"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />





    <TextView
        android:layout_marginLeft="5dp"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="............"
        android:id="@+id/textView4"
        android:layout_below="@+id/profileImage" />



    <Button
        android:background="@drawable/phonegreen"

        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/buttonfordialog"
        android:layout_above="@+id/textView"
        android:layout_toRightOf="@+id/textView5"
        android:layout_toEndOf="@+id/textView5"
        android:layout_marginLeft="22dp"
        android:layout_marginStart="22dp" />


</RelativeLayout>

适配器:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {

private List<Celebrity> celebrityList;
private final View.OnClickListener btnListener;

public ItemAdapter(List<Celebrity> celebrityList, View.OnClickListener btnListener) {
    this.celebrityList = celebrityList;
    this.btnListener = btnListener;
}

@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, parent, false);

    return new ItemHolder(itemView, btnListener);
}

@Override
public void onBindViewHolder(ItemHolder holder, int position) {
    Celebrity item = celebrityList.get(position);
    holder.txtCelebName.setText(item.getName());
    holder.txtCelebMovie.setText(item.getFamousMovie());
}

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

public class ItemHolder extends RecyclerView.ViewHolder {


    private  Button buttoncalling;
    public TextView txtCelebName, txtCelebMovie;
    public ImageView profileImage;

    public ItemHolder(View view, View.OnClickListener btnListener) {
        super(view);
        txtCelebName = (TextView) view.findViewById(R.id.txtCelebName);
        txtCelebMovie = (TextView) view.findViewById(R.id.txtCelebMovie);
        profileImage = (ImageView) view.findViewById(R.id.profileImage);
        buttoncalling  = (Button) view.findViewById(R.id.buttonfordialog);
        buttoncalling.setOnClickListener(btnListener);
    }
}

}

主java:

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;

private ItemAdapter itemAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    final View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
            a_builder.setCancelable(false);
            a_builder.setMessage("do you want to call this person!!!");
            a_builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:11111111111"));



                    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    startActivity(callIntent);
                    }
                });


            a_builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });

            AlertDialog alert = a_builder.create();
            alert.setTitle("Alert !");
            alert.show();



        }
        };






    final Toolbar toolbar = (Toolbar)findViewById(R.id.MyToolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapse_toolbar);
    collapsingToolbarLayout.setTitle("Service/DPNG");


    ArrayList<Celebrity> itemList = new ArrayList<>();

    fillDummyData(itemList);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    itemAdapter = new ItemAdapter(itemList, btnListener);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(itemAdapter);
}

private void fillDummyData(ArrayList<Celebrity> celebList) {
    Celebrity celeb1 = new Celebrity();
    celeb1.setName("Johny.D");
    celeb1.setFamousMovie("Pirates  ");
    celeb1.setProfilePhotoLocation("@drawable/contact1");
    celebList.add(celeb1);

    Celebrity celeb2 = new Celebrity();
    celeb2.setName("Arnold");
    celeb2.setFamousMovie("The Terminator");
    celeb2.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTI3MDc4NzUyMV5BMl5BanBnXkFtZTcwMTQyMTc5MQ@@._V1._SY209_CR13,0,140,209_.jpg");
    celebList.add(celeb2);

【问题讨论】:

    标签: java jquery android-studio android-recyclerview


    【解决方案1】:

    这已经很老了,但我希望有人会发现这个答案很有用:

    • 首先,您在按钮侦听器中对要呼叫的号码进行硬编码。因此,无论您点击什么按钮,您都是在要求它拨打 1111111。
    • 您需要在 Celebrity 类中为电话号码添加一个字段。然后在您的适配器中,创建一个将在您的 MainActivity 类中实现的接口。因此,在点击一个项目(在本例中是一个卡片视图)时,您检索名人的电话号码,并将其提供给您实现的界面,您可以在该界面中呼叫名人。
    • 这样做,您将检索每个名人的正确电话号码,而不是使用您当前拥有的硬编码字符串。

    适配器:

    public interface CardClickListener {
       onCardClicked(Celebrity celebrity);
    }
    

    主活动:

    public class MainActivity extends AppCompatActivity implements ItemAdapter.CardClickListener {
    ......
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    ........
    
    // add the following to your onCreate method 
    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Celebrity celebrity = itemList.get(position);
                onCardClicked(celebrity);
            }
    
            @Override
            public void onLongClick(View view, int position) {
    
            }
        }));
    .........
    }
    
    // implement your interface here
    @Override
    public void onCardClicked(Celebrity celebrity) {
       // don't forget to check for permissions
       Intent callIntent = new Intent(Intent.ACTION_CALL);
       callIntent.setData(Uri.parse("tel:" + celebrity.phone));
    }
    
    ......
    
    // add phone numbers to your class definitions
    private void fillDummyData(ArrayList<Celebrity> celebList) {
       Celebrity celeb1 = new Celebrity();
       celeb1.setName("Johny.D");
       celeb1.setFamousMovie("Pirates  ");
       celeb1.setPhone("111111");
       celeb1.setProfilePhotoLocation("@drawable/contact1");
       celebList.add(celeb1);
    
       Celebrity celeb2 = new Celebrity();
       celeb2.setName("Arnold");
       celeb2.setFamousMovie("The Terminator");
       celeb2.setPhone("222222");
       celeb2.setProfilePhotoLocation("http://ia.mediaimdb.com/images/M/209_.jpg");
       celebList.add(celeb2);
    }
    } //mainactivity
    

    RecyclerTouchListener:

    public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
    private GestureDetector gestureDetector;
    private ClickListener clickListener;
    
    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
    
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }
    
            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }
    
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildAdapterPosition(child));
        }
        return false;
    }
    
    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }
    
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
    

    }

    点击监听器:

    public interface ClickListener {
      void onClick(View view, int position);
    
      void onLongClick(View view, int position);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2011-07-21
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多