Android floating View does not disappear with ScrollView. androidscrollview

Source: Internet
Author: User

Android floating View does not disappear with ScrollView. androidscrollview
Reprinted, please indicate the path of the great ox from Wang Xiaoying

The title is abstract, first going up and down, and the project structure


Slide up (not to the end)

Slide to head

In turn, this process is reversed.

How to implement it? You will understand it.

The black line is ours.Screen
The Blue Line simulates a layout in the upper part of us. Now we make this dead warehouseSister
The red line simulates one of our microphones.LinearLayout
So what about green?
The green content is exactly the same as the red content. It is only visible to gone during initialization. do you understand it here?
We listen to ScrollView sliding. When the sliding distance is greaterSisterThe green part is displayed, while the green part is hidden. That's simple. Let's look at the code.

Custom rolling layout 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) ;}}/ *** listens to the rolling status of floating views * @ param topView top area view, that is, when the slide height of the ScrollView is greater than or equal to which view, the floatview * @ param flowView floating view is hidden, that is, the view to stay at the top */public void listenerFlowViewScrollState (View topView, view flowView) {mTopView = topView; mFlowView = flowView ;}}

MainActivity in the main view

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); // listens to the rolling status of floating view mScrollView. listenerFlowVie WScrollState (mImageView, mFlowLayout); // scroll the ScrollView to the starting position of mScrollView. scrollTo (0, 0);} OnItemClickListener onItemClickListener = new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {Toast. makeText (MainActivity. this, "menu" + (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 ("menu" + I);} return data;}/*** is used to solve the nested listview of ScrollView, the problem that only one row can be displayed in listview * @ param listView */public void setListViewHeightBasedOnChildren (ListView listView) {// obtain the Adapter ListAdapter listAdapter = ListView corresponding to listView. getAdapter (); if (listAdapter = null) {return;} int totalHeight = 0; for (int I = 0, len = listAdapter. getCount (); I <len; I ++) {// listAdapter. getCount () returns the number of data items. View listItem = listAdapter. getView (I, null, listView); // calculate the width and height of the subitem View listItem. measure (0, 0); // calculate the total height of all subitems totalHeight + = listItem. getMeasuredHeight ();} ViewGroup. layoutParams params = listView. getLayoutParams (); params. height = totalHeight + (listView. getDividerHeight () * (listAdapter. getCount ()-1); // listView. getDividerHeight () obtains the height occupied by separators between subitems. // params. the height finally shows the required height for the entire ListView. setLayoutParams (params );}}

Main Layout

<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 = "spanking mic" android: textColor = "# ffffff"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "beat his ass" 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 = "spanking mic" android: textColor = "# ffffff"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "beat his ass" android: textColor = "# ffffff"/> </LinearLayout> </RelativeLayout>

Adapter 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;    }}

The implementation of things is not complicated as long as you think of how to do it. The source code will not be uploaded today, because there is no resource file or anything, and these types of copy and paste can be used, making it a pleasant weekend.

For some content, refer to the Internet. If there are duplicates, please forgive me

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.