Android ListView实现按钮操作上一条下一条数据功能(ListView根据按钮点击上下滑动)
listVIew平滑滚动到列表最后一项的效果
listVIew平滑滚动到列表第一项的效果
listVIew实现按钮操作下一条数据滑动效果
listVIew实现按钮操作上一条数据滑动效果
listVIew实现监听滑动距离位置
一.效果图:
二.快速实现:
1.主函数:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.m1571.myapplication.DataSupport;
import com.example.m1571.myapplication.R;
import java.util.ArrayList;
import java.util.List;
/**
* ListView 使用按钮控制列表上下滑动
*/
public class MyListViewActivity extends AppCompatActivity {
ListView listview;
Button btDown,btUp,btBottom,btTop;
List<String> list = new ArrayList<>();
int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list);
listview = (ListView) findViewById(R.id.listview);
btDown = (Button) findViewById(R.id.bt_down);
btUp = (Button) findViewById(R.id.bt_up);
btTop = (Button) findViewById(R.id.bt_top);
btBottom = (Button) findViewById(R.id.bt_bottom);
listview.setAdapter(new ListAdapter());
btDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// listview.smoothScrollBy(-20, 10);//这里调整向下滑的速度,向下滑是负数
if(num==list.size()-1){
Toast.makeText(MyListViewActivity.this,"最后一条",Toast.LENGTH_SHORT).show();
return;
}
if(list.size()>0&&list.size()>=5&&num<=list.size()){
num++;
// listview.smoothScrollToPosition(num);
listview.setSelection(num);
}
}
});
btUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(num<1){
Toast.makeText(MyListViewActivity.this,"第一条",Toast.LENGTH_SHORT).show();
return;
}
if(list.size()>0&&num>0){
num--;
listview.setSelection(num);
}
}
});
//置顶
btTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listview.setSelection(0);
num = 0;
}
});
//到底
btBottom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = listview.getFirstVisiblePosition();
// View v = listview.getChildAt(0);
// int top = (v == null) ? 0 : v.getTop();
// //减慢ListView的滑动速度 原来的 1/10
listview.setFriction(ViewConfiguration.getScrollFriction() * 6);
listview.smoothScrollBy(600,20);
// listview.setSelectionFromTop(listview.getCount()-1, 3500);
//带有滑动动作
listview.smoothScrollToPosition(listview.getCount()-1);//平滑到某个item
// listview.setSelection(listview.getCount()-1);//滑动到某个item
// listview.smoothScrollBy(20, 10);//这里可以调整滑动的速度,向上滑就是正数。
// listview.scrollListBy(600);//向下滑动600px。向上是-600
// listview.smoothScrollBy(600, 2000);//向下平滑1000px,在2s内。向上是-600
num = listview.getCount()-1;//最后一条数据
}
});
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: // 没有滚动的时候
System.out.println("最后显示的listview的位置是--->>"
+ listview.getLastVisiblePosition());
if(listview.getLastVisiblePosition()>5){
num = listview.getLastVisiblePosition();
}else {
num = 0;
}
Toast.makeText(MyListViewActivity.this, "滑到了:"+listview.getLastVisiblePosition(),
Toast.LENGTH_SHORT).show();
System.out.println("最后显示的getLastVisiblePosition--->>"
+ listview.getLastVisiblePosition());
System.out.println("最后显示的getCount--->>"
+ listview.getCount());
System.out.println("最后显示的getCount-1--->>"
+ (listview.getCount()-1));
if (listview.getLastVisiblePosition() == (listview
.getCount()-1)) {
Toast.makeText(MyListViewActivity.this, "滑到listView底了",
Toast.LENGTH_SHORT).show();
}
break;
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
class ListAdapter extends BaseAdapter {
private List<String> data;
public ListAdapter() {
list.clear();
data = new DataSupport().getData();
list.addAll(data);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = LayoutInflater.from(MyListViewActivity.this).inflate(R.layout.item_view, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.tv_content);
holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.textView.setText(data.get(position));
holder.tvTime.setText((position+1)+".测试:2020年");
return convertView;
}
class Holder {
TextView textView;
TextView tvTime;
}
}
}
2.布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ListView
android:id="@+id/listview"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:id="@+id/bt_up"
android:text="上一位"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_down"
android:text="下一位"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_top"
android:text="顶部"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_bottom"
android:text="底部"
android:layout_width="90dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
3.适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/item_single_choice_selector"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/imageview"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginRight="10dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="@+id/imageview"
android:textSize="13sp"
android:textColor="@drawable/item_text_choice_selector"
android:gravity="center_vertical"
android:text="2016年8月8日 10:30:02"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageview"
android:layout_below="@+id/tv_time"
android:textSize="15sp"
android:textColor="@drawable/item_text_choice_selector"
android:text="(`・ω・´)ฅ铲屎官快来"/>
</RelativeLayout>
</FrameLayout>