facebook twitter hatena line email

Android/独自スクロール

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

スクロールを最下に移動したときの処理サンプル

  • src/ScrollTestActivity.java
public class ScrollTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.scroll_layout);
  }
}

  • res/layout/scroll_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
   <com.example.androidhelloworld.view.MyScrollView
       android:id="@+id/scrollview1"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       />
</LinearLayout>
  • src/view/MyScrollView.java
public class MyScrollView extends ScrollView {
 private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
 private LinearLayout mLayout;
 private Context mContext;
 public MyScrollView(Context context) {
   super(context);
   mContext = context;
   initView(context);
 }
 public MyScrollView(Context context, AttributeSet attrs) {
   super(context, attrs);
   mContext = context;
   initView(context);
 }
 private void initView(Context context) {
   mLayout = new LinearLayout(context);
   mLayout.setOrientation(LinearLayout.VERTICAL);
   for (int i = 1; i <= 10; i++) {
     ImageView imageView = new ImageView(context);
     imageView.setImageResource(R.drawable.ic_launcher);
     mLayout.addView(imageView, new LinearLayout.LayoutParams(WC, WC));
   }
       addView(mLayout, new LinearLayout.LayoutParams(WC, WC));
 }
   @Override
   protected void onScrollChanged(int x, int y, int oldx, int oldy) {
       super.onScrollChanged(x, y, oldx, oldy);
       View content = getChildAt(0);
       if (content == null) return;
       // 最下検知
       if (y + this.getHeight() >= content.getHeight()) {
         // 処理
     ImageView imageView = new ImageView(mContext);
     imageView.setImageResource(R.drawable.ic_launcher);
     mLayout.addView(imageView, new LinearLayout.LayoutParams(WC, WC));
       }
   }
}

参考

http://qiita.com/haratchatta/items/86aa8517a91fea1e772f

http://developer.android.com/reference/android/widget/ScrollView.html