Skip to content

Instantly share code, notes, and snippets.

@takeshiyako2
Last active February 1, 2017 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takeshiyako2/854f98ef7000492ee5cf to your computer and use it in GitHub Desktop.
Save takeshiyako2/854f98ef7000492ee5cf to your computer and use it in GitHub Desktop.
Android List AutoScroll Sample
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
package app.sample.listautoscroll;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
// 1ページ辺りの項目数
Integer per_page = 20;
// フッターのプログレスバー(クルクル)
View mFooter;
// 予報表示用リストビューのアダプター
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// リスト用のアダプターを準備
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
// アダプターにアイテムを追加します
for (int i = 0; i < per_page; i++) {
adapter.add("リストビュー:" + i);
}
// リストビューへ紐付け
ListView listview = (ListView)findViewById(R.id.listView);
// リストビューにアダプターを設定します
listview.setAdapter(adapter);
// リストビューにフッターを追加
listview.addFooterView(getFooter());
// スクロールのリスナー
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
// スクロール中の処理
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// 最初とスクロール完了したとき
if ((totalItemCount - visibleItemCount) == firstVisibleItem) {
// アイテムの数 フッター分の1を引く
Integer ItemCount = totalItemCount - 1;
// アダプターにアイテムを追加します
for (int i = ItemCount; i < (ItemCount + per_page); i++) {
adapter.add("リストビュー:" + i);
}
}
}
// ListViewがスクロール中かどうか状態を返すメソッドです
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}
});
}
private View getFooter() {
if (mFooter == null) {
mFooter = getLayoutInflater().inflate(R.layout.listview_footer, null);
}
return mFooter;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment