最近写了个上一页下一页显示Demo,感觉还是比较好实现!通过设置listview的每页item显示的个数,然后控制返回item的count实现点击按钮上一页下一页效果.
一,布局文件(若是想实现滑动的上一页和下一页切换直接使用ViewPager嵌套listview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:tools="http://schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tstest.www.pagershow.MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="为你搜索到如下结果:"
android:textSize="18sp" />
<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2/4"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/pre_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一页" />
<Button
android:id="@+id/nex_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="下一页" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二,实现代码,这个demo做的是当在第一页时隐藏上一页按钮,最后一页时隐藏下一页按钮.中间上一页按钮和下一页按钮皆显示.
package com.tstest.www.pagershow;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private List<NearlySearchBean> list=new ArrayList<>();
private String name[]={"gaga","嘎嘎","他们吗","发密码","摩尔玛","陪我"};
private ListViewAdapter adapter;
private Button prePager, nexPager;
private TextView num;
private NearlySearchBean nearlySearchBean;
int index=0;
int pagerCount=4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prePager = (Button) findViewById(R.id.pre_pager);
nexPager = (Button) findViewById(R.id.nex_pager);
num = (TextView) findViewById(R.id.num);
ListView listView = (ListView) findViewById(R.id.lv);
for (int i = 0; i < name.length; i++) {
nearlySearchBean = new NearlySearchBean();
nearlySearchBean.setName(name[i]);
list.add(nearlySearchBean);
}
prePager.setOnClickListener(this);
nexPager.setOnClickListener(this);
adapter = new ListViewAdapter();
listView.setAdapter(adapter);
prePager.setVisibility(View.INVISIBLE);
num.setText(index+1+"/"+(int) Math.ceil((double)list.size()/pagerCount));
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.pre_pager:
prePager();
break;
case R.id.nex_pager:
nexPager();
break;
}
}
private void nexPager() {
index++;
System.out.println(index+"nexPager");
num.setText(index+1+"/"+(int) Math.ceil((double)list.size()/pagerCount));
adapter .notifyDataSetChanged();
//隐藏上一个或下一个按钮
checkButton();
}
private void checkButton() {
if (index<=0){
prePager.setVisibility(View.INVISIBLE);
nexPager.setVisibility(View.VISIBLE);
}else if (list.size()-index*pagerCount<=pagerCount){ //数据总数减每页数当小于每页可显示的数字时既是最后一页
nexPager.setVisibility(View.INVISIBLE);
prePager.setVisibility(View.VISIBLE);
}
}
private void prePager() {
index--;
System.out.println(index+"prePager");
adapter.notifyDataSetChanged();
num.setText(index+1+"/"+(int) Math.ceil((double)list.size()/pagerCount));
//隐藏上一个或下一个按钮
checkButton();
}
class ListViewAdapter extends BaseAdapter{
@Override
public int getCount() {
//数据大于页数*每页个数,显示默认数字,小于时显示剩余的
int current = index * pagerCount;
return list.size()-current<pagerCount?list.size()-current:pagerCount;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.pager_item,viewGroup,false);
TextView name = (TextView) convertView.findViewById(R.id.name);
int pos = position + index * pagerCount;
NearlySearchBean nearlySearchBean = list.get(pos);
name.setText(nearlySearchBean.getName());
return convertView;
}
}
}
三,item布局和Javabean,Javabean可以根据自己显示的内容增减
public class NearlySearchBean {
private String name;
private String instance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInstance() {
return instance;
}
public void setInstance(String instance) {
this.instance = instance;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:text="肯德基"
android:textSize="24sp"
android:textColor="@color/colorAccent"
android:layout_height="wrap_content" />
</RelativeLayout>
更多推荐
Android开发实现上一页,下一页显示内容
发布评论