Android putExtra(“roomTitle”、“Title”); //意图。额外(“房间状态”、“副标题”); } @试验 public void postsRecycleViewHomeAdapter(){ //设置虚拟数据 List roomProduct
putExtra(“roomTitle”、“Title”); //意图。额外(“房间状态”、“副标题”); } @试验 public void postsRecycleViewHomeAdapter(){ //设置虚拟数据 List roomProductList=Arrays.asList( 新的RoomProduct(0,右拉深,ic_启动器_背景,“点击”,“打开”,05f), 新的RoomProduct(1,R.drawable.ic_发射器_背景,“淋浴”,“运行”,0.5f), 新RoomProduct(2,R.drawable.ic_launcher_背景,“电视频道”,“53”,0.5f) ); RoomAdapter=新的RoomAdapter(上下文,roomProductList); RecyclerView rvParent=新的RecyclerView(上下文); rvParent.setLayoutManager(新的LinearLayoutManager(上下文)); //对onBindViewHolder和虚拟数据上的ViewHolder运行测试 RoomAdapter.ItemViewAdapterHolder viewHolder=adapter.onCreateViewHolder(rvParent,0); //测试1 adapter.onBindViewHolder(viewHolder,0); assertEquals(“点击”,viewHolder.roomItem.getText()); assertEquals(“On”,viewHolder.roomState.getText()); ShadowDrawable ShadowDrawable=Shadows.shadowOf(viewHolder.roomImageItem.getDrawable()); assertEquals(R.drawable.ic_launcher_background,shadowDrawable.getCreatedFromResId()); //测试2 适配器。onBindViewHolder(viewHolder,1); assertEquals(“Shower”,viewHolder.roomItem.getText()); assertEquals(“Running”,viewHolder.roomState.getText()); ShadowDrawable ShadowDrawable 1=Shadows.shadowOf(viewHolder.roomImageItem.getDrawable()); assertEquals(R.drawable.ic_launcher_background,shadowDrawable1.getCreatedFromResId()); //测试3 适配器。onBindViewHolder(viewHolder,2); assertEquals(“电视频道”,viewHolder.roomItem.getText()); assertEquals(“53”,viewHolder.roomState.getText()); ShadowDrawable ShadowDrawable 2=Shadows.shadowOf(viewHolder.roomImageItem.getDrawable()); assertEquals(R.drawable.ic_launcher_background,shadowDrawable2.getCreatedFromResId()); } }Android putExtra(“roomTitle”、“Title”); //意图。额外(“房间状态”、“副标题”); } @试验 public void postsRecycleViewHomeAdapter(){ //设置虚拟数据 List roomProduct,android,android-recyclerview,Android,Android Recyclerview,putExtra(“roomTitle”、“Title”); //意图。额外(“房间状态”、“副标题”); } @试验 public void postsRecycleViewHomeAdapter(){ //设置虚拟数据 List roomProductList=Arrays.asList( 新的RoomProduct(0,右拉深,ic_启动器_背景,“点击”,“打开”,05f), 新的RoomProduct(1,R.drawable.ic_发射器_背景,“淋浴”,“运行”,0.5f), 新R
RuntimeEnvironment.application已弃用。另一种解决方案是使用Kotlin语法的context=ApplicationProvider.getApplicationContext()
public class HomeAdapter extends RecyclerView.Adapter <HomeAdapter.ProductViewHolder> {
private Context mCtx;
private List <HomeProduct> homeProductList;
public HomeAdapter(Context context, List <HomeProduct> homeProductList) {
this.mCtx = context;
this.homeProductList = homeProductList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.home_item_layout, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
final HomeProduct product = homeProductList.get(position);
final String title = product.getTitle();
final String subtitle = product.getSubtitle();
holder.homeImageView.setImageDrawable(ContextCompat.getDrawable(mCtx, product.getImage()));
holder.homeTitleTextView.setText(product.getTitle());
holder.homeSubtitleTextView.setText(product.getSubtitle());
holder.homeLinearlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentRoomActivity = new Intent(mCtx, RoomActivity.class);
intentRoomActivity.putExtra("room_title", title);
intentRoomActivity.putExtra("room_state", subtitle);
mCtx.startActivity(intentRoomActivity);
}
});
}
@Override
public int getItemCount() {
return homeProductList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView homeTitleTextView, homeSubtitleTextView;
ImageView homeImageView;
LinearLayout homeLinearlayout;
public ProductViewHolder(View itemView) {
super(itemView);
homeTitleTextView = itemView.findViewById(R.id.home_title_textView);
homeSubtitleTextView = itemView.findViewById(R.id.home_subtitle_textView);
homeImageView = itemView.findViewById(R.id.home_imageView);
homeLinearlayout = itemView.findViewById(R.id.homeLinearLayout);
}
}
package com.codepath.testingdemo.adapters;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.codepath.testingdemo.BuildConfig;
import com.codepath.testingdemo.models.Post;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.Arrays;
import java.util.List;
import static org.assertj.android.api.Assertions.assertThat;
import static org.assertj.android.recyclerview.v7.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class)
public class PostsAdapterTest {
private Context context;
@Before
public void setup() {
context = RuntimeEnvironment.application;
}
@Test
public void postsAdapterViewRecyclingCaption() {
// Set up input
List<Post> posts = Arrays.asList(
new Post("Lebron", null),
new Post("Steph", "We Won!!!")
);
PostsRecyclerViewAdapter adapter = new PostsRecyclerViewAdapter(posts);
RecyclerView rvParent = new RecyclerView(context);
rvParent.setLayoutManager(new LinearLayoutManager(context));
// Run test
PostsRecyclerViewAdapter.PostItemViewHolder viewHolder =
adapter.onCreateViewHolder(rvParent, 0);
adapter.onBindViewHolder(viewHolder, 0);
// JUnit Assertion
assertEquals(View.GONE, viewHolder.tvCaption.getVisibility());
// AssertJ-Android Assertion
assertThat(viewHolder.tvCaption).isGone();
adapter.onBindViewHolder(viewHolder, 1);
// JUnit Assertion
assertEquals("Steph: We Won!!!", viewHolder.tvCaption.getText().toString());
// AssertJ-Android Assertion
assertThat(viewHolder.tvCaption).isVisible().containsText("Won");
assertThat(adapter).hasItemCount(2);
}
}
import static junit.framework.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class RoomAdapterTest {
private Context context;
@Before
public void setUp() throws Exception
{
context = RuntimeEnvironment.application;
// Intent intent = new Intent();
// intent.putExtra("roomTitle", "Title");
// intent.putExtra("roomState","Subtitle");
}
@Test
public void postsRecycleViewHomeAdapter() {
// Set up dummy data
List<RoomProduct> roomProductList = Arrays.asList(
new RoomProduct(0, R.drawable.ic_launcher_background,"Tap", "On" , 05f),
new RoomProduct(1, R.drawable.ic_launcher_background, "Shower", "Running", 0.5f),
new RoomProduct(2, R.drawable.ic_launcher_background, "TV Channel", "53", 0.5f)
);
RoomAdapter adapter = new RoomAdapter(context,roomProductList);
RecyclerView rvParent = new RecyclerView(context);
rvParent.setLayoutManager(new LinearLayoutManager(context));
// Run test On onBindViewHolder and ViewHolder on dummy data
RoomAdapter.ItemViewAdapterHolder viewHolder = adapter.onCreateViewHolder(rvParent, 0);
//Test number 1
adapter.onBindViewHolder(viewHolder, 0);
assertEquals("Tap", viewHolder.roomItem.getText());
assertEquals("On", viewHolder.roomState.getText());
ShadowDrawable shadowDrawable = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
assertEquals(R.drawable.ic_launcher_background, shadowDrawable.getCreatedFromResId());
//Test number 2
adapter.onBindViewHolder(viewHolder,1);
assertEquals("Shower", viewHolder.roomItem.getText());
assertEquals("Running", viewHolder.roomState.getText());
ShadowDrawable shadowDrawable1 = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
assertEquals(R.drawable.ic_launcher_background, shadowDrawable1.getCreatedFromResId());
//Test number 3
adapter.onBindViewHolder(viewHolder, 2);
assertEquals("TV Channel", viewHolder.roomItem.getText());
assertEquals("53", viewHolder.roomState.getText());
ShadowDrawable shadowDrawable2 = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
assertEquals(R.drawable.ic_launcher_background, shadowDrawable2.getCreatedFromResId());
}
}