简体   繁体   English

检查回收站视图中的项目是否已选中

[英]Check if the item in recycler view selected

I would like to check the property of one of the items in RecyclerView . 我想检查RecyclerView中一项的属性。 To be more specific I want to check if this item is selected. 更具体地说,我想检查是否选择了此项。

Firstly I select the item 首先,我选择项目

onView(withId(R.id.list_master))
            .perform(
                    RecyclerViewActions.actionOnItemAtPosition(14, clickAnItem(R.id.layout_menu))
            );

Secondly check if the specific item is selected: 其次检查是否选择了特定项目:

        onView(allOf(withId(R.id.layout_menu), hasDescendant(withText("SO"))))
            .check(matches(isSelected()));

With custom matcher: 使用自定义匹配器:

    private static Matcher<View> isSelected() {
    return new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View item) {

            return item.isSelected();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Selected property");
        }
    };

Sadly the state of this view is not selected, but UI shows it successfully marked as selected item. 遗憾的是,该视图的状态未被选中,但是UI将该视图成功标记为选中项。 Implementation details 实施细节

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final ViewHolder viewHolder = (ViewHolder) holder;
    viewHolder.itemView.setSelected(position == selected);

Check this code I had the same thing want you want check whether the field is selected or not in Recycler View. 检查此代码我同样想让您检查是否在Recycler View中选择了该字段。

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

private Context context;
private Fragment fragment;
private ArrayList<ObjGetLookupDataResponseIn> lookupData;


public AutoWithRuleListListAdapter(Context context, Fragment fragment, ArrayList<ObjGetLookupDataResponseIn> lookupData) {
    this.context = context;
    this.fragment = fragment;
    this.lookupData = lookupData;

}

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

@Override
public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    String output = lookupData.get(position).getDisplayName().substring(0, 1).toUpperCase() + lookupData.get(position).getDisplayName().substring(1);
    holder.radioBen.setText(output);
    holder.radioBen.setOnCheckedChangeListener(null);
    holder.radioBen.setChecked(lookupData.get(position).isSelected());

    holder.radioBen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            resetAll();
            lookupData.get(position).setSelected(true);
            notifyDataSetChanged();
            ((AutoViewRuleFragment)fragment).ruleName(position);
        }
    });


}

private void resetAll() {
    for (int i=0;i<lookupData.size();i++)
    {
        lookupData.get(i).setSelected(false);
    }
    notifyDataSetChanged();
}


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

public class ViewHolder extends RecyclerView.ViewHolder {
    RadioButton radioBen;

    public ViewHolder(View itemView) {
        super(itemView);

        radioBen = (RadioButton) itemView.findViewById(R.id.radioReceivesFunds);
        radioBen.setTypeface(Utils.gothamMedium);

    }
}}

rulename method in my fragment through which i get the position of the view.Add boolean selected in your POJO if this item is selected or not. 我的片段中的rulename方法,通过它我可以获取视图的位置。如果未选择此项,则在POJO中添加选择的布尔值。 Hope this help.Happy Coding.Vote if you find it useful. 希望对您有帮助。如果您觉得有用,请投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM