android 浮動的View不會跟隨ScrollView消失而消失,androidscrollview
轉載請註明出處王亟亟的大牛之路
標題比較抽象,先上下以及項目結構
向上滑(沒到底)
滑到頭
反過來就是這個流程再反一反。
如何??看下你就懂了
黑色的線是我們的螢幕
藍色的線類比的是我們上部的一個布局,我們現在使這個死庫水的妹子
紅色的線類比的是我們的那一個打麥麥屁股的一個LinearLayout
那麼綠色呢?
綠色其實跟紅色的內容是一模一樣只是在初始化的時候受否可見為gone,說到這裡大家理解了嗎?
我們監聽ScrollView的滑動當向上滑動的距離大於妹子時,我們的綠色部分就顯現出來,反之則隱藏。就是這麼簡單,那我們來看下代碼
自訂的滾動布局CustomScrollView
public class CustomScrollView extends ScrollView { View mTopView; View mFlowView; public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if(mTopView != null && mFlowView != null) { if(t >= mTopView.getHeight()) { mFlowView.setVisibility(View.VISIBLE); } else { mFlowView.setVisibility(View.GONE); } } } /** * 監聽浮動view的滾動狀態 * @param topView 頂部地區view,即當ScrollView滑動的高度要大於等於哪個view的時候隱藏floatview * @param flowView 浮動view,即要哪個view停留在頂部 */ public void listenerFlowViewScrollState(View topView, View flowView) { mTopView = topView; mFlowView = flowView; }}
主視圖MainActivity
public class MainActivity extends Activity { private CustomScrollView mScrollView; private ImageView mImageView; private LinearLayout mFlowLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mScrollView = (CustomScrollView) findViewById(R.id.scroll_view); mImageView = (ImageView) findViewById(R.id.image_view); mFlowLayout = (LinearLayout) findViewById(R.id.flow_llay); ListView listview = (ListView) findViewById(R.id.list_view); listview.setAdapter(new ListViewDataAdapter(getData(), this)); listview.setFocusable(false); listview.setOnItemClickListener(onItemClickListener); setListViewHeightBasedOnChildren(listview); //監聽浮動view的滾動狀態 mScrollView.listenerFlowViewScrollState(mImageView, mFlowLayout); //將ScrollView滾動到起始位置 mScrollView.scrollTo(0, 0); } OnItemClickListener onItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "菜單"+(arg2+1), Toast.LENGTH_SHORT).show(); } }; private ArrayList<String> getData() { ArrayList<String> data = new ArrayList<String>(); for(int i=1; i<30; i++) { data.add("菜單"+i); } return data; } /** * 用於解決ScrollView嵌套listview時,出現listview只能顯示一行的問題 * @param listView */ public void setListViewHeightBasedOnChildren(ListView listView) { // 擷取ListView對應的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回資料項目的數目 View listItem = listAdapter.getView(i, null, listView); // 計運算元項View 的寬高 listItem.measure(0, 0); // 統計所有子項的總高度 totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1)); // listView.getDividerHeight()擷取子項間分隔字元佔用的高度 // params.height最後得到整個ListView完整顯示需要的高度 listView.setLayoutParams(params); } }
主布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.jimstin.topfloatdemo.view.CustomScrollView android:id="@+id/scroll_view" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="120dp" android:background="@drawable/pic01"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#4169E1"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="10pt" android:text="打屁股麥麥" android:textColor="#ffffff"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打他屁股" android:textColor="#ffffff"/> </LinearLayout> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </ListView> </LinearLayout> </com.jimstin.topfloatdemo.view.CustomScrollView> <LinearLayout android:id="@+id/flow_llay" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#4169E1" android:visibility="gone"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="10pt" android:text="打屁股麥麥" android:textColor="#ffffff"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打他屁股" android:textColor="#ffffff"/> </LinearLayout></RelativeLayout>
適配器ListViewDataAdapter
public class ListViewDataAdapter extends BaseAdapter { private ArrayList<String> mData = new ArrayList<String>(); private Context mContext; public ListViewDataAdapter(ArrayList<String> mData, Context mContext) { super(); this.mData = mData; this.mContext = mContext; } @Override public int getCount() { // TODO Auto-generated method stub return mData.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return mData.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View convertView, ViewGroup arg2) { // TODO Auto-generated method stub Holder holder = null; if(convertView == null) { LayoutInflater inflater = LayoutInflater.from(mContext); convertView = inflater.inflate(R.layout.layout_list_view_item, null); holder = new Holder(); holder.avator_iv = (ImageView) convertView.findViewById(R.id.avator); holder.name_tv = (TextView) convertView.findViewById(R.id.item_tv); convertView.setTag(holder); } holder = (Holder) convertView.getTag(); holder.avator_iv.setImageResource(R.drawable.pic02); holder.name_tv.setText(mData.get(arg0)); return convertView; } class Holder { ImageView avator_iv; TextView name_tv; }}
東西就這麼點,只要想到怎麼做的話實現其實並不複雜,源碼今天就不傳了,因為並沒有什麼資源檔什麼的,這幾個類 複製黏貼就能用了,周末愉快
部分內容參考互連網,如有重複請見諒
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。