添加TabLayout依賴
implementation 'com.android.support:design:28.0.0'
清單文件權(quán)限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
點(diǎn)擊聯(lián)系人打電話代碼
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+mapList.get(i).get("number").toString()));
if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
startActivity(intent);
}
});
Activity代碼
public class MainActivity extends AppCompatActivity {
private List<Fragment> fragmentList=new ArrayList<>();
private List<String> stringList=new ArrayList<>();
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout=findViewById(R.id.TabLayout);
viewPager=findViewById(R.id.ViewPager);
stringList.add("聯(lián)系人");
stringList.add("短信");
fragmentList.add(new mf1());
fragmentList.add(new mf2());
mytabadapter mytabadapter = new mytabadapter(getSupportFragmentManager(),fragmentList,stringList);
viewPager.setAdapter(mytabadapter);
tabLayout.setupWithViewPager(viewPager);
}
}
TabLayout適配器代碼
public class mytabadapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> stringList;
public mytabadapter(FragmentManager fm,List<Fragment> fragmentList,List<String> stringList) {
super(fm);
this.fragmentList=fragmentList;
this.stringList=stringList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return stringList.get(position);
}
}
Activity布局代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/TabLayout"
android:layout_width="match_parent"
android:layout_height="62dp">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/TabLayout"
>
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
Fragment1聯(lián)系人代碼
public class mf1 extends Fragment {
List<Map<String,Object>> list=new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = getLayoutInflater().inflate(R.layout.f1, null);
final ListView ListView = view.findViewById(R.id.ListView_phone);
Button Button = view.findViewById(R.id.Button_phone);
String str="android.permission.READ_CONTACTS";
getAccess(str);
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentResolver resolver = getActivity().getContentResolver();
Cursor cursor=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Map<String,Object> map=new HashMap<>();
map.put("namenumber",name+number);
list.add(map);
}
myitemadapter myitemadapter = new myitemadapter(list,getActivity());
ListView.setAdapter(myitemadapter);
}
});
return view;
}
private void getAccess(String str) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{"android.permission.READ_SMS",
"android.permission.READ_CONTACTS"}, 1);
}
}
Fragment1聯(lián)系人布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<Button
android:id="@+id/Button_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取聯(lián)系人"
/>
<ListView
android:id="@+id/ListView_phone"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Fragment1聯(lián)系人適配器代碼
public class myitemadapter extends BaseAdapter {
private List<Map<String,Object>> list;
private Context context;
private LayoutInflater layoutInflater;
public myitemadapter(List<Map<String, Object>> list, Context context) {
this.list = list;
this.context = context;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view==null){
view = LayoutInflater.from(context).inflate(R.layout.item, null);
viewHolder=new ViewHolder();
viewHolder.textView=view.findViewById(R.id.TextViewab);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText(list.get(i).get("namenumber").toString());
return view;
}
class ViewHolder{
TextView textView;
}
}
Fragment2短信代碼
public class mf2 extends Fragment {
List<Map<String, Object>> list = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = getLayoutInflater().inflate(R.layout.f2, null);
final ListView ListView = view.findViewById(R.id.ListView_message);
Button Button = view.findViewById(R.id.Button_message);
Button.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor cursor = null;
cursor = contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
Map<String, Object> map = new HashMap<>();
map.put("addressbody", address + body);
list.add(map);
}
myitem2adapter myitem2adapter = new myitem2adapter(list, getActivity());
ListView.setAdapter(myitem2adapter);
}
});
return view;
}
}
Fragment2短信適配器代碼
public class myitem2adapter extends BaseAdapter {
private List<Map<String,Object>> list;
private Context context;
private LayoutInflater layoutInflater;
public myitem2adapter(List<Map<String, Object>> list, Context context) {
this.list = list;
this.context = context;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view==null){
view = LayoutInflater.from(context).inflate(R.layout.item, null);
viewHolder=new ViewHolder();
viewHolder.textView=view.findViewById(R.id.TextViewab);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText(list.get(i).get("addressbody").toString());
return view;
}
class ViewHolder{
TextView textView;
}
}
Fragment2短信布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<Button
android:id="@+id/Button_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取短信"
/>
<ListView
android:id="@+id/ListView_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
item布局代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/TextViewab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1111111111111" />
</LinearLayout>