简体   繁体   中英

How to fix a layout on top of screen

I have a linear layout inside a scrollview and I want the the third element to stick to the top of the parent and remain visible while others are being scrolled up and when it's scrolled down, it returns back to it's original position. How can I handle such an even?

<ScrollView>
    <LinearLayout>
       <View1 />
       <View2 />
       <View3 />
       <View4 />
       <View5 />
        .
        .
        .
       <Viewn />
    <LinearLayout>
</ScrollView>

When scrolling, as View3 hits the head of the ScrollView, I want it to stay fix at the top and other elements to scroll under it. And on scroll down, when it returns back to its proper position.

What I want to do is define a "gone" View just before the ScrollView such that when the View reaches the head, I then make the View visible and then make it gone again on scroll down. I've no idea how to implement this.

<ScrollView>
<LinearLayout>
   <View1 />
   <View2 />
   <View3 />
   <View4 />
   <View5 />
    .
    .
    .
   <Viewn />
<LinearLayout>
</ScrollView>

 public class List1Adapter extends BaseAdapter implements AbsListView.OnScrollListener{

    private boolean isFix = false; 
    private Object mData[];
    private int firstVisibleItem = 0;

    public List1Adapter(Object data[]) {
        this.mData = data;
    }

    @Override
public int getCount() {
    if(null != mData) return mData.length + 1;
}


@Override
public Object getItem(int position) {
    return mData[position];
}

    @Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
        Object dataItem;
        if(isFix && firstVisibleItem >= topViewIndex && position == firstVisibleItem) {
           dataItem = getItem(topViewIndex); 
        }
        if(null == convertView) {
            convertView = new View();
        }

        //render with your data.
    }

     void  onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){

          if(visibleItemCount > 0 && firstVisibleItem >= topViewIndex) {
              isFix = true;
          }else {
              isFix = false;
          }             
          this.firstVisibleItem = firstVisibleItem;
     }

 }

could you do in this way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM