作者:手机用户2502870367 | 来源:互联网 | 2023-07-29 17:36
1、可以采用开源框架StickyListHeadersListView,地址https:github.comemilsjolanderStickyListHeaders2、可以参考博客An
1、可以采用开源框架StickyListHeadersListView,地址 https://github.com/emilsjolander/StickyListHeaders
2、可以参考博客 Android 仿美团网,大众点评购买框悬浮效果之修改版 http://blog.csdn.net/xiaanming/article/details/17761431
3、针对listview的悬浮,主要原理是两个相同的布局显示或隐藏和listview的监听接口setOnScrollListener
效果一
效果二
下面是布局代码
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(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();
}
});
}
}