我想使用RecyclerView为配置文件选择菜单。我用RecyclerView创建了它。现在我在onItemClickListener中遇到问题。我要更改CardView的背景和选择的文本颜色。一次只能选择一项。然后在下一个按钮上单击,它应根据选择重定向到活动。
这是我的屏幕,看起来像:
public class SelectProfile extends AppCompatActivity {
private String[] mTextData;
private int[] mImgData;
RecyclerView recyclerView;
private ProfileAdapter profileAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_profile);
recyclerView = findViewById(R.id.recycleProfile);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
profileAdapter = new ProfileAdapter();
recyclerView.setAdapter(profileAdapter);
int imgData[] = {R.drawable.ic_college_icon,R.drawable.ic_parent,R.drawable.ic_student,R.drawable.ic_teaching,
R.drawable.ic_non_teaching,R.drawable.ic_other};
final String textData[] = {"School/College","Parent","Student","Teaching Staff","Non-Teaching Staff","Other"};
profileAdapter.setData(imgData,textData);
}
private class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.ProfileAdapterViewHolder>{
int index = -1;
@NonNull
@Override
public ProfileAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.profile_item;
LayoutInflater layoutInflater = LayoutInflater.from(context);
boolean shouldAttachToParent = false;
View view = layoutInflater.inflate(layoutIdForListItem,parent,shouldAttachToParent);
return new ProfileAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ProfileAdapterViewHolder holder,final int position) {
int mImage = mImgData[position];
String mText = mTextData[position];
holder.img.setImageResource(mImage);
holder.txt.setText(mText);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
index = position;
notifyDataSetChanged();
}
});
if (index == position){
holder.card.setBackground(getResources().getDrawable(R.drawable.bg_select_profile));
holder.txt.setTextColor(getResources().getColor(R.color.colorPrimary));
}else {
holder.card.setCardBackgroundColor(getResources().getColor(android.R.color.white));
holder.txt.setTextColor(getResources().getColor(R.color.gray));
}
}
@Override
public int getItemCount() {
if (null == mImgData) return 0;
return mImgData.length;
}
public class ProfileAdapterViewHolder extends RecyclerView.ViewHolder {
private final AppCompatImageView img;
private final TextView txt;
private final CardView card;
private ProfileAdapterViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
txt = itemView.findViewById(R.id.txt);
card = itemView.findViewById(R.id.card);
}
}
private void setData(int[] imgData,String[] txtData){
mImgData = imgData;
mTextData = txtData;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
您使用Index变量的方法是正确的,为索引添加getter和setter方法。但是您不能在适配器上设置onClickListener。取而代之的是,在卡片视图上设置监听器,如下所示->
holder.card.setOnItemClickListener(new ClickListener() {
@Override
public void onItemClick(int position, View v) {
index = position;
notifyDataSetChanged();
}
});
将此方法添加到适配器类中->
public int getSelectedIndex(){
return this.index; }
此后,在您的“活动”中,在您的 NEXT 按钮的onClickListener内执行以下操作
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(profileAdapter.getSelectedIndex() == 1)
//goto activity of your desire and so on
}
});
答案 1 :(得分:0)
将卡片视图用作回收者视图项目的父级布局,并在单击侦听器上将其应用于回收者视图适配器中的卡片视图。
holder.your_card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get you recyclerview item position here
}
});
答案 2 :(得分:0)
在所有者根视图上添加OnClickListner,编写监听器的最佳位置是 onCreateViewHolder ,因此请使用以下
替换onCreateViewHolderpublic ProfileAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.profile_item;
LayoutInflater layoutInflater = LayoutInflater.from(context);
boolean shouldAttachToParent = false;
View view = layoutInflater.inflate(layoutIdForListItem,parent,shouldAttachToParent);
ViewHolder viewHolder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postion=viewHolder.getAdapterPosition();
notifyDataSetChanged();
}
});
return viewHolder ;
}
答案 3 :(得分:0)
如下所示更改和调整适配器:
private class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.ProfileAdapterViewHolder>{
int index = -1;
@NonNull
@Override
public ProfileAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.profile_item;
LayoutInflater layoutInflater = LayoutInflater.from(context);
boolean shouldAttachToParent = false;
View view = layoutInflater.inflate(layoutIdForListItem,parent,shouldAttachToParent);
return new ProfileAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProfileAdapterViewHolder holder, int position) {
int mImage = mImgData[position];
String mText = mTextData[position];
holder.img.setImageResource(mImage);
holder.txt.setText(mText);
if (index == position){
holder.card.setCardBackgroundColor(getResources().getColor(R.color.colorPrimary));
holder.txt.setTextColor(getResources().getColor(R.color.colorPrimary));
}else {
holder.card.setCardBackgroundColor(getResources().getColor(android.R.color.white));
holder.txt.setTextColor(getResources().getColor(R.color.gray));
}
}
@Override
public int getItemCount() {
if (null == mImgData) return 0;
return mImgData.length;
}
public class ProfileAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final AppCompatImageView img;
private final TextView txt;
private final CardView card;
private ProfileAdapterViewHolder(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
txt = itemView.findViewById(R.id.txt);
card = itemView.findViewById(R.id.card);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
setIndex(getAdapterPosition());
notifyDataSetChanged();
}
}
private void setData(int[] imgData,String[] txtData){
mImgData = imgData;
mTextData = txtData;
notifyDataSetChanged();
}
public void setOnItemClickListener(ClickListener clickListener){
mClickListener = clickListener;
}
private void setIndex(int index){
this.index=index;
}
}
您可能不需要ClickListener。
答案 4 :(得分:0)
您可以在通过片段OR活动实现的构造函数中传递一个侦听器对象
/**
* item click interface of adapter
*/
public interface ProfileAdapterListener {
void onItemClick(int position, ProfileAdapterViewHolder holder)
}
此接口通过Fragment OR Activity实现
/**
* On item clicked Implement Method from adapter listener.
*
* @param position
*/
@Override
public void onItemClick(int position, ProfileAdapterViewHolder holder) {
// Here you can call that method
}
然后,您在适配器的构造函数中传递此侦听器。
private void buildRecyclerView() {
profileAdapter = new ProfileAdapter(this);
recyclerView.setAdapter(profileAdapter);
}
在构造函数中,您可以这样分配
private ProfileAdapterListener mProfileAdapterListener;
public OfferAdapter(ProfileAdapterListener mProfileAdapterListener) {
this.mProfileAdapterListener = mProfileAdapterListener
}
}
现在,您可以通过在任何这样的Viwe上设置点击监听器来使用此监听器
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProfileAdapterListener.onItemClick(position, holder);
}
});
它返回调用实现该方法的onItemClick
方法。这是一种安全可靠的方法,可单击每个项目或项目中的任何视图。