1

我有一个自定义视图,需要能够在两个不同的适配器中使用。在多个适配器中使用自定义视图

这里是自定义视图的布局(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库。

我应该在上面的方法中加入什么?

感谢。

回答

1

对于RecyclerView,在你onCreateViewHolder()我把这个:

@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,把这个对onBindViewHolder():

@Override 
public void onBindViewHolder(CustomViewHolder holder, int position) { 
    holder.textView.setText(userData.get(position).getName()); 
} 

当然,这意味着你必须先找到你ViewHolder TextView的:

class CustomViewHolder extends RecyclerView.ViewHolder { 
    TextView textView; 

    CustomViewHolder(View v) { 
     super(v); 
     textView = (TextView) v.findViewById(R.id.textView); 
    } 
} 

为L东我认为这是你怎么用RecyclerView和ViewHolder做到这一点,我想你可能想将你的代码像init()一样移动到ViewView类中,也许可以移动到ViewHolder中。

+0

我已经在自定义视图类中设置了所有的textview文本,因此我再次在ViewHolder中执行'setText()'是没有意义的。 – l890456

+0

但我认为ViewHolder是你应该为你的物品/行设置视图的地方? –

+0

在每个适配器中执行'setText()'两次,如果我可以在自定义视图类中执行一次,则没有任何意义。 – l890456

相关问题