2つの異なるアダプタで使用する必要があるカスタムビューがあります。ここで複数のアダプタでカスタムビューを使用する
は、カスタムビューのレイアウト(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
に?
カスタムビューを使用するために必要な2番目のアダプタは、AndroidTreeView libraryのアダプタです。ここでは、参考のため
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?
}
}
AndroidTreeViewライブラリのintegration documentationです:
はここAndroidTreeViewライブラリのための私のアダプタ(今のところ)です。
上記の方法ではどうすればよいですか?
ありがとうございました。 )
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_view, parent, false);
return new CustomViewHolder(view);
}
そして、あなたのViewHolderへのuserDataからデータをバインドするために、onBindViewHolder(のためにこれを置く:RecyclerViewは、あなたのonCreateViewHolderに()私はこれを置くところについては
私はすでにカスタムビュークラスにすべてのテキストビューテキストを設定しているので、ViewHolderで 'setText()'をもう一度やっても意味がありません。 – l890456
しかし、ViewHolderはアイテム/行のビューを設定する場所だと思いましたか? –
'setText()'を2回(各アダプタで1回)行うのは、カスタムビュークラスで1回しか実行できない場合は意味がありません。 – l890456