因此,我有一个RecycleView
的布局文件,该文件具有可扩展/可折叠的视口。
单击标题将展开/折叠多余的数据。一切在编辑器中看起来都很好。但是,它将具有类似于layout_marginBottom
的幻像空间。
首次加载外观
扩展
正确合拢
因此编辑器将仅显示ViewHolder
,如图2和3所示。
但是,在设备上运行时,它将首先显示图1,然后单击以展开它,它将显示图2。再次折叠它会显示图3而不是图1。它将继续显示正确的图。 (2和3)。
图2中的额外内容具有共同的父级,其标头为ConstraintLayout
。
无论GONE
指示符如何,都会发生这种情况:以编程方式还是XML
使用activity.runOnUIThread
无济于事。使用new Handler().onPostDelayed
只会更糟糕,就像显示图1一样,但是在没有额外信息的情况下,拥有更多不必要的空间,与图2一样多。
这是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:layout_marginBottom="10dp"
app:cardCornerRadius="10dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/validity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageButton
android:id="@+id/expand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
app:srcCompat="@drawable/ic_right" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/textDark" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:background="@color/colorSecondaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="@+id/size"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />
<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="@+id/validity"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/date" />
<LinearLayout
android:id="@+id/validity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/size">
<ImageView
android:id="@+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:contentDescription="@string/backup_layout_help_desc"
app:srcCompat="@drawable/ic_help" />
<TextView
android:id="@+id/invalid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/backup_layout_invalid"
android:textAllCaps="true"
android:textColor="@color/red" />
</LinearLayout>
<TextView
android:id="@+id/passwords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/amount" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="@android:color/transparent"
android:text="@string/backup_layout_delete"
android:textColor="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/importshare"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwords"
app:layout_constraintVertical_bias="1.0" />
<LinearLayout
android:id="@+id/importshare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/validity"
app:layout_constraintVertical_bias="1.0">
<Button
android:id="@+id/commit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="Import"
android:textColor="@color/yellow" />
<ImageButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:contentDescription="Share"
android:padding="10dp"
app:srcCompat="@drawable/ic_share" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
还有ViewHolder(剪切以显示相关部分):
public ViewHolder(View itemView, Context context) {
super(itemView);
Amount = itemView.findViewById(R.id.amount);
Date = itemView.findViewById(R.id.date);
Delete = itemView.findViewById(R.id.delete);
Divider = itemView.findViewById(R.id.divider);
ExpandCollapse = itemView.findViewById(R.id.expand);
Import = itemView.findViewById(R.id.commit);
ImportShare = itemView.findViewById(R.id.importshare);
Header = itemView.findViewById(R.id.header);
Help = itemView.findViewById(R.id.help);
Passwords = itemView.findViewById(R.id.passwords);
Share = itemView.findViewById(R.id.share);
Size = itemView.findViewById(R.id.size);
Title = itemView.findViewById(R.id.title);
Validity = itemView.findViewById(R.id.validity);
Title.setTypeface(Typeface.createFromAsset(context.getAssets(), "cera.otf"));
Header.setOnClickListener(this);
onClick(Header);
}
@Override
public void onClick(View v) {
if (expanded) collapse();
else expand();
}
public void expand() {
ExpandCollapse.animate().rotation(90).start();
Date.setVisibility(View.VISIBLE);
Size.setVisibility(View.VISIBLE);
Amount.setVisibility(View.VISIBLE);
Passwords.setVisibility(View.VISIBLE);
Divider.setVisibility(View.VISIBLE);
Validity.setVisibility(valid ? View.INVISIBLE : View.VISIBLE);
Delete.setVisibility(View.VISIBLE);
ImportShare.setVisibility(View.VISIBLE);
expanded = true;
}
public void collapse() {
ExpandCollapse.animate().rotation(0).start();
Date.setVisibility(View.GONE);
Size.setVisibility(View.GONE);
Amount.setVisibility(View.GONE);
Passwords.setVisibility(View.GONE);
Divider.setVisibility(View.GONE);
Validity.setVisibility(View.GONE);
Delete.setVisibility(View.GONE);
ImportShare.setVisibility(View.GONE);
expanded = false;
}
答案 0 :(得分:0)
科特琳
在打开键盘时尝试隐藏元素时,我遇到了非常相似的问题
会发生什么情况,当您打开键盘时,将得到两种调整大小,一种是调整开口和键盘的大小,另一种是调整视图的大小。问题是第二个调整仅在另一个元素请求布局时才会发生,因此您的屏幕会非常明显地跳动两次。
我已经找到了解决方案,但是仅当Window软输入模式=调整调整大小时
见下文
class MainActivity : AppCompatActivity() {
private var activityHeight = 0
private var keyboardOpen = false
private var mainScreen : ConstraintLayout? = null
private var infoText : TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_screen)
mainScreen = findViewById(R.id.mainConstraintLayout)
infoText = findViewById(R.id.mainInfoText)
//...
/* Capture initial screen size */
this@MainActivity.window.decorView.doOnNextLayout {
val displayFrame : Rect = Rect()
this@MainActivity.window.decorView.getWindowVisibleDisplayFrame(displayFrame)
activityHeight = displayFrame.height()
}
/* Check for keyboard open/close */
this@MainActivity.window.decorView.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
val drawFrame : Rect = Rect()
this@MainActivity.window.decorView.getWindowVisibleDisplayFrame(drawFrame)
val currentSize = drawFrame.height()
if(keyboardOpen){
keyboardOpen = currentSize < activityHeight
if (!keyboardOpen){
infoText?.visibility = View.VISIBLE
}
}else {
keyboardOpen = currentSize < activityHeight
if(keyboardOpen) {
infoText?.visibility = View.GONE
/* Request a new layout on your base layout so screen adjusts correctly */
mainScreen?.requestLayout()
}
}
}
}
}