我有一個自定義視圖,需要能夠在兩個不同的適配器中使用。在多個適配器中使用自定義視圖
這裏是自定義視圖的佈局(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