当我长按物品
时,我在RecyclerView中选择单选按钮时遇到问题目前,我已经设置了RecyclerView,有一个默认的LayoutManager,并且有一个显示所有数据的适配器。我刚刚得到onClickListener()工作(虽然我不确定它是否应该进入onCreateViewHolder()或onBindViewHolder *( - 不确定何时调用onBindViewHolder())。
我的SingelRow XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
>
<ImageView
android:id="@+id/item_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:contentDescription="icon"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item_icon"
android:layout_alignBaseline="@+id/item_icon"
android:textColor="@android:color/darker_gray"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:textSize="22dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
我的适配器具有单选模式和ViewHolder
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static RadioButton lastChecked = null;
private static int lastCheckedPos = 0;
Context context;
private ArrayList<ItemData> itemsDatas;
public MyAdapter(ArrayList<ItemData> itemsDatas) {
this.itemsDatas = itemsDatas;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
if (position == 0 && itemsDatas.get(0).isSelected() && viewHolder.radioButton.isChecked()) {
viewHolder.radioButton.setActivated(true);
viewHolder.radioButton.setSelected(true);
lastChecked = viewHolder.radioButton;
lastCheckedPos = 0;
}
viewHolder.txtViewTitle.setText(itemsDatas.get(position).getTitle1());
viewHolder.imgViewIcon.setImageResource(itemsDatas.get(position).getImageUrl());
viewHolder.radioButton.setTag(this);
viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton cb = (RadioButton) v;
if (cb.isChecked()) {
if (lastChecked != null) {
lastChecked.setChecked(false);
itemsDatas.get(lastCheckedPos).setIsSelected(false);
}
lastChecked = cb;
lastCheckedPos = position;
} else
lastChecked = null;
itemsDatas.get(position).setIsSelected(cb.isChecked());
}
});
}
@Override
public int getItemCount() {
return itemsDatas.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public RadioButton radioButton;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener(this);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
radioButton = (RadioButton) itemLayoutView.findViewById(R.id.radioButton);
}
@Override
public void onClick(View v) {
Log.d("TAG", "onClick " + getPosition() + " " + txtViewTitle.getText().toString());
}
@Override
public boolean onLongClick(View v) {
Log.d("TAG", "onLongClick " + getPosition() + " " + txtViewTitle.getText().toString());
return true;
}
}}
当我单击单选按钮时它是工作但我想要的是当我长时间点击任何项目时我想选择此项目中的单选按钮 当我点击任何地方在项目上我不想用它来做其他工作
我尝试了很多解决方案 但没有成功
我将不胜感激任何帮助。谢谢。
答案 0 :(得分:0)
如果我误解了你,那你就不知道应该在哪里设置java.lang.AbstractMethodError: Method test/BastelTest$$Lambda$7.handle(Ljavafx/event/Event;)V is abstract
at test.BastelTest$$Lambda$7/25282035.handle(Unknown Source)
at test.BastelTest.handle(BastelTest.java:38)
at test.BastelTest.testFunc(BastelTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at [usual test case stuff...]
,在这种情况下修改onLongClickListener
这样的方法:
onCreateViewHolder
答案 1 :(得分:0)
这是ViewHolder的修订版
class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public RadioButton radioButton;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
.
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
radioButton = (RadioButton) itemLayoutView.findViewById(R.id.radioButton);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//what ever you want
}
});
itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(!radioButton.isSelected()){
radioButton.setSelected(true);
}else{
radioButton.setSelected(false);
}
return false;
}
});
}