gpt4 book ai didi

不同 Activity 中的Android RecyclerView onClick

转载 作者:行者123 更新时间:2023-11-30 00:24:03 29 4
gpt4 key购买 nike

一切正常,我的 recyclerView 中的 onClick 正在获取我的项目的位置,但我的设计要求能够单击 recyclerView 的项目并打开一个新 Activity (作为弹出窗口或弹出窗口)。我可以做到这一点,但我的问题来自于我需要在弹出窗口上显示的信息。信息在 Activity 中是这样的(在 Firebase 值调用中)

attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);

这非常适合显示信息,但与点击关联的不仅仅是“值”和“名称”。

基本上,当我选择一个项目时,我需要获得被点击项目的位置(我有)并将它与 attributeList 中的位置进行比较,这样我就可以调用 Firebase 调用(或以某种方式传递数据)到弹出窗口显示来自“属性”类的值(例如名称、值、描述和另一个列表 (recyclerView))。

我的回收 View :

    public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {

private List<attribute_list> listItems;
private Context context;

public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);

holder.txtTitle.setText(listItem.getTxtTitle());
}

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

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView txtTitle;

public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}

@Override
public void onClick(View v) {

}
}
}

最佳答案

这是例子:

public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {

private List<attribute_list> listItems;
private Context context;

public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});

}


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

public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}

OnItemClickListener onItemClickListener;

public interface OnItemClickListener{
void onItemClick(int position);
}
}

您的 Activity 。在 Oncreate() 中

 public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}

@Override
public void onItemClick(int position) {
// code here
}
}

关于不同 Activity 中的Android RecyclerView onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45725780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com