Android ListView实现按钮操作上一条下一条数据功能(ListView根据按钮点击上下滑动)

Android ListView实现按钮操作上一条下一条数据功能(ListView根据按钮点击上下滑动)_第1张图片

Android ListView实现按钮操作上一条下一条数据功能(ListView根据按钮点击上下滑动)

listVIew平滑滚动到列表最后一项的效果

listVIew平滑滚动到列表第一项的效果

listVIew实现按钮操作下一条数据滑动效果

listVIew实现按钮操作上一条数据滑动效果

listVIew实现监听滑动距离位置

一.效果图:

Android ListView实现按钮操作上一条下一条数据功能(ListView根据按钮点击上下滑动)_第2张图片

 

二.快速实现:

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 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 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.布局:




    

    

        

 3.适配器布局:




    

        

        

        
    

 

你可能感兴趣的:(RecyclerView列表类)