首页 > 代码库 > Dynamic load data by scrollview

Dynamic load data by scrollview

The  demo generate from 北京尚学堂

package com.example.scrollview;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.LinearLayout;import android.widget.ScrollView;import android.widget.TextView;import android.widget.Toast;import android.os.Build;public class MainActivity extends ActionBarActivity {    /**     * when the view too many to show ,just display a part      * when user slide to bottom we can display next part      * when it slide a piece of the view we regard it slide to bottom and the we can display next part      * */    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (savedInstanceState == null) {            getSupportFragmentManager().beginTransaction()                    .add(R.id.container, new PlaceholderFragment()).commit();        }                ScrollView sv  = (ScrollView) findViewById(R.id.scrollview);        sv.setOnTouchListener(new OnTouchListener() {                        @Override            public boolean onTouch(View arg0, MotionEvent arg1) {                if(arg1.getAction() == MotionEvent.ACTION_MOVE){                    int scrollY = (int) arg0.getScrollY();                    int height = arg0.getHeight() ;                    LinearLayout ll  = (LinearLayout) findViewById(R.id.linear01);                                        int llheigh = ll.getMeasuredHeight();                    System.out.println("scrollY:"+scrollY+"getHeight:"+height+"getMeasuredHeight:"+llheigh);                    if((scrollY+height)==llheigh){                        Toast.makeText(MainActivity.this, "Has came to bottom", Toast.LENGTH_SHORT).show();                        for (int i = 0; i < 20; i++) {                                                        TextView tv  = new TextView(MainActivity.this);                            tv.setTextSize(50);                            tv.setText("TextView1");                            ll.addView(tv);                        }                    }                }                return false;            }        });                    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container,                    false);            return rootView;        }    }}