在activity_main.xml中,我使用带有id信息的TextView来显示带有id item_name的recycler_view列表中点击文本的信息。 Recycler视图显示100个项目的列表及其名称,并切换为活动和非活动状态。
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.recyclerviewtesting.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="eraseInfo"
android:padding="20dp"
android:textColor="#000000"
android:textColorHint="#999999" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
</LinearLayout>
</layout>
项目,即在回收者视图中使用: 这是我的item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="item"
type="com.app.recyclerviewtesting.Item" />
<variable
name="listener"
type="com.app.recyclerviewtesting.AdapterItemsClickHandler" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="@{() -> listener.onItemNameClick(item)}"
android:text="@{item.name}"
android:textSize="20sp" />
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:checked="@{item.on}"
android:onClick="@{() -> listener.onToggleClick(item)}"
tools:ignore="RelativeOverlap" />
</RelativeLayout>
</layout>
这是MainActivity.java
public class MainActivity extends AppCompatActivity
{
public static final String ITEM_NAME_FORMAT = "Item :%d";
public static final String ITEM_SELECTED_FORMAT = "%s is selected.";
public static final String INFO_HINT = "Click on item!";
private ActivityMainBinding mBinding;
private List<Item> mMainList = new ArrayList<>();
private RecyclerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initViews();
}
private void initViews() {
mBinding.info.setHint(INFO_HINT);
for (int i = 0; i < 100; i++) {
mMainList.add(new Item(String.format(Locale.getDefault(), ITEM_NAME_FORMAT, i), false));
}
mAdapter = new RecyclerAdapter(mMainList, new HandleAdapterActions());
mBinding.recyclerView.setAdapter(mAdapter);
}
public void eraseInfo(View view) {
mBinding.info.setText("");
}
private class HandleAdapterActions implements AdapterItemsClickHandler {
@Override
public void onItemNameClick(Item item) {
mBinding.info.setText(String.format(Locale.getDefault(), ITEM_SELECTED_FORMAT, item.getName()));
}
@Override
public void onToggleClick(Item item) {
item.setOn(!item.getOn());
mAdapter.notifyItemChanged(mMainList.indexOf(item));
}
}
}
这是适配器
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private final int ITEM_VIEW_TYPE = 1;
private List<Item> mainList;
private AdapterItemsClickHandler mListener;
public RecyclerAdapter(List<Item> mainList, AdapterItemsClickHandler mListener) {
this.mainList = mainList;
this.mListener = mListener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
if (viewType == ITEM_VIEW_TYPE) {
final ItemBinding binding = ItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
binding.setListener(mListener);
viewHolder = new ItemViewHolder(binding);
} else {
viewHolder = null;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
final ItemBinding binding = itemViewHolder.binding;
Item item = mainList.get(position);
binding.setItem(item);
}
}
@Override
public int getItemCount() {
return mainList.size();
}
@Override
public int getItemViewType(int position) {
return ITEM_VIEW_TYPE;
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
public ItemBinding binding;
ItemViewHolder(ItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
如果在第15位开关打开或关闭,请帮助我找到编写测试用例的方法。
答案 0 :(得分:2)
以下是此问题的解决方案:
public static Matcher<View> withToggleMatcher(final int position) {
return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(RecyclerView recyclerView) {
final RecyclerAdapter.ItemViewHolder viewHolder = (RecyclerAdapter.ItemViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
return viewHolder.binding.toggle.isChecked();
}
};
}
@Test
public void checkIfToggleIsOnAfterClickOnIt() {
int position = 15;
String itemName = String.format(Locale.getDefault(), MainActivity.ITEM_NAME_FORMAT, position);
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.scrollToPosition(position));
onView(allOf(withId(R.id.toggle), hasSibling(withText(itemName))))
.perform(click());
onView(withId(R.id.recyclerView))
.check(matches(withToggleMatcher(position)));
}