0

我的回收站视图中有 2 个微调器。现在我的第二个微调器值取决于第一个微调器选定的项目。

我能够在微调器 1 中绑定数据,但是当我尝试基于微调器 1 的 setOnItemSelectedListener 绑定微调器 2 数据时,它只会绑定屏幕上的可见项目。

我想绑定微调器 2 数据,即使它在屏幕上不可见。我有以下适配器代码:

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CardViewHolder> {

    private ArrayList<MainDetails> mMainDetails;
    private Context mContext;

    public CustomerAdapter(Context mcontext, ArrayList<MainDetails> mainDetails) {
        mContext = mcontext;
        mMainDetails = mainDetails;
    }


    private Context getContext() {
        return mContext;
    }

    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        Context context = viewGroup.getContext();
        View itemView = LayoutInflater.from(context).inflate(R.layout.row_bind_data, viewGroup, false);
        return new CardViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final CardViewHolder cardViewHolder, int position) {

        final MainDetails testMainDetails = mMainDetails.get(position);

        ArrayAdapter<Products> spinnerUserAdapter = new ArrayAdapter<Products>(getContext(), R.layout.dropdown_item, testMainDetails.getUsers());
        spinnerUserAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cardViewHolder.spinner1.setAdapter(spinnerUserAdapter);

        cardViewHolder.spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try {
                    Products pp = (Products) parent.getSelectedItem();
                    ArrayAdapter<Orders> spinnerPPAdapter = new ArrayAdapter<Orders>(getContext(), R.layout.pc_estimator_dropdown_item, pp.getOrders());
                    spinnerPPAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    cardViewHolder.spinner2.setAdapter(spinnerPPAdapter);
                } catch (Exception e) {
                    Log.e("error", e.toString());
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        cardViewHolder.spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               //other operation
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

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


    public static class CardViewHolder extends RecyclerView.ViewHolder {

        public Spinner spinner1;
        public Spinner spinner2;

        public CardViewHolder(View v) {
            super(v);
            spinner1 = (Spinner) v.findViewById(R.id.spinner1);
            spinner2 = (Spinner) v.findViewById(R.id.spinner2);

        }
    }
}

我的微调器cardViewHolder.spinner1.setOnItemSelectedListener方法只要求屏幕上的可见项目,但我想绑定所有数据,即使它不可见。

4

2 回答 2

0

直接在里面onBindViewHolder你只设置一个适配器,spinner1而没有任何东西spinner2- 它只有在onItemSelected调用之后才得到它的适配器spinner1

如果你想为你设置一些初始值,spinner2你必须在里面启动它的适配器onBindViewHolder,而不仅仅是在onItemSelected

将您的适配器启动代码移动到单独的方法

public void setUpSpinner2(Spinner spinner, Products pp) {
    try {
        ArrayAdapter<Orders> spinnerPPAdapter = new ArrayAdapter<Orders>(getContext(), R.layout.pc_estimator_dropdown_item, pp.getOrders());
        spinnerPPAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerPPAdapter);
    } catch (Exception e) {
        Log.e("error", e.toString());
    }
}

然后在里面onItemSelected调用它

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Products pp = (Products) parent.getSelectedItem();
    setUpSpinner2(cardViewHolder.spinner2, pp);
}

并且还通过检查setUpSpinner2直接在内部调用(是否在开始时选择了项目)onBindViewHoldernull

if (spinnerUserAdapter.getSelectedItem() != null) {
    Products pp = (Products) spinnerUserAdapter.getSelectedItem();
    setUpSpinner2(cardViewHolder.spinner2, pp);
}

顺便提一句。我看到您没有spinner1在任何地方存储选定的值,因此回收后两个微调器将再次为空。所以在onItemSelected获取所选项目的位置(有方法getSelectedItemPosition)并将其存储在某个数组中

//top of adapters code
private ArrayList<MainDetails> mMainDetails;
private Context mContext;
private SparseArray<Integer> selectedItemsForPositions = new SparseArray<>();

在此行onItemSelectedonNothingSelected放置/清除所选值(不要混合 and 的位置onBindViewHolderonItemSelected以不同的方式命名它们)

selectedItemsForPositions.put(onBindViewHolderPosition, parent.getSelectedItemPosition());
// or put null if nothing selected

当你有这个保持选定位置的阵列时,然后在spinner1适配器设置后恢复这个位置

cardViewHolder.spinner1.setAdapter(spinnerUserAdapter);
Integer selectedPosition = selectedItemsForPositions.get(onBindViewHolderPosition);
if(selectedPosition != null) spinnerUserAdapter.setSelection(selectedPosition);
于 2021-03-03T06:25:07.453 回答
0

我认为您需要在 ViewHolder 中实现所有内容。让我给你看一个例子:

 @Override
    public void onBindViewHolder(@NonNull SomeHolder holder, int position) {
        if (SomeDataList.size() > 0) {
            SomeData data = SomeDataList.get(position);
            holder.bindData(data);
        }
    }

然后在持有人类:

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

    private final TextView tvAddress;
    private final TextView tvAccountNumber;
    private final TextView tvName;

    private SomeData someData;

    public SomeHolder(@NonNull View itemView) {
        super(itemView);
        tvAccountNumber = itemView.findViewById(R.id.item_point_account_number);
        tvName = itemView.findViewById(R.id.item_point_subscr_name);
        tvAddress = itemView.findViewById(R.id.item_point_address);
       
        itemView.setOnClickListener(this);
    }

    public void bindData(SomeData data) {
        someData = data;
        tvAddress.setText(data.getAddress());
        //other stuff
    }

    @Override
    public void onClick(View v) {
        if (someData!=null){
           tvName.setText(someData.getName());
            //code for click event
        }
    }
}
于 2021-03-03T06:12:31.733 回答