1、可以采用开源框架StickyListHeadersListView,地址 https://github.com/emilsjolander/StickyListHeaders
2、可以参考博客 Android 仿美团网,大众点评购买框悬浮效果之修改版 http://blog.csdn.net/xiaanming/article/details/17761431
3、针对listview的悬浮,主要原理是两个相同的布局显示或隐藏和listview的监听接口setOnScrollListener
效果一
效果二
下面是布局代码
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="#f2f2f2" android:dividerHeight="1dp" android:drawSelectorOnTop="false" android:fadingEdge="none" /> <LinearLayout android:id="@+id/ll_hidden_layout" android:layout_width="fill_parent" android:layout_height="50dp" android:background="#ccedc7" android:orientation="horizontal" android:visibility="gone" > <TextView android:id="@+id/tv_hidden" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="悬浮部分" /> </LinearLayout> </FrameLayout></span>
<span style="font-size:14px;">public class MainActivity extends Activity { private ListView lv; private LinearLayout ll_hidden_layout; private String[] strs; private TextView tv_hidden; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sticky); ll_hidden_layout = (LinearLayout) findViewById(R.id.ll_hidden_layout); tv_hidden = (TextView) findViewById(R.id.tv_hidden); strs = new String[20]; for (int i = 0; i < 20; i++) { strs[i] = "data-----" + (i+2); } lv = (ListView) findViewById(R.id.lv); lv.addHeaderView(View.inflate(this, R.layout.stick_header, null));// 添加头部 // lv.addHeaderView(View.inflate(this, R.layout.stick_action, null));// ListView条目中的悬浮部分 // 添加到头部 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv_item, strs)); lv.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem >= 1) { ll_hidden_layout.setVisibility(View.VISIBLE); lv.setPadding(0, ll_hidden_layout.getMeasuredHeight(), 0, 0); tv_hidden.setText("" + strs[firstVisibleItem-1]); } else{ lv.setPadding(0, 0, 0, 0); ll_hidden_layout.setVisibility(View.GONE); } } }); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "pos:" + position, 0) .show(); } }); } } </span>