我有一个基本的自定义视图,如下所示:
public class CustomView extends RelativeLayout {
private User user;
private ImageView profilePicture;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
inflate(getContext(), R.layout.custom_layout, this);
profilePicture = (ImageView) findViewById(R.id.profilePicture);
// ACCESS USER MODEL HERE
// e.g. user.getUsername()
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我想在View中访问用户数据(即:)user.getUsername().
我还需要能够在RecyclerView适配器中使用自定义视图.
这是我的适配器目前的样子:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<User> userData;
public MyAdapter(Context context, List<User> userData) {
this.context = context;
this.userData …Run Code Online (Sandbox Code Playgroud)