我有一个基本的自定义视图,如下所示:
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() }}
如您所见,我想在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 = userData; } public class VIEwHolder extends RecyclerVIEw.VIEwHolder { public VIEwHolder(VIEw v) { super(v); } } @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { LayoutInflater inflater = LayoutInflater.from(context); // HOW TO INFLATE THE CUSTOM VIEW? // VIEwHolder vIEwHolder = new VIEwHolder(customVIEw); return vIEwHolder; } @OverrIDe public voID onBindVIEwHolder(final VIEwHolder holder, int position) { // ANYTHING HERE? } @OverrIDe public int getItemCount() { return userData.size(); }}
如何在适配器中充气自定义视图?
另外,我应该在onBindVIEwHolder()中添加任何内容吗?
注意:我必须使用自定义视图,因为我在不同的适配器下使用此视图(即:不仅仅是这个RecyclerVIEw适配器).
解决方法:
假设一个CustomVIEw类看起来像这样:
public class CustomVIEw extends relativeLayout { private User user; private ImageVIEw profilePicture; // overrIDe all constructors to ensure custom logic runs in all cases public CustomVIEw(Context context) { this(context, null); } public CustomVIEw(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomVIEw(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public CustomVIEw( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes ) { super(context, attrs, defStyleAttr, defStyleRes); // put all custom logic in this constructor, which always runs inflate(getContext(), R.layout.custom_layout, this); profilePicture = (ImageVIEw) findVIEwByID(R.ID.profilePicture); } public voID setUser(User newUser) { user = newUser; // ACCESS USER MODEL HERE // e.g. user.getUsername() }}
您的RecyclerVIEw.Adapter和RecyclerVIEw.VIEwHolder看起来像这样:
public class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.VIEwHolder> { // no Context reference needed—can get it from a VIEwGroup parameter private List<User> userData; public MyAdapter(List<User> userData) { // make own copy of the List so it can't be edited externally this.userData = new ArrayList<User>(userData); } @OverrIDe public int getItemCount() { return userData.size(); } @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) { // no need for a LayoutInflater instance— // the custom vIEw inflates itself CustomVIEw itemVIEw = new CustomVIEw(parent.getContext()); // manually set the CustomVIEw's size itemVIEw.setLayoutParams(new VIEwGroup.LayoutParams( VIEwGroup.LayoutParams.MATCH_PARENT, VIEwGroup.LayoutParams.WRAP_CONTENT )); return new VIEwHolder(itemVIEw); } @OverrIDe public voID onBindVIEwHolder(final VIEwHolder holder, int position) { holder.getCustomVIEw().setUser(userData.get(position)); } public class VIEwHolder extends RecyclerVIEw.VIEwHolder { private CustomVIEw customVIEw; public VIEwHolder(VIEw v) { super(v); customVIEw = (CustomVIEw) v; } public CustomVIEw getCustomVIEw() { return customVIEw; } }}
> CustomVIEw管理自己的设置,它在自己的构造函数中发生,在这种情况下使用XML文件的膨胀. (或者,它可以以编程方式设置其子视图.)
>因此,RecyclerVIEw.Adapter不需要执行任何通胀 – 它只是创建一个新的CustomVIEw实例,并让CustomVIEw担心自己的设置.
>在调用setUser方法之前,CustomVIEw无法获取User实例,因此在构造函数中不能进行用户访问.在任何情况下,在一个CustomVIEw生命周期内,RecyclerVIEw可以要求它在不同时间显示许多不同用户的信息. CustomVIEw需要能够执行此 *** 作.因此,引入了setUser方法.
>因为CustomVIEw是通过代码而不是XML实例化的,所以无法在XML中定义大小的属性.因此,在定时之后以编程方式完成大小调整.
> onBindVIEwHolder只需在CustomVIEw上调用setUser即可将CustomVIEw与正确的User实例链接起来.
> VIEwHolder类现在只是RecyclerVIEw项和CustomVIEw之间的链接.
在RecyclerVIEws中使用来自另一个类的预构建的自定义视图(即不在RecyclerVIEw.Adapter中膨胀XML)似乎从未被讨论过.我认为这是一个很好的想法,即使自定义视图专门用于RecyclerVIEw,因为它是promotes separation of concerns和adherence to the Single Responsibility Principle.
总结以上是内存溢出为你收集整理的android – 在RecyclerView适配器中使用自定义视图?全部内容,希望文章能够帮你解决android – 在RecyclerView适配器中使用自定义视图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)