gpt4 book ai didi

java - 折叠和展开动画后布局高度未正确更新

转载 作者:行者123 更新时间:2023-11-30 01:41:25 26 4
gpt4 key购买 nike

早上好。

我正在尝试创建一个动态布局,您可以在其中添加和删除标签。布局还必须是可折叠和可展开的。

现在一切正常,布局高度通过添加和删除标签正确更新,但在我执行折叠和展开后功能停止。我不确定这是什么原因。如果有人能为我指出正确的方向,我将非常高兴!

代码:

public class FilterActivity extends AppCompatActivity {

Logger logger = Logger.getLogger();

LinearLayout tagsContainerLayout;

Switch tagSwitch;

// tag button
ImageButton addTagButton;
Button goButton;

// heights for expanding
Map<Integer, Integer> originalHeights;

// tags
ArrayList<String> selectedTags;

// tag text bar
AutoCompleteTextView tagsBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

originalHeights = new HashMap<>();
selectedTags = new ArrayList<>();

// tag bar
tagsBar = (AutoCompleteTextView) findViewById(R.id.tagSearch);

// add tag
addTagButton = (ImageButton) findViewById(R.id.addBtn);
addTagButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tag = tagsBar.getText().toString();

if (!selectedTags.contains(tag)){
addTagToLayout(tag);
}
}
});

// start search
goButton = (Button) findViewById(R.id.startSearchBtn);

// switches
tagSwitch = (Switch) findViewById(R.id.tagSwitch);

tagSwitch.setChecked(true);

tagLayout = (RelativeLayout) findViewById(R.id.tagsLayout);
tagsContainerLayout = (LinearLayout) findViewById(R.id.tagContainer);

// layouts
addChecked(tagSwitch, tagLayout);
}



private void addTagToLayout(final String tagText)
{
// create tag
final TextView newTag = new TextView(this);
newTag.setText(tagText);

newTag.setTextSize(getResources().getDimension(R.dimen.textsize));

Drawable img = this.getResources().getDrawable(R.drawable.delete_tag_icon);
newTag.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
logger.d(tagText);

newTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));

tagsContainerLayout.addView(newTag);

logger.e("tag container height = " + tagsContainerLayout.getHeight());
logger.e("tag top layout height = " + tagLayout.getHeight());

ViewGroup.LayoutParams params = tagLayout.getLayoutParams();
//params.height = newTag.getHeight() + tagLayout.getHeight();
logger.e("new attempted height = " + String.valueOf(params.height + newTag.getHeight()));

logger.e("params height = " + params.height);
logger.e("newTag height = " + newTag.getHeight());

//tagLayout.setLayoutParams(params);

selectedTags.add(tagText);


newTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tagsContainerLayout.removeView(newTag);
tagsContainerLayout.invalidate();
//tagsLayout.re
selectedTags.remove(tagText);

}
});

tagLayout.requestLayout();
tagsContainerLayout.requestLayout();
}

public void addChecked(final Switch s, final View target)
{
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expand(target);
} else {
collapse(target);
}
}
});
}

private void expand(View summary) {
//set Visible
summary.setVisibility(View.VISIBLE);

//final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int height = originalHeights.get(summary.getId());

//summary.measure(widthSpec, height);

ValueAnimator mAnimator = slideAnimator(0, 400, summary);

mAnimator.start();
}

private void saveOriginalHeight(int id, int height)
{
originalHeights.put(id, height);
}

private void collapse(final View summary) {
int finalHeight = summary.getHeight();

saveOriginalHeight(summary.getId(), finalHeight);

ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
//Height=0, but it set visibility to GONE
summary.setVisibility(View.GONE);
}

@Override
public void onAnimationStart(Animator animator) {
}

@Override
public void onAnimationCancel(Animator animator) {
}

@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
}


private ValueAnimator slideAnimator(int start, int end, final View summary) {

ValueAnimator animator = ValueAnimator.ofInt(start, end);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();

ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
layoutParams.height = value;
summary.setLayoutParams(layoutParams);
}
});
return animator;
}
}

如您所见,我一直在尝试手动执行此操作,但它不起作用,因为 newTag.getHeight() 出于某种原因不断返回 0。

最佳答案

这可能是因为您试图在渲染之前获取它的高度,请尝试使用 measure , 然后 getMeasuredHeight而不是在新创建的标签上使用 getHeight

关于java - 折叠和展开动画后布局高度未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460819/

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