悬浮头部
Posted dingpeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了悬浮头部相关的知识,希望对你有一定的参考价值。
1.Activity代码:
public class Demo extends Activity { private LinearLayout invis; private LinearLayout invis2; private TextView mTVSuspension; private PullToRefreshListView mPullToRefreshListView; //一个可以下拉刷新的listView对象 private ArrayList<String> list = new ArrayList<String>(); //数据源 private ListView listView; //普通的listView对象 private View header1; private View header2; @Override protected void onCreate ( Bundle savedInstanceState ) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); invis = (LinearLayout) findViewById(R.id.invis); mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.refreshListView); for (int i = 0; i < 50; i++) { list.add("data-----" + i); } listView = mPullToRefreshListView.getRefreshableView(); mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); header1 = View.inflate(this, R.layout.header1, null); listView.addHeaderView(header1); header2 = View.inflate(this, R.layout.header2, null); invis2 = (LinearLayout) header2.findViewById(R.id.invis2); mTVSuspension = (TextView) header2.findViewById(R.id.tv_suspension); listView.addHeaderView(header2); //添加头部,ListView条目中的悬浮部分 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick ( AdapterView<?> parent, View view, int position, long id ) { Toast.makeText(Demo.this, "第" + position + "item", Toast.LENGTH_SHORT).show(); } }); //实例化适配器对象 listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged ( AbsListView view, int scrollState ) { } //方法onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 内参数的介绍: // (1)AbsListView view 所监听的listView对象 //(2)int firstVisibleItem 能看见的第一个item的位置 //(3)int visibleItemCount 能看见的item的数量 //(4)int totalItemCount 所有item的总数 @Override public void onScroll ( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) { if (firstVisibleItem >= 2) { //第二个条目内容已经不显示 if (mTVSuspension.getParent() != invis) { invis2.removeView(mTVSuspension); invis.addView(mTVSuspension); } } else { if (mTVSuspension.getParent() != invis2) { invis.removeView(mTVSuspension); invis2.addView(mTVSuspension); } } } }); } }
2.demo布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/refreshListView" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/invis" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical"> </LinearLayout> </RelativeLayout>
3.header1布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/texts" android:layout_width="match_parent" android:layout_height="130dp" android:background="#000" android:gravity="center" android:text="标题" android:textColor="#ffffff" /> </RelativeLayout>
4.header2布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/invis2" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_suspension" android:background="#ccedc7" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="悬浮部分" /> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:background="@mipmap/ic_launcher"/> </LinearLayout>
以上是关于悬浮头部的主要内容,如果未能解决你的问题,请参考以下文章