我想查看RecyclerView
中其中一项的属性。更具体一点,我想检查是否选中了这个项目。
首先我选择项目
onView(withId(R.id.list_master))
.perform(
RecyclerViewActions.actionOnItemAtPosition(14, clickAnItem(R.id.layout_menu))
);
其次检查是否选择了特定项目:
onView(allOf(withId(R.id.layout_menu), hasDescendant(withText("SO"))))
.check(matches(isSelected()));
使用自定义匹配器:
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");
}
};
遗憾的是,未选择此视图的状态,但UI显示它已成功标记为所选项目。实施细节
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.itemView.setSelected(position == selected);
答案 0 :(得分:2)
检查此代码我有同样的想法,想要检查在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方法,通过该方法,我得到了view的位置。如果选择了此项,则在POJO中选择添加布尔值。 希望这有帮助。快乐编码。如果你觉得有用,就投票。