参考文章:
效果图:
实现步骤:
-
- 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内
- 将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll
- 给滚动的NestedScroolView设置app:layout_behavior="@String/appbar_scrolling_view_behavior"就大功告成了(记得根布局要是CoordinatorLayout)
import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * Created by Administrator on 2017/5/5. */public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ArrayAdapter;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private MyListView lv; private Listlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (MyListView) findViewById(R.id.lv); list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(i + ""); } ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); }}