我在RecyclerView
使用CardView
,CardView
内有2个按钮。现在,已经通过onClick
静态类实现View.OnClickListener
并覆盖事件(其工作)来实现ViewHolder
事件,如
@Override
public void onClick(View v) {
if (v.getId() == mClassBtn.getId()){
}
else if (v.getId() == mDaysBtn.getId()){
}
}
寻找解决方案/模式来处理Click
类中的Activity
,例如
adapter = new ItemsListAdapter(getActivity(), data, new CustomItemClickListener() {
@override
public void onItemClick(View v, int position) {
Log.d(TAG, "clicked position:" + position);
// which button clicked and separate action for each button
}
});
// CustomItemClickListener -- interface in the adapter class which holds the click event .
上述代码工作正常并且能够记录Clicks
但无法区分哪个按钮已被点击。
感谢任何帮助。提前致谢。
答案 0 :(得分:10)
这是实现这一目标的最佳方式: -
这就是我实施的方式
public class FollowersDetailsAdapter extends RecyclerView.Adapter<FollowersDetailsAdapter.MyViewHolder> {
private List<GetUserFollowDetailsResponseData> followersList;
Context context;
public FollowersDetailsAdapterListener onClickListener;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView rv_tv_follower_name, rv_tv_follower_desig;
public CircleImageView rv_iv_follower_image;
RelativeLayout rv_rel_layout_followers, rel_layout_followers;
LikeButton rv_iv_follower_icon;
public MyViewHolder(View view) {
super(view);
rv_rel_layout_followers = (RelativeLayout) view.findViewById(R.id.rv_rel_layout_followers);
rel_layout_followers = (RelativeLayout) view.findViewById(R.id.rel_layout_followers);
rv_iv_follower_image = (CircleImageView) view.findViewById(R.id.rv_iv_follower_image);
rv_tv_follower_name = (TextView) view.findViewById(R.id.rv_tv_follower_name);
rv_tv_follower_desig = (TextView) view.findViewById(R.id.rv_tv_follower_desig);
rv_iv_follower_icon = (LikeButton) view.findViewById(R.id.rv_iv_follower_icon);
rel_layout_followers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickListener.iconTextViewOnClick(v, getAdapterPosition());
}
});
}
}
public FollowersDetailsAdapter(List<GetUserFollowDetailsResponseData> FollowersList, FollowersDetailsAdapterListener listener) {
this.followersList = FollowersList;
this.onClickListener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.followers_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
if (followersList.get(position).getImage() != null && !followersList.get(position).getImage().trim().equalsIgnoreCase("")) {
Picasso.with(context)
.load(followersList.get(position).getImage())
.resize(200, 200).into(holder.rv_iv_follower_image);
}
holder.rv_tv_follower_name.setText(followersList.get(position).getName());
holder.rv_tv_follower_desig.setText(followersList.get(position).getDescription());
if (followersList.get(holder.getAdapterPosition()).getID() == Integer.parseInt(ApplicationPreference.getUserId())) {
holder.rv_iv_follower_icon.setVisibility(View.GONE);
} else {
holder.rv_iv_follower_icon.setVisibility(View.VISIBLE);
if (followersList.get(position).getIsFollowing() == 0) {
holder.rv_iv_follower_icon.setLiked(false);
} else if (followersList.get(position).getIsFollowing() == 1) {
holder.rv_iv_follower_icon.setLiked(true);
}
}
holder.rv_iv_follower_icon.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
onClickListener.iconImageViewOnClick(likeButton, position);
}
@Override
public void unLiked(LikeButton likeButton) {
onClickListener.iconImageUnFollowOnClick(likeButton, position);
}
});
}
public interface FollowersDetailsAdapterListener {
void iconTextViewOnClick(View v, int position);
void iconImageViewOnClick(View v, int position);
void iconImageUnFollowOnClick(View v, int position);
}
@Override
public int getItemCount() {
return followersList.size();
}
}
并在您初始化适配器的活动中 -
followersDetailsAdapter = new FollowersDetailsAdapter(followersList, new FollowersDetailsAdapter.FollowersDetailsAdapterListener() {
@Override
public void iconTextViewOnClick(View v, int position) {
gotoUserProfile(followersList.get(position).getID());
}
@Override
public void iconImageViewOnClick(View v, int position) {
followUserProfile(followersList.get(position).getID(),1);
}
@Override
public void iconImageUnFollowOnClick(View v, int position){
followUserProfile(followersList.get(position).getID(),0);
}
});
答案 1 :(得分:7)
请尝试这个原则,通过界面创建活动,适配器之间的通信,希望这个原则可以帮助你:
接口:
public interface AdapterCallback {
void onClickCallback(YourModel itemModel);
}
适配器:
public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {
private List<String> mList;
private AdapterCallback callback;
private Context context;
public YourAdapter(List<String> list, AdapterCallback adapterCallback) {
this.mList = list;
adapter = adapterCallback
}
@Override
public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View item = inflater.inflate(R.layout.item_recycler, parent, false);
return new ViewHolder(item);
}
@Override
public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
YourModel itemModel = list.get(position);
String lastName = itemModel.getLastName(); // for example;
String firstName = itemModel.getFirstName(); // for example;
holder.tvFirstName.setText(firstName);
holder.tvLastName.setText(lastName);
holder.tvFirstName.setOnClickListener(
// you can override this onClick into your Activity.
adapterCallback.onClickCallback(itemModel));
}
}
@Override
public int getItemCount() {
return mList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvFirstName;
private TextView tvLastName;
ViewHolder(View itemView) {
super(itemView);
tvFirstName = (TextView) itemView.findViewById(R.id.tv_first);
tvLastName = (TextView) itemView.findViewById(R.id.tv_last);
}
}
的活动:
public class MyActivity extends AppCompatActivity implements AdapterCallback {
private YourAdapter adapter;
private List<String> items;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. init your list && fill it with needed data
// 2. init your adapter
// 3. put your list as parameter into constructor + interface
items = new ArrayList<String>() // empty
adapter = new YourAdapter(items, this)
}
@Override
public void onClickCallback(YourModel itemModel) {
Toast.makeText(this, itemModel.getFirstName(), Toast.LENGTH_SHORT).show();
}
我认为这些信息足以理解我的意思。 同样的原则,您可以在界面中创建多个OnClick方法,或者为一些按钮添加一个onClick,并检查id或其他内容以获取所需的信息。 祝你好运
答案 2 :(得分:0)
试试这个
在你的活动中你喜欢这个
CustomItemClickListener listner;
listner=YourActivityName.this // in OnCreate Method
并在您的适配器中传递此列表器
adapter = new ItemsListAdapter(getActivity(), data, listner);
在你的适配器中使用它就像这样
CustomItemClickListener listner;
this.listner=listner; // In your Adapter COnstructor
最后你的onBindViewHolder就是这样的
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listner.onItemClick(holder.getLayoutPosition()); //getting position
}
});
holder.button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listner.onItemClick(holder.getLayoutPosition()); //getting position
}
});
答案 3 :(得分:0)
我是这样做的。
在ViewHolder中的Button上设置OnClickListener
使用按钮位置作为参数
传递点击条目的位置并执行操作
ViewHolder
select s.date,si.name, coalesce(gics_sector,si.security_type), @weight := s.market_value / fundval.val, p.name, si.currency, t1.div_yield
from matilda.security_info si
left join(select * from matilda.synth_exp where date = WORKDAY_ADD(current_date(),-1)) as s on si.ticker = s.ticker
left join (select fund_id, sum(market_value), sum(synth_value) as val, date from matilda.synth_exp where date = WORKDAY_ADD(current_date(),-1) group by fund_id,date) as fundval on fundval.fund_id = s.fund_id
left join (select * from matilda.stocks where date = LAST_DAY(DATE_ADD(current_date(), INTERVAL -1 MONTH))) as gics on gics.id = si.id
left join (select * from matilda.dailies where date = WORKDAY_ADD(current_date(),-1)) as t1 on t1.id = si.id
left join matilda.portfolio p on p.fund_id = s.fund_id
where s.market_value is not null;
活动
yourButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int position = getAdapterPosition(); //Clicked entry in your List
//Method from your activity
yourActivity.methodOnBtnClick(position);
}
答案 4 :(得分:0)
这是我的解决方案,如果它可以帮助某人。
您可以单击Recyclerview的一个项目或两个按钮中的一个。
界面实现:
onStudentItemClick,用于单击recyclerview项目
onChatItemClick和onAbsenceItemClick,单击按钮
使用
holder.imgChat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onChatItemClick(mStudents.get(index));
}
});
我为一个按钮设置了点击监听器
在StudentViewHolder类中,我实现了OnClickListener并覆盖onClik
@Override
public void onClick(View v) {
if (mOnClickListener != null){
int clickedPosition = getAdapterPosition();
mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
}
}
适配器:
public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.StudentsViewHolder> {
private ListItemClickListener mOnClickListener;
private ArrayList<Student> mStudents;
public interface ListItemClickListener {
void onStudentItemClick(Student student);
void onChatItemClick(Student student);
void onAbsenceItemClick(Student student);
}
public StudentsAdapter(ArrayList<Student> students, ListItemClickListener listener) {
mStudents = students;
mOnClickListener = listener;
}
@Override
public StudentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.student_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
StudentsViewHolder viewHolder = new StudentsViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(StudentsViewHolder holder, int position) {
final Integer index = position;
holder.tvStudent.setText(mStudents.get(position).nameForList());
holder.imgChat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onChatItemClick(mStudents.get(index));
}
});
holder.imgAbsence.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onAbsenceItemClick(mStudents.get(index));
}
});
}
public Student getItem(Integer position){
return mStudents.get(position);
}
@Override
public int getItemCount() {
return mStudents.size();
}
class StudentsViewHolder extends RecyclerView.ViewHolder
implements OnClickListener {
ImageView imgStudent;
TextView tvStudent;
ImageButton imgChat, imgAbsence;
public StudentsViewHolder(View itemView) {
super(itemView);
imgStudent = (ImageView) itemView.findViewById(R.id.img_student);
tvStudent = (TextView) itemView.findViewById(R.id.tv_student);
imgChat = (ImageButton) itemView.findViewById(R.id.btn_chat_student);
imgAbsence = (ImageButton) itemView.findViewById(R.id.btn_absence);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnClickListener != null){
int clickedPosition = getAdapterPosition();
mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
}
}
}
}
将工具分割为界面:
public class SubjectDetailFragment extends Fragment implements StudentsAdapter.ListItemClickListener {
...
@Override
public void onStudentItemClick(Student student) {
StudentDetailFragment detailFragment = StudentDetailFragment.newInstance();
Bundle bundle = new Bundle();
bundle.putParcelable("student", student);
detailFragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, detailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onChatItemClick(Student student) {
...
}
@Override
public void onAbsenceItemClick(Student student) {
...
}