这是我的 onBindViewHolder 方法:
@Override
public void onBindViewHolder(@NonNull final mViewHolder h, int i) {
final JSON data = jdata[i];
if(data .getName() != null && data .getStatus() !=null) {
h.textcontainer.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
h.title.setText(feed.getName());
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 2
);
h.textcontainer.setVisibility(View.GONE);
h.playerView.setLayoutParams(params);
}
这是我的 XML:
<LinearLayout
android:id="@+id/videopost_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:id="@+id/video_textcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="@dimen/feed_item_padding_left_right"
android:paddingRight="@dimen/feed_item_padding_left_right"
android:paddingBottom="@dimen/feed_item_padding_top_bottom"
android:paddingTop="@dimen/feed_item_padding_top_bottom"
android:layout_weight="1"
>
<TextView
android:id="@+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="@color/background"
android:paddingStart="@dimen/feed_item_profile_info_padd"
android:paddingEnd="@dimen/feed_item_profile_info_padd"
/>
</LinearLayout>
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:show_buffering="true"
android:layout_weight="1"
>
</com.google.android.exoplayer2.ui.PlayerView>
我的适配器中有一个textview,当json数据= null时,我显然想隐藏该TextView而不留任何间距,并且当数据(视频标题)不为空时,我想显示文本。 当前代码隐藏了 TextView,但留下了空白空间。如果我从 if 语句中删除以下两行:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
那么就没有剩余空间了,但是如果数据不为空,TextView将不会显示。我很确定这是某个地方的一些愚蠢错误,但我无法找到它。求救:-)
最佳答案
首先,您有两个单独的布局 LinearLayout
和PlayerView
共享两 (2) 的权重总和,使其成为 50:50 的分布。是否为 subview LinearLayout
消失后,50% 的空间仍将保留,直至分配完毕。
因此,任何时候您需要完全隐藏 textview/title 并删除不需要的空间,动态设置 PlayerView
的权重为两 (2) 并设置 LinearLayout
的可见性走了。
关于java - View.Gone 位于适配器内部,在 onBindViewHolder 中留下空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483129/