Android ListView, scroll to specified position

How to scroll Android ListView to top, bottom or specified position.

package com.example.androidlistviewscrolling;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 String[] months = { "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December" };

 ListView listView;
 int toPosition = months.length;
 
 Button btnScrollTop, btnScrollBottom, btnScrollTo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listview);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, months);
  listView.setAdapter(adapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    toPosition = position;
    btnScrollTo.setText("Scroll to position " + toPosition);
   }});
  
  btnScrollTop = (Button)findViewById(R.id.scrolltop);
  btnScrollTop.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(0);
   }});
  
  btnScrollBottom = (Button)findViewById(R.id.scrollbottom);
  btnScrollBottom.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(listView.getCount()-1);
   }});
  
  btnScrollTo = (Button)findViewById(R.id.scrollto);
  btnScrollTo.setText("Scroll to position " + toPosition);
  btnScrollTo.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    listView.smoothScrollToPosition(toPosition);
   }});
 }

}

<LinearLayout 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:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidlistviewscrolling.MainActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="28dp"
            android:text="lesapplication.blogspot.com" />
        
        <Button
            android:id="@+id/scrolltop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scroll to Top" />
        <Button
            android:id="@+id/scrollbottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scroll to Bottom" />
        <Button
            android:id="@+id/scrollto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

0 Response to "Android ListView, scroll to specified position"

Posting Komentar