gpt4 book ai didi

android - 如何更改适配器内 View 的高度?

转载 作者:搜寻专家 更新时间:2023-11-01 09:30:17 26 4
gpt4 key购买 nike

我想改变 View 的高度并降低所有底部 View 的高度。这在 fragment 中运行良好,但在适配器中不起作用。我不想挤压主视图,我只需要增加 containerAvatar 的大小。

public class OrderAdapter extends ArrayAdapter {

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = layoutInflater.inflate(R.layout.open_order_item, parent, false);

containerAvatar = convertView.findViewById(R.id.container_avatar_size);
imgAvatar = convertView.findViewById(R.id.img_open_order_avatar);

Glide.with(context)
.load("http://neuronovosti.ru/wp-content/uploads/2017/12/Drosophila-Brain-1024x1024.jpg")
.into(imgAvatar);

containerAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ViewGroup.LayoutParams lp = containerAvatar.getLayoutParams();
final ResizeAnimation a = new ResizeAnimation(containerAvatar);
a.setDuration(500);
a.setParams(lp.height, lp.height * 2);
containerAvatar.startAnimation(a);
}
});

return convertView;
}

public class ResizeAnimation extends Animation {
private int startHeight;
private int deltaHeight; // distance between start and end height
private View view;

public ResizeAnimation(View v) {
this.view = v;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
view.requestLayout();
}

public void setParams(int start, int end) {

this.startHeight = start;
deltaHeight = end - startHeight;
}

@Override
public void setDuration(long durationMillis) {
super.setDuration(durationMillis);
}

@Override
public boolean willChangeBounds() {
return true;
}

enter image description here

这就是你所需要的
enter image description here

enter image description here

最佳答案

尝试使用 setLayoutParams() 方法 View -

private void setDimensions(View view, int width, int height){
android.view.ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}

将其添加到您的适配器上 -

public class OrderAdapter extends ArrayAdapter {

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = layoutInflater.inflate(R.layout.open_order_item, parent, false);

containerAvatar = convertView.findViewById(R.id.container_avatar_size);
imgAvatar = convertView.findViewById(R.id.img_open_order_avatar);

Glide.with(context)
.load("http://neuronovosti.ru/wp-content/uploads/2017/12/Drosophila-Brain-1024x1024.jpg")
.into(imgAvatar);

containerAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setDimensions(containerAvatar, 100, 100);
}
});

return convertView;
}

private void setDimensions(View view, int width, int height){
android.view.ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}

}

关于android - 如何更改适配器内 View 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47689538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com