实现的效果:
代码:IndexMainActivity.java
package com.example.callphone;
import java.util.ArrayList;
import java.util.List;
import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.callphone.adapter.MyPageAdapter;
import com.example.callphone.utils.Globals;
public class IndexMainActivity extends Activity {
// 声明一个ViewPager专用的Adapter
private MyPageAdapter pageAdapter;
private ArrayAdapter
private ListView list;
private ListView list2;
private ViewPager viewpage;
//多个页面串起来
private List
//实现的第一页面的数据
private List
private List
//实现的第二页面的数据
private List
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Globals.init(this);
setContentView(R.layout.activity_index_main);
viewpage = (ViewPager) this.findViewById(R.id.viewPager);
//设置最大显示的页数限制
viewpage.setOffscreenPageLimit(3);
LayoutInflater layoutInflater = LayoutInflater.from(this);
//分别对两个页面进行实例化
View view1 = layoutInflater.inflate(R.layout.page_01, null);
list = (ListView) view1.findViewById(R.id.list01);
//准备数据
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,
null, null, null, null);
//循环将数据加入到集合里
c.moveToFirst();
while(!c.isAfterLast()){
StringBuilder builder = new StringBuilder();
//取得联系人的姓名
String name = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));
//取得联系电话
//需要先取得出这个联系人的id
String contactsId = c.getString(c.getColumnIndex(Contacts._ID));
//由于联系电话保存在其他的表中,因此这里要进行关联查询
Cursor c2 = getContentResolver().query(Phone.CONTENT_URI,
null,Phone.CONTACT_ID + " = ?",new String[]{contactsId},null);
String num = null;
builder.append(name);
builder.append("-->");
c2.moveToFirst();
while(!c2.isAfterLast()){
num = c2.getString(c2.getColumnIndex(Phone.NUMBER));
builder.append(num + ",");
c2.moveToNext();
}
c2.close();
allPhoneNums_page01.add(num);
c.moveToNext();
allValues_page01.add(builder.toString());
}
c.close();
phoneAdapter = new ArrayAdapter
android.R.layout.simple_list_item_1,allValues_page01);
list.setAdapter(phoneAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
//取得要拨的电话号码
String phoneNum = allPhoneNums_page01.get(arg2);
//使用Intent来切换到打电话的界面上,并将要播的电话传进去,
Intent in = new Intent();
//设置现在要切换的功能
in.setAction(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:" + phoneNum));
startActivity(in);
}
});
View view2 = layoutInflater.inflate(R.layout.page_02, null);
list = (ListView)view2.findViewById(R.id.list02);
//准备数据
Cursor c_page02 = getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null,null,null);
//循环将数据加入到集合里
c_page02.moveToFirst();
while(!c_page02.isAfterLast()){
StringBuilder builder = new StringBuilder();
//取得姓名
String name = c_page02.getString(c_page02.getColumnIndex(Calls.CACHED_NAME));
if(name == null || name.equals("")){
name = "未知";
}
String num2 = null;
builder.append(name);
builder.append("-->");
num2 = c_page02.getString(c_page02.getColumnIndex(Calls.NUMBER));
builder.append(num2);
builder.append("-->");
int type = c_page02.getInt(c_page02.getColumnIndex(Calls.TYPE));
if(type == Calls.INCOMING_TYPE){
builder.append("已接电话");
}else if(type == Calls.MISSED_TYPE){
builder.append("未接来电");
}else if(type == Calls.OUTGOING_TYPE){
builder.append("拨出电话");
}
allPhoneNums_page02.add(num2);
c_page02.moveToNext();
allValues_page02.add(builder.toString());
}
c_page02.close();
phoneAdapter = new ArrayAdapter
android.R.layout.simple_expandable_list_item_1,allValues_page02);
list.setAdapter(phoneAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
//取得要拨电话号码
String phoneNum = allPhoneNums_page02.get(arg2);
//是用Intent来切换到打电话的页面上,并将要拨的电话传进去
Intent in = new Intent();
//设置现在要切换的功能
in.setAction(Intent.ACTION_CALL);
//设置要拨打的电话,格式必须为 tel:电话
in.setData(Uri.parse("tel:" + phoneNum));
startActivity(in);
}
});
//将两个单独的页面加载到一起
views = new ArrayList
views.add(view1);
views.add(view2);
// views.add(LayoutInflater.from(this).inflate(R.layout.page_02,
// null));
viewpage.setAdapter(new MyPageAdapter(views));
}
}
adapter:MyPageAdapter.java
package com.example.callphone.adapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyPageAdapter extends PagerAdapter {
private List
public MyPageAdapter(List
super();
this.allViews = allViews;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//将组建从显示容器中删除
container.removeViewAt(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//将组件从集合中取得,并加入到显示容器中
container.addView(allViews.get(position));
return allViews.get(position);
}
@Override
public int getCount() {
return allViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
公共类:Gloabls.java
package com.example.callphone.utils;
import android.app.Activity;
public class Globals {
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
public static void init(Activity a){
SCREEN_WIDTH = a.getWindowManager().getDefaultDisplay().getWidth();
SCREEN_HEIGHT = a.getWindowManager().getDefaultDisplay().getHeight();
}
}
布局:Activity_index_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:background="#000000"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
布局二:page_header.xml
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical">
android:layout_height="35dp"
android:background="@drawable/head_bg"
android:orientation="horizontal">
android:layout_weight="0.7"
android:layout_height="wrap_content"
/>
android:layout_weight="1"
android:layout_height="match_parent"
android:text="联系人"
android:textSize="20sp"
android:gravity="center_vertical"
android:textColor="#454545"
/>
android:layout_weight="0.5"
android:layout_height="wrap_content"
/>
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="通话记录"
android:textSize="20sp"
android:textColor="#454545"
/>
android:layout_weight="0.5"
android:layout_height="wrap_content"
/>
android:layout_height="5dp"
android:background="@drawable/head_btn"
>
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#ffffff"
/>
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
布局三:page_01.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"/>
布局五:page_02.xml
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:scrollbars="none"
>