码迷,mamicode.com
首页 > 其他好文 > 详细

Dynamic load data by scrollview

时间:2014-08-02 20:52:34      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   os   io   for   art   

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

}

 

Dynamic load data by scrollview,布布扣,bubuko.com

Dynamic load data by scrollview

标签:android   style   blog   color   os   io   for   art   

原文地址:http://www.cnblogs.com/oaks/p/3887400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!