Android ListView 데이터 의 분할 표시 기능

11032 단어 androidlistviw
Android ListView 데이터 의 분할 표시
레이아웃 파일 activitycall_safe2.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" >
  <RelativeLayout 
    android:layout_width="match_parent"
   android:layout_height="wrap_content"
    >
    <TextView 
 style="@style/TitleStyle"
   android:text="     "
    />
    <Button 
      android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="  "
      />
  </RelativeLayout>
  <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>    ,         ,  ListView     ,          
CasllSafeActivity.java

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.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
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 CallSafeAdapter adapter;
 private int totalNumber;//    
 private int mStartIndex = 0;//       
 private int maxCount = 20;//        
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_safe2);
    initUi();
    initData();
 }
 //     
 private Handler handler = new Handler(){
 @Override
    public void handleMessage(Message msg) {
  //       ,        
  ll_pb.setVisibility(View.INVISIBLE);
  //         
  if(adapter == null){
  adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this);
  //     
  list_view_callsafe.setAdapter(adapter);
  }else{
   adapter.notifyDataSetChanged();
  }
 }
 };
 private EditText et_page_number;
 //     
 private void initData() {
 //       ,          (       )
 new Thread(){
  @Override
  public void run(){
  dao = new BlackNumberDao(CallSafeActivity.this);
   //        
     totalNumber = dao.getTotalNumber();
  //      
     if(blackNumberInfos == null){
      //     
      blackNumberInfos = dao.findPar2(mStartIndex, maxCount);
     }else{
      //      。   blackNumberInfos    。        
      blackNumberInfos.addAll(dao.findPar2(mStartIndex, maxCount));
     }
  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);
 //  listView      
 list_view_callsafe.setOnScrollListener(new OnScrollListener(){
      //           
      /*
       * @param scrollState        
       *AbsListView.OnScrollListener.SCROLL_STATE_IDLE     
       *AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL           
       *AbsListView.OnScrollListener.SCROLL_STATE_FLING   
       */
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
          //              
          int lastVisiblePosition = list_view_callsafe.getLastVisiblePosition();
          System.out.println("lastVisiblePosition==========" + lastVisiblePosition);
            if(lastVisiblePosition == blackNumberInfos.size() - 1){//                  -1
              //        。            
              mStartIndex += maxCount;
              if (mStartIndex >= totalNumber) {
                Toast.makeText(getApplicationContext(),
                    "        。", Toast.LENGTH_SHORT).show();
                return;
              }
              initData();
            }
          break;
      }
  }
  //listview          
      //    。                 
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem,
   int visibleItemCount, int totalItemCount) {
  // TODO Auto-generated method stub
  }
 });
 }
 //       ,     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;
 }
}
위 에서 말 한 것 은 소 편 이 여러분 에 게 소개 한 Android ListView 데이터 의 분할 표시 기능 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기