如何在Android中为recyclerview中的单元格设置不同的宽度
在我的recyclerview中,我将地址作为数据之一。如果地址长度>50dp(比如说),那么我想把它切割成2行(对于那个单元),这会增加这个单元的高度。然而,我想让所有的细胞匹配这个新的高度(保持一致) 请让我知道如何做到这一点。下面有什么我可以调整的吗如何在Android中为recyclerview中的单元格设置不同的宽度,android,android-layout,Android,Android Layout,在我的recyclerview中,我将地址作为数据之一。如果地址长度>50dp(比如说),那么我想把它切割成2行(对于那个单元),这会增加这个单元的高度。然而,我想让所有的细胞匹配这个新的高度(保持一致) 请让我知道如何做到这一点。下面有什么我可以调整的吗 public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > { List <
public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > {
List < Customer > mCustomerList;
public CustomerAdapter(List < Customer > customerList) {
this.mCustomerList = customerList;
}
@Override
public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_customer, parent, false);
return new CustomerViewHolder(view);
}
@Override
public void onBindViewHolder(CustomerViewHolder holder, int position) {
Customer customer = mCustomerList.get(position);
holder.tvName.setText(customer.mCustomerName);
holder.tvAddress.setText(customer.mCustomerAddress);
}
@Override
public int getItemCount() {
return mCustomerList.size();
}
public class CustomerViewHolder extends RecyclerView.ViewHolder {
TextView tvName, tvAddress;
public CustomerViewHolder(View itemView) {
super(itemView);
this.tvName = (TextView) itemView.findViewById(R.id.text_view_name);
this.tvAddress = (TextView) itemView.findViewById(R.id.text_view_address);
}
}
}
公共类CustomerAdapter扩展了RecyclerView.Adapter{
列表mccustomerlist;
公共CustomerAdapter(列表<客户>客户列表){
this.mcCustomerList=客户列表;
}
@凌驾
public CustomerViewHolder onCreateViewHolder(视图组父级,int-viewType){
View=LayoutFlater.from(parent.getContext()).flate(R.layout.card_customer,parent,false);
返回新的CustomerServiceWholder(视图);
}
@凌驾
BindViewHolder上的公共无效(CustomerViewHolder,int位置){
Customer=mccustomerlist.get(位置);
holder.tvName.setText(customer.mccustomername);
holder.tvAddress.setText(customer.mccustomerAddress);
}
@凌驾
public int getItemCount(){
返回mcCustomerList.size();
}
公共类CustomerViewHolder扩展了RecyclerView.ViewHolder{
text查看tvName、tvAddress;
公共CustomerViewHolder(视图项视图){
超级(项目视图);
this.tvName=(TextView)itemView.findViewById(R.id.text\u view\u name);
this.tvAddress=(TextView)itemView.findViewById(R.id.text\u view\u address);
}
}
}
最简单的解决方案是使文本字段的高度始终为2行,这样单元格就会统一
如果您只想在至少1个单元格需要2行的情况下使用2行,则需要测量每个文本字段需要多少行,并确定是否至少有1个文本字段需要2行,然后将它们全部设置为2行,否则将它们设置为1行
可以使用来测量文本在呈现之前所需的行数