我有一个自定义视图,需要能够在两个不同的适配器中使用。在多个适配器中使用自定义视图
这里是自定义视图的布局(custom_view.xml
):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container">
<ImageView
android:id="@+id/imageView"
android:layout_height="100dp"
android:layout_width="100dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
这里是自定义视图的类(CustomView.java
):
public class CustomView extends RelativeLayout {
private final String TAG = "CustomView";
private User user;
private RelativeLayout container;
private ImageView imageView;
private TextView textView;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, User user) {
super(context, attrs);
this.user = user;
init();
}
public CustomView(Context context, User user) {
super(context);
this.user = user;
init();
}
private void init() {
inflate(getContext(), R.layout.custom_view, this);
container = (RelativeLayout) findViewById(R.id.container);
avatar = (ImageView) findViewById(R.id.imaveView);
displayName = (TextView) findViewById(R.id.textView);
// Add the user profile picture to the imageView
Glide.with(getContext())
.load("http://www.website.com/img/users/" + user.getId() + "/avatar.png")
.into(imageView);
// Set the user username in the textView
textView.setText(user.getUsername());
}
}
现在,我需要使用自定义视图中的两个不同适配器。
第一适配器我需要是一个普通的RecyclerView
适配器,它目前看起来像这样使用的自定义视图:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
private Context context;
private List<User> userData;
public CustomAdapter(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) {
// TODO: WHAT DO I PUT HERE?
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// TODO: WHAT DO I PUT HERE?
}
@Override
public int getItemCount() {
return userData.size();
}
}
我怎么能膨胀的自定义视图在这个类,我怎么能绑定userData
它?
我需要使用自定义视图的第二个适配器是AndroidTreeView library的适配器。
这里是我的适配器(到目前为止)为AndroidTreeView库:
public class CustomTreeAdapter extends TreeNode.BaseNodeViewHolder<CustomTreeAdapter.TreeItem> {
public CustomTreeAdapter(Context context) {
super(context);
}
@Override
public View createNodeView(TreeNode node, TreeItem value) {
// TODO: WHAT DO I PUT HERE?
}
public static class TreeItem {
// TODO: WHAT DO I PUT HERE?
}
}
仅供参考,这里是integration documentation为AndroidTreeView库。
我应该在上面的方法中加入什么?
感谢。
我已经在自定义视图类中设置了所有的textview文本,因此我再次在ViewHolder中执行'setText()'是没有意义的。 – l890456
但我认为ViewHolder是你应该为你的物品/行设置视图的地方? –
在每个适配器中执行'setText()'两次,如果我可以在自定义视图类中执行一次,则没有任何意义。 – l890456