我正在尝试在我的recyclerView中使用Spinner(带有下拉菜单的3个点),但是当我加载此recyclerView函数时,会自动调用 onItemSelected 函数,以后再按该项目时,它将不再调用。这是我在适配器中的代码:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.my_listing_item_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((SaleAdapter.SalesViewHolder) holder).overflow.setAdapter(adapter);
((SaleAdapter.SalesViewHolder) holder).overflow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(position) + " item is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public Spinner overflow;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
overflow = (Spinner) itemView.findViewById(R.id.list_item_overflow);
}
}
这是View的代码:
<LinearLayout
android:id="@+id/list_item_overflow_layout"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:padding="15dp"
android:background="@drawable/ic_more_vert_24dp_grey"
>
<Spinner
android:id="@+id/list_item_overflow"
android:layout_width="24dp"
android:layout_height="24dp"
/>
</LinearLayout>
有人知道我的代码在这里出什么问题吗?我应该在其他地方导出 setOnItemSelectedListener 吗?
答案 0 :(得分:1)
对于列表中的每个项目,最好使用PopUpMenu。使用ImageView
代替Spinner
,它将显示“ 3点”图标。当有人单击圆点时,PopUpMenu将...弹出!
步骤:
1)用Spinner
替换xml布局中的ImageView
,这将显示三个点图标。
2)在RecyclerView.Adapter<SalesViewHolder>
内定义一个侦听器类,并在其中保存一个实例。
3)在SalesViewHolder
内设置一个OnClickListener
来响应点击。
4)从活动中弹出。
您的新适配器应如下所示
class SalesAdapter extends RecyclerView.Adapter<SalesViewHolder>{
public interface OnMenuItemClickListener{
void onMenuItemClicked(ImageView view);
}
private OnMenuItemClickListener listener;
public void setOnMenuItemClickListener(OnMenuItemClickListener listener){
this.listener = listener;
}
public static class SalesViewHolder extends RecyclerView.ViewHolder {
public LinearLayout overflowLayout;
public ImageView menu;
public SalesViewHolder(View itemView, Context context) {
super(itemView);
overflowLayout = (LinearLayout) itemView.findViewById(R.id.list_item_overflow_layout);
menu= (ImageView) itemView.findViewById(R.id.imageview_id);
// Make sure to enable lambdas
menu.setOnClickListener( (view) -> {
if( listener != null ){
listener.onMenuItemClicked(view);
}
}
}
}
}
您的活动必须实现OnMenuItemClickListener,并且可能看起来像这样
class YourActivity extends Activity implements SalesAdapter.OnMenuItemClickListener{
// Don't forget to set the listener to the adapter, let's suppose that happens in onCreate()
@Override
public void onCreate(Bundle arg){
SalesAdapter adapter = new SalesAdapter();
adapter.setOnMenuItemClickListener(this);
}
@Override
public void onMenuItemClicked(ImageView menu){
showPopUp(menu);
}
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
}
别忘了提供菜单操作等...,如here所述。