리스트뷰의 더보기 기능 구현입니다.
추가적으로 커스텀으로 만들었습니다. 소스를 어디서 훔쳐온건지는...기억이 안나... 링크를 못달아둡니다.ㅠ
혹시라도 자료에 문제가 있거나, 삭제를 요청 혹은 출처를 아시는 분은 댓글을 달아주시기 바랍니다.
커스텀 리스트뷰 + footer를 이용한 더보기 구현 (샘플소스 다운로드 및 스크린샷은 소스 하단부에 있습니다.)
MainActivity.java
- package com.example.morelistview;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.LayoutInflater;
- 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.BaseAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnScrollListener
- {
- private ArrayList<MyItem> marItem;
- private MyListAdapter mMyAdapte;
- private ListView mListView;
- private MyItem items;
- // 스크롤 로딩
- private LayoutInflater mInflater;
- private boolean mLockListView;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- marItem = new ArrayList<MyItem>();
- mLockListView = true;
- // 푸터를 등록. setAdapter 이전에 해야함.
- mListView.addFooterView(mInflater.inflate(R.layout.listview_footer, null));
- // 스크롤 리스너 등록
- mListView.setOnScrollListener(this);
- mMyAdapte = new MyListAdapter(this, R.layout.custom_layout, marItem);
- mListView.setAdapter(mMyAdapte);
- // 임시 데이터 등록
- addItems(10);
- }
- // dialog
- {
- AlertDialog.Builder alt_bld = new AlertDialog.Builder(_activity);
- alt_bld.setMessage(msg).setCancelable(false)
- .setPositiveButton("확인", new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int id)
- {
- if(_slct)
- {
- Toast.makeText(MainActivity.this, "확인", 1).show();
- }
- else if(!_slct)
- {
- Toast.makeText(MainActivity.this, "취소", 1).show();
- }
- }
- })
- .setNegativeButton("취소", new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int id)
- {
- }
- });
- AlertDialog alert = alt_bld.create();
- alert.show();
- }
- // 리스트뷰 출력 항목
- class MyItem
- {
- {
- sCustId = _coustId;
- }
- String sCustId;
- }
- // 어댑터 클래스
- class MyListAdapter extends BaseAdapter
- {
- Context cContext;
- LayoutInflater lInflater;
- ArrayList<MyItem> alSrc;
- int layout;
- {
- cContext = _context;
- alSrc = _arrayList;
- layout = _layout;
- }
- @Override
- public int getCount()
- {
- return alSrc.size();
- }
- @Override
- {
- return alSrc.get(position).sCustId;
- }
- @Override
- public long getItemId(int position)
- {
- return position;
- }
- // 각 뷰의 항목 생성
- @Override
- {
- final int pos = position;
- if(convertView == null)
- {
- convertView = lInflater.inflate(layout, parent, false);
- }
- TextView tvCustId = (TextView)convertView.findViewById(R.id.tvCoustId);
- tvCustId.setText(alSrc.get(position).sCustId);
- btSending.setOnClickListener(new OnClickListener()
- {
- @Override
- {
- btnDialog(MainActivity.this, "전송", true, getCustId);
- }
- });
- // 삭제버튼
- deleteBtn.setOnClickListener(new OnClickListener()
- {
- @Override
- {
- btnDialog(MainActivity.this, "삭제", false, getCustId);
- }
- });
- return convertView;
- }
- }
- // 더미 아이템 추가
- private void addItems(final int size)
- {
- // 아이템을 추가하는 동안 중복 요청을 방지하기 위해 락을 걸어둡니다.
- mLockListView = true;
- {
- @Override
- public void run()
- {
- for(int i = 0 ; i < size ; i++)
- {
- items = new MyItem("more " + i);
- marItem.add(items);
- }
- // 모든 데이터를 로드하여 적용하였다면 어댑터에 알리고
- // 리스트뷰의 락을 해제합니다.
- mMyAdapte.notifyDataSetChanged();
- mLockListView = false;
- }
- };
- // 속도의 딜레이를 구현하기 위한 꼼수
- Handler handler = new Handler();
- handler.postDelayed(run, 1000);
- }
- {
- finish();
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
- {
- // 현재 가장 처음에 보이는 셀번호와 보여지는 셀번호를 더한값이
- // 전체의 숫자와 동일해지면 가장 아래로 스크롤 되었다고 가정합니다.
- int count = totalItemCount - visibleItemCount;
- if(firstVisibleItem >= count && totalItemCount != 0 && mLockListView == false)
- {
- Log.i("list", "Loading next items");
- addItems(10);
- }
- }
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState)
- {
- }
- }
activity_main.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" >
- <ListView
- android:id="@+id/moreList"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </ListView>
- </LinearLayout>
custom_layout.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="horizontal" >
- <TextView
- android:id="@+id/tvCoustId"
- android:layout_width="0dip"
- android:layout_height="wrap_content"
- android:layout_weight="6"
- android:text="Medium Text"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <Button
- android:id="@+id/sendBtn"
- android:layout_width="0dip"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="확인" />
- <Button
- android:id="@+id/deleteBtn"
- android:layout_width="0dip"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="취소" />
- </LinearLayout>
listview_footer.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center" >
- <ImageView
- android:id="@+id/iv_list_footer_loading"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/tv_list_footer"
- android:layout_width="wrap_content"
- android:layout_height="50dip"
- android:layout_marginLeft="10dip"
- android:gravity="center"
- android:text="More item ..."
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
샘플소스 다운로드
'DEVELOPMENT > ANDROID' 카테고리의 다른 글
[android] CheckBox(체크박스) 이미지 변경하기 (0) | 2014.04.21 |
---|---|
[android] SharedPreferences 사용하기 (0) | 2014.04.16 |
[android] Intent로 Web URL 연결 (0) | 2014.04.15 |
[android] ImageView에 외부(URL)에서 이미지 받아 삽입하기 (0) | 2014.04.09 |
[android] WebView 만들기 (0) | 2014.04.08 |