我有一个LinearLayout
,默认情况下在此android:visibility="gone"
中用LinearLayout
设置,我有一个RecyclerView
用我的活动的onCreate()
方法初始化。 / p>
将LinearLayout
设置为android:visibility="visible"
时,RecyclerView
不会显示其项目...
经过几天的思考,我来告诉我这个问题是由于在LinearLayout
中添加项目时RecyclerView
不可见,或者是由于LinearLayout
是使用Utils.expand(View view)
和Utils.collapse(View view)
方法动画的,但是我仍然不能解决我的问题...
XML-activity_main.xml
<Button
android:id="@+id/activityMain_button_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activityMain_button_filter"
android:textColor="@color/colorBlackOverlay"
android:textAllCaps="false"
style="?android:attr/buttonBarButtonStyle"
android:drawableRight="@drawable/vector_icon_expand"
android:drawableEnd="@drawable/vector_icon_expand"
android:drawablePadding="@dimen/content_padding_small"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/activityMain_linearLayout_filter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorCloudGray"
android:paddingTop="@dimen/content_padding_normal"
android:paddingLeft="@dimen/activity_horizontal_padding"
android:paddingRight="@dimen/activity_horizontal_padding"
android:paddingBottom="@dimen/content_padding_normal"
android:visibility="gone">
<!-- Others views -->
<android.support.v7.widget.RecyclerView
android:id="@+id/activityMain_recyclerView_filterLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.StaggeredGridLayoutManager"
app:spanCount="1"
android:descendantFocusability="blocksDescendants" />
<!-- Others views -->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
JAVA-MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonFilter = findViewById(R.id.activityMain_button_filter);
final LinearLayout linearLayoutFilter = findViewById(R.id.activityMain_linearLayout_filter);
RecyclerView recyclerFilterLocation = findViewById(R.id.activityMain_recyclerView_filterLocation);
final RecyclerLocationAdapter recyclerLocationAdapter = new RecyclerLocationAdapter(new ArrayList<>(filterLocation));
recyclerFilterLocation.setNestedScrollingEnabled(false);
recyclerFilterLocation.setHasFixedSize(true);
recyclerFilterLocation.setAdapter(recyclerLocationAdapter);
buttonFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (linearLayoutFilter.getVisibility() == View.GONE) {
Utils.expandView(linearLayoutFilter);
}
else {
Utils.collapseView(linearLayoutFilter);
}
}
}
}
}
JAVA-Utils.java
public class Utils {
public static void expandView(final View view) {
view.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final int targetHeight = view.getMeasuredHeight();
view.getLayoutParams().height = 1;
view.setVisibility(View.VISIBLE);
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = interpolatedTime == 1
? LinearLayout.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration((int) (targetHeight / view.getContext().getResources().getDisplayMetrics().density));
view.startAnimation(animation);
}
public static void collapseView(final View view) {
final int initialHeight = view.getMeasuredHeight();
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
view.setVisibility(View.GONE);
} else {
view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
view.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
animation.setDuration((int) (initialHeight / view.getContext().getResources().getDisplayMetrics().density));
view.startAnimation(animation);
}
}
谢谢您的帮助!