Android开发

Android分页中显示出下面翻页的导航栏的布局实例代码

本文主要是介绍Android分页中显示出下面翻页的导航栏的布局实例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

当页面条目过多的时候需要分页,要在布局中显示出分页的相关布局,使用android:layout_weight="11"

activity_call_safe.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <TextView 
 style="@style/TitleStyle"
   android:text="黑名单管理"
    />
  <FrameLayout 
    android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="11"
    >
    <LinearLayout 
      android:id="@+id/ll_pb"
      android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:visibility="invisible"
    android:orientation="vertical"
    >
    <ProgressBar
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      />
    <TextView 
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="玩命加载中..."
      />
    </LinearLayout>
   <include 
     android:id="@+id/list_view_callsafe"
     layout="@layout/list_view_callsafe"/>
  </FrameLayout>
  <LinearLayout 
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
    >
    <Button 
      android:onClick="prePage"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="上一页"
    android:layout_weight="1"
      />
 <Button 
      android:onClick="nextPage"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="下一页"
    android:layout_weight="1"
      /> 
 <Button 
      android:onClick="jump"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="跳转"
    android:layout_weight="1"
      /> 
     <EditText 
      android:id="@+id/et_page_number"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
       />
     <TextView 
      android:id="@+id/tv_page_number"
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0/12"
    android:textSize="20sp"
    android:layout_weight="1"
       />         
  </LinearLayout>
</LinearLayout>

逻辑文件,实现分页显示,页面的跳转,页数的显示,数据的删除同时更新ListView

