如何单独添加单击到RecyclerView整行和特定行项?

时间:2016-12-29 09:37:37

标签: android android-recyclerview

我有一个RecyclerView,其中包含两个TextView和一个ImageView。我想单独添加单击每行以及每行中的ImageView。有可能吗?

这是我的Adapter类:

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

private List<ContactsDataModel> dataList;
private Typeface tfSegoeui;
Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, detail;
    public ImageView catIcon, imgCall;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        detail = (TextView) view.findViewById(R.id.detail);
        catIcon = (ImageView) view.findViewById(R.id.catIcon);
        imgCall = (ImageView) view.findViewById(R.id.imgCall);
    }
}


public ContactsListAdapter(Context context, List<ContactsDataModel> dataList) {
    this.dataList = dataList;
    this.context = context;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    ContactsDataModel dataModel = dataList.get(position);
    tfSegoeui = Typeface.createFromAsset(holder.title.getContext().getResources().getAssets(), "fonts/segoeui.ttf");
    holder.title.setText(dataModel.getName());
    holder.detail.setText(dataModel.getPersonalNumber());
    holder.imgCall.setTag(dataModel.getPersonalNumber());
    holder.catIcon.setImageResource(dataModel.getImage());
    holder.title.setTypeface(tfSegoeui);
    holder.detail.setTypeface(tfSegoeui);

           holder.imgCall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String number = holder.imgCall.getTag().toString();
            Log.d("number", number);
            String num = "tel:" + number;

            try {
                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(num));
                context.startActivity(dialIntent);
            }catch(Exception e) {
                Toast.makeText(context,"Your call has failed...",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }


            EndCallListener callListener = new EndCallListener();
            TelephonyManager mTM = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    });

}

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

private class EndCallListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String LOG_TAG = "Call contact ";
        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
            Log.i(LOG_TAG, "OFFHOOK");
        }
        if(TelephonyManager.CALL_STATE_IDLE == state) {
            //when this state occurs, and your flag is set, restart your app
            Log.i(LOG_TAG, "IDLE");
        }
    }
}

}

4 个答案:

答案 0 :(得分:2)

将单行布局文件设置为

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, detail;
public ImageView catIcon, imgCall;
//add item_row layout e.g
public RelativeLayout rel;


public MyViewHolder(View view) {
    super(view);
    title = (TextView) view.findViewById(R.id.title);
    detail = (TextView) view.findViewById(R.id.detail);
    catIcon = (ImageView) view.findViewById(R.id.catIcon);
    imgCall = (ImageView) view.findViewById(R.id.imgCall);
    rel= (RelativeLayout) view.findViewById(your_id_here);


rel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       //whatever you want here
    }
});
}
}

答案 1 :(得分:0)

你可以在ViewHolder class

中添加点击监听器到任何类似的视图
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, detail;
public ImageView catIcon, imgCall;

public MyViewHolder(View view) {
    super(view);
    title = (TextView) view.findViewById(R.id.title);
    detail = (TextView) view.findViewById(R.id.detail);
    catIcon = (ImageView) view.findViewById(R.id.catIcon);
    imgCall = (ImageView) view.findViewById(R.id.imgCall);
    imgCall.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
    title.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
    catIcon.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
     detail.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
}

}

如果您想要点击该项目的位置。

onBindViewHolder

中添加此行
title.setTag(position);

使用title.getTag()

获取排名

答案 2 :(得分:0)

请参阅以下代码,了解项目点击事件。

<强> ContactsListAdapter

private List<ContactsDataModel> dataList;
    private Typeface tfSegoeui;
    Context context;

    public interface OnContactClick {
        public void onContactClick(int position);
    }

    OnContactClick onContactClick;

    public void setOnContactClick(OnContactClick onContactClick) {
        this.onContactClick = onContactClick;
    }

onBindViewHolder

中添加此内容
@Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        ContactsDataModel dataModel = dataList.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onContactClick != null) {
                    onContactClick.onContactClick(position);
                }
            }
        });

活动

中添加以下行
ContactsListAdapter contactsListAdapter = new ContactsListAdapter(this, dataList);
        contactsListAdapter.setOnContactClick(new ContactsListAdapter.OnContactClick() {
            @Override
            public void onContactClick(int position) {
                //You will receive event when clicking item in list
            }
        });

答案 3 :(得分:-1)

保存

View itemView  

在持有者中并像使用imgCall

一样进行setonclick

你也在imgCall

上做了两个onclick听众