ListView实现滚动动画
16lz
2021-01-26
main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_scroll_up" android:text="向上滚动" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_scroll_down" android:text="向下滚动" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_scroll_stop" android:text="停止滚动" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000"> </ListView></LinearLayout>
item.xml:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="40dp"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="40dp"/></LinearLayout>
Adapter.java:
package cn.guet.levide;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class Adapter extends BaseAdapter { private Context context; public Adapter(Context context) { this.context = context; } @Override public int getCount() { return 40; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } class ViewHolder { TextView tv; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null) { holder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.item, null); holder.tv = (TextView)convertView.findViewById(R.id.tv); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.tv.setText((position + 1) + ". " + "hello friends."); return convertView; }}
MainActivity.java:
package cn.guet.levide;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ListView;public class MainActivity extends Activity implements OnClickListener{ private Button btn_up,btn_down,btn_stop; private ListView listview; private Adapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findBy(); init(); } private void init() { btn_up.setOnClickListener(this); btn_down.setOnClickListener(this); btn_stop.setOnClickListener(this); adapter = new Adapter(this); listview.setAdapter(adapter); } private void findBy() { btn_up = (Button)findViewById(R.id.btn_scroll_up); btn_down = (Button)findViewById(R.id.btn_scroll_down); btn_stop = (Button)findViewById(R.id.btn_scroll_stop); listview = (ListView)findViewById(R.id.listview); } @Override public void onClick(View v) { switch(v.getId()) { case R.id.btn_scroll_down: listScrollDown(); break; case R.id.btn_scroll_up: listScrollUp(); break; case R.id.btn_scroll_stop: listScrollOff(); break; } } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { handler.removeCallbacks(run_scroll_down); handler.removeCallbacks(run_scroll_up); } }; public void listScrollUp() { listScrollOff(); handler.postDelayed(run_scroll_up, 0); } public void listScrollDown() { listScrollOff(); handler.postDelayed(run_scroll_down, 0); } public void listScrollOff() { handler.removeCallbacks(run_scroll_down); handler.removeCallbacks(run_scroll_up); } Runnable run_scroll_up = new Runnable() { @Override public void run() { listview.smoothScrollBy(1, 10); handler.postDelayed(run_scroll_up, 10); } }; Runnable run_scroll_down = new Runnable() { @Override public void run() { listview.smoothScrollBy(-1, 10); handler.postDelayed(run_scroll_down, 10); } };}
更多相关文章
- android 搞定标题随scrollview滑动变色
- Android布局 屏幕滚动方法 ScrollView
- 仿Google应用动态隐藏显示状态栏
- [控件]SeekBar拖动条
- ScrollView 滚动到最后/前/上/下
- android 可以控制速度的跑马灯
- android EditText 自动滚动条 显示最新内容
- android srcollview按钮顶部停留
- Android自动滚动 轮播循环的ViewPager