Android開發實現上一頁,下一頁顯示內容
阿新 • • 發佈:2019-01-03
最近寫了個上一頁下一頁顯示Demo,感覺還是比較好實現!通過設定listview的每頁item顯示的個數,然後控制返回item的count實現點選按鈕上一頁下一頁效果.
一,佈局檔案(若是想實現滑動的上一頁和下一頁切換直接使用ViewPager巢狀listview)
<?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: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.com/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>