我想做这个post中描述的内容,但是我无法获得隐藏和滚动显示的功能。一切都在显示,抽屉菜单,标题视图和列表视图,但是当我上下滚动列表视图时,标题视图保持不变,不会隐藏和显示列表视图正在滚动。我已经查找了一些其他帖子,例如这些解决方案,但没有一个帮助。
Android design library CoordinatorLayout, AppBarLayout and DrawerLayout
DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show image
我有一个包含抽屉菜单和片段的活动。在Fragment中,我想要显示的CoordinatorLayout和AppBarLayout显示并隐藏列表视图滚动的标题视图。
以DrawerLayout作为根视图的Activity布局,其中包含主内容的FrameLayout和抽屉菜单内容的RelativeLayout。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content_frame"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"/>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="280dp"
android:layout_gravity="start"
android:background="#eee">
<RelativeLayout
android:id="@+id/sliding_menu_logo_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee">
<ImageView
android:id="@+id/sliding_menu_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="@id/sliding_menu_logo_container"
android:clipToPadding="true"
android:divider="@null"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false"
android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
片段布局,我希望在列表视图向上滚动时隐藏标题视图,并在列表视图向下滚动时显示标题视图。
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:id="@+id/my_appbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#eee"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="First Name"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Last Name"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/rv_numbers"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
应用主题是
Theme.AppCompat.Light.DarkActionBar
活动类
public class ScrollingActivity4 extends AppCompatActivity {
protected DrawerLayout drawerLayout;
RelativeLayout leftDrawerView;
protected ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling4);
ScrollingActivity4Fragment scrollingActivity4Fragment = new ScrollingActivity4Fragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, scrollingActivity4Fragment, "tag_scrollingActivity4Fragment")
.addToBackStack(null)
.commit();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
if (drawerToggle == null) {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
}
public void onDrawerOpened(View drawerView) {
}
public void onDrawerSlide (View drawerView, float slideOffset) {
}
public void onDrawerStateChanged(int newState) {
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
drawerToggle.syncState();
leftDrawerView = (RelativeLayout) findViewById(R.id.left_drawer);
ListView rvNumbers = (ListView) findViewById(R.id.list);
String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, numbers);
rvNumbers.setAdapter(itemArrayAdapter);
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
public class ItemArrayAdapter extends ArrayAdapter<String> {
String[] itemList;
private int listItemLayout;
public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int pos = position;
String item = getItem(pos);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout, parent, false);
viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.item.setText(item);
return convertView;
}
class ViewHolder {
TextView item;
}
}
}
片段类
public class ScrollingActivity4Fragment extends Fragment {
LinearLayout llHeader;
ListView rvNumbers;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_scrolling4_fragment, container, false);
llHeader = (LinearLayout) view.findViewById(R.id.ll_header);
rvNumbers = (ListView) view.findViewById(R.id.rv_numbers);
String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.list_item, numbers);
rvNumbers.setAdapter(itemArrayAdapter);
return view;
}
public class ItemArrayAdapter extends ArrayAdapter<String> {
String[] itemList;
private int listItemLayout;
public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int pos = position;
String item = getItem(pos);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout, parent, false);
viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
convertView.setTag(viewHolder); // view lookup cache stored in tag
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.item.setText(item);
return convertView;
}
class ViewHolder {
TextView item;
}
}
}
答案 0 :(得分:1)
根据这篇帖子:ScrollingViewBehavior for ListView,CoordinatorLayout
仅适用于RecyclerView
和NestedScrollView
,因此我建议您将ListView
更改为{{1} }}