package com.ldw.safe.Activity;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.ldw.safe.R;
import com.ldw.safe.adapter.MyBaseAdapter;
import com.ldw.safe.bean.BlackNumberInfo;
import com.ldw.safe.db.dao.BlackNumberDao;
public class CallSafeActivity extends Activity {
 private ListView list_view_callsafe;
 private List<BlackNumberInfo> blackNumberInfos;
 private LinearLayout ll_pb;
 private BlackNumberDao dao;
 private TextView tv_page_number;
 private int pageNumber;
 private int mPageSize = 20;//每一页显示多少数据
 private int mCurrentPageNumber = 0;//当前页面
 private int totalPage;
 private CallSafeAdapter adapter;
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_safe);
    initUi();
    initData();
 }
 //初始化数据
 private Handler handler = new Handler(){
 @Override
    public void handleMessage(Message msg) {
  //通过总数,和每页显示多少条显示有多少条数据
  tv_page_number.setText(mCurrentPageNumber + "/" + totalPage);
  //数据初始化结束,关闭家在全的展示
  ll_pb.setVisibility(View.INVISIBLE);
  adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this);
  //配置适配器
  list_view_callsafe.setAdapter(adapter);
 }
 };
 private EditText et_page_number;
 //初始化数据
 private void initData() {
 //防止阻塞主线程,在子线程中初始化数据(后期数据会很多)
 new Thread(){
  @Override
  public void run(){
  dao = new BlackNumberDao(CallSafeActivity.this);
  //获取到总条数和页数
  totalPage = dao.getTotalNumber() / mPageSize;
  //获取到表的所有数据
  //blackNumberInfos = dao.findAll();
  //分页显示数据
  blackNumberInfos = dao.findPar(mCurrentPageNumber, mPageSize);
  handler.sendEmptyMessage(0);
  }
 }.start();
 }
 //初始化UI
 private void initUi() {
 ll_pb = (LinearLayout) findViewById(R.id.ll_pb);
    //初始化的时候展示加载的圆圈
    ll_pb.setVisibility(View.VISIBLE);
 list_view_callsafe = (ListView) findViewById(R.id.list_view_callsafe);
 tv_page_number = (TextView) findViewById(R.id.tv_page_number);
 et_page_number = (EditText) findViewById(R.id.et_page_number);
 }
 //对代码进行优化,继承自定义MyBaseAdapter,MyBaseAdapter继承自BaseAdapter封装一些常用的方法
 private class CallSafeAdapter extends MyBaseAdapter<BlackNumberInfo>{
 private CallSafeAdapter(List lists, Context mContext){
  super(lists, mContext);
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  //谷歌推出的优化方案
  ViewHolder holder;
  if(convertView == null){
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  holder = new ViewHolder();
  holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
  convertView.setTag(holder);
  }else{
  holder = (ViewHolder) convertView.getTag();
  }
  holder.tv_number.setText(lists.get(position).getNumber());
  String mode = lists.get(position).getMode();
  if(mode.equals("1")){
  holder.tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  holder.tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  holder.tv_mode.setText("短信拦截");
  }
  final BlackNumberInfo info = lists.get(position);
  //监听删除数据按钮,删除数据
  holder.iv_delete.setOnClickListener(new OnClickListener(){
  @Override
  public void onClick(View v) {
   String number = info.getNumber();
   boolean result = dao.delete(number);
   if(result){
   Toast.makeText(CallSafeActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
   lists.remove(info);
   //更新ListView
   adapter.notifyDataSetChanged();
   }else{
   Toast.makeText(CallSafeActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
   }
  }
  });
  return convertView;
  /*
  //谷歌推出的优化方案
  ViewHolder holder;
  if(convertView == null){
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  holder = new ViewHolder();
  holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  convertView.setTag(holder);
  }else{
  holder = (ViewHolder) convertView.getTag();
  }
  holder.tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  holder.tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  holder.tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  holder.tv_mode.setText("短信拦截");
  }
  return convertView;
  */
  /*初步的优化
  convertView = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  TextView tv_number = (TextView) convertView.findViewById(R.id.tv_number);
  TextView tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
  tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  tv_mode.setText("短信拦截");
  }
  return convertView;
  */
  /*传统的方式
  View view = View.inflate(CallSafeActivity.this, R.layout.item_call_safe, null);
  TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
  TextView tv_mode = (TextView) view.findViewById(R.id.tv_mode);
  tv_number.setText(blackNumberInfos.get(position).getNumber());
  String mode = blackNumberInfos.get(position).getMode();
  if(mode.equals("1")){
  tv_mode.setText("电话和短信拦截");
  }else if(mode.equals("2")){
  tv_mode.setText("电话拦截");
  }else if(mode.equals("3")){
  tv_mode.setText("短信拦截");
  }
  return view;
  */
 } 
 }
 static class ViewHolder{
 TextView tv_number;
 TextView tv_mode;
 ImageView iv_delete;
 }
 /*
 * 上一页
 */
 public void prePage(View v){
 //页码不能超过范围
 if(mCurrentPageNumber <= 0){
  Toast.makeText(this, "这已经是第一页", Toast.LENGTH_SHORT).show();
  return;
 }
 mCurrentPageNumber --;
 initData();
 }
 /*
 * 下一页
 */
 public void nextPage(View v){
 //页码不能超过范围
 if(mCurrentPageNumber >= totalPage - 1){
  Toast.makeText(this, "这已经是最后一页", Toast.LENGTH_SHORT).show();
  return;
 }
 mCurrentPageNumber ++;
 initData();
 }
 /*
 * 跳转页面
 */
 public void jump(View v){
 String jump_page = et_page_number.getText().toString();
 if(TextUtils.isEmpty(jump_page)){
  Toast.makeText(this, "请输入正确的页码", Toast.LENGTH_SHORT).show();
 }else{
  //读取的字符串转换成int
  int pageNumber = Integer.parseInt(jump_page);
  if(pageNumber >=0 && pageNumber<= totalPage - 1){
  mCurrentPageNumber = pageNumber;
  initData();
  }else{
  Toast.makeText(this, "请输入正确的页码", Toast.LENGTH_SHORT).show();
  }
 }
 }
}

以上所述是小编给大家介绍的Android分页中显示出下面翻页的导航栏的布局实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

这篇关于Android分页中显示出下面翻页的导航栏的布局实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!