我曾经用Android Studio中的Project-Template设置了一个折叠工具栏,并试图在NestedScrollView中实现ListView(请参见下面的代码)
我拥有最新的Android Studio版本,并且AVD在Android 9上运行
我已经尝试过
package com.erhein.cdroid.classdroid;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.erhein.cdroid.classdroid.models.G11S1;
import java.util.ArrayList;
public class StudentAdapter extends BaseAdapter {
Activity context;
ArrayList<G11S1> students;
private static LayoutInflater inflater = null;
public StudentAdapter(Activity context, ArrayList<G11S1> students) {
this.context = context;
this.students = students;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return students.size();
}
@Override
public G11S1 getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
itemView = (itemView == null) ? inflater.inflate(R.layout.list_item, null): itemView;
TextView textViewName = (TextView) itemView.findViewById(R.id.textViewName);
TextView textViewAge = (TextView) itemView.findViewById(R.id.textViewAge);
G11S1 selectedStudent = students.get(position);
textViewName.setText(selectedStudent.Name);
textViewAge.setText(selectedStudent.Age);
return itemView;
}
}
我发现了一些使用android:fillViewport="true"
的解决方案,我不想使用这种方法-如果这是最后一种解决方法,我将;)。 (这是针对学校项目的,我们应该只使用在学校 face palm 中学到的组件)
我将在下面跳过一些XML导入行:
RecyclerView
NestedListView
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainScreen">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<fragment
android:id="@+id/fragment1"
android:name="net.mountdev.alcohollevel.EntryList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@drawable/ic_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
当前结果是,ListView位于ActionBar中,不应位于该位置(请参见屏幕截图)
答案 0 :(得分:0)
将android.support.design.widget.CoordinatorLayout
更改为RelativeLayout
在您的AppBarLayout
内部使用android:layout_alignParentTop="true"
为您的fragment
添加android:layout_below="@+id/app_bar"
答案 1 :(得分:0)
问题出在fragment
上。它占用了整个空间。尝试类似
<RelativeLayout
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="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<fragment
android:layout_below="@+id/app_bar"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/app_bar"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp"
android:layout_marginEnd="16dp"
app:layout_anchorGravity="bottom|end"/>