2019-08-13ViewPager+Fragment+TabLayout+聯(lián)系人短信+打電話

添加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>

項目:tablayoutaa

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市竿报,隨后出現(xiàn)的幾起案子扩所,更是在濱河造成了極大的恐慌迹恐,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,294評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異难捌,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鸦难,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評論 3 385
  • 文/潘曉璐 我一進(jìn)店門根吁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人合蔽,你說我怎么就攤上這事击敌。” “怎么了拴事?”我有些...
    開封第一講書人閱讀 157,790評論 0 348
  • 文/不壞的土叔 我叫張陵沃斤,是天一觀的道長。 經(jīng)常有香客問我刃宵,道長衡瓶,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,595評論 1 284
  • 正文 為了忘掉前任牲证,我火速辦了婚禮哮针,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘坦袍。我一直安慰自己十厢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,718評論 6 386
  • 文/花漫 我一把揭開白布键闺。 她就那樣靜靜地躺著寿烟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪辛燥。 梳的紋絲不亂的頭發(fā)上筛武,一...
    開封第一講書人閱讀 49,906評論 1 290
  • 那天,我揣著相機(jī)與錄音挎塌,去河邊找鬼徘六。 笑死,一個胖子當(dāng)著我的面吹牛榴都,可吹牛的內(nèi)容都是我干的待锈。 我是一名探鬼主播,決...
    沈念sama閱讀 39,053評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼嘴高,長吁一口氣:“原來是場噩夢啊……” “哼竿音!你這毒婦竟也來了和屎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,797評論 0 268
  • 序言:老撾萬榮一對情侶失蹤春瞬,失蹤者是張志新(化名)和其女友劉穎柴信,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宽气,經(jīng)...
    沈念sama閱讀 44,250評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡随常,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,570評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了萄涯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绪氛。...
    茶點(diǎn)故事閱讀 38,711評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖涝影,靈堂內(nèi)的尸體忽然破棺而出枣察,到底是詐尸還是另有隱情,我是刑警寧澤袄琳,帶...
    沈念sama閱讀 34,388評論 4 332
  • 正文 年R本政府宣布询件,位于F島的核電站,受9級特大地震影響唆樊,放射性物質(zhì)發(fā)生泄漏宛琅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,018評論 3 316
  • 文/蒙蒙 一逗旁、第九天 我趴在偏房一處隱蔽的房頂上張望嘿辟。 院中可真熱鬧,春花似錦片效、人聲如沸红伦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,796評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昙读。三九已至,卻和暖如春膨桥,著一層夾襖步出監(jiān)牢的瞬間蛮浑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,023評論 1 266
  • 我被黑心中介騙來泰國打工只嚣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留沮稚,地道東北人。 一個月前我還...
    沈念sama閱讀 46,461評論 2 360
  • 正文 我出身青樓册舞,卻偏偏與公主長得像蕴掏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,595評論 2 350

推薦閱讀更多精彩內(nèi)容