早上好。窥视。
我正在尝试创建一个动态布局,您可以在其上添加和删除标签。布局也必须是可折叠和可扩展的。
现在一切正常,布局高度通过添加和删除标签正确更新,但在执行折叠和展开后功能停止。我不确定原因是什么。如果有人能指出我正确的方向,我会非常高兴!
代码:
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,因此无法正常工作。
答案 0 :(得分:1)
这可能是因为你想要在渲染之前确定它的高度,尝试使用measure
,然后使用getMeasuredHeight
代替echo -e "to be prepended \n another line" | cat - text.txt | sponge text.txt
在你新创建的标签上。