【达内课程】联系人项目(ViewPager和底部联动)

系列文章:

  1. 联系人项目(知识预热)
  2. 联系人项目(ViewPager和底部联动)
  3. 联系人项目(查询联系人数据)
  4. 联系人项目(显示联系人数据)
  5. 联系人项目(显示通话记录)
  6. 联系人项目(显示拨号界面)
  7. 联系人项目(短信显示)
  8. 联系人项目(短信详情显示)
  9. 联系人项目(总)

目录结构

在这里插入图片描述

MVP架构 分包结构
在这里插入图片描述

开始联系人项目之前先把目录结构建好
在这里插入图片描述

界面

这一节,先做出ViewPager和RadioButton联动效果

在这里插入图片描述

AndroidManifest.xml中修改主题

        android:theme="@android:style/Theme.Light.NoTitleBar"

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/radioGroup">

    </android.support.v4.view.ViewPager>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="#fffff0">
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/radio_button_selector1"
            android:layout_weight="1"
            android:button="@null"
            android:layout_marginTop="8dp"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/radio_button_selector2"
            android:layout_weight="1"
            android:button="@null"
            android:layout_marginTop="8dp"/>
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/radio_button_selector3"
            android:layout_weight="1"
            android:button="@null"
            android:layout_marginTop="8dp"/>
        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/radio_button_selector4"
            android:layout_weight="1"
            android:button="@null"
            android:layout_marginTop="8dp"/>
    </RadioGroup>
    
</RelativeLayout>

drawable下新建radio_button_selector1

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/history_selected"/>
    <item android:state_checked="false" android:drawable="@mipmap/history_unselected"/>
</selector>

radio_button_selector2-4类似,不再重复

新建ContactFragment

public class ContactFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contactfragment,null);
        return view;
    }
}

注意导入V4包

fragment_contactfragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.ContactFragment">

    <RelativeLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fffff0">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="联系人"
            android:layout_centerInParent="true"/>

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/search"
            android:layout_marginRight="10dp"
            />
    </RelativeLayout>

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:numColumns="3">
        
    </GridView>
</RelativeLayout>

MainActivity

public class MainActivity extends FragmentActivity {
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        setPagerAdapter();
        setListeners();
    }

    private void setListeners() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()){
                    case R.id.radioButton1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.radioButton2:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.radioButton3:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.radioButton4:
                        viewPager.setCurrentItem(4);
                        break;
                }
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioButton1.setChecked(true);
                        break;
                    case 1:
                        radioButton2.setChecked(true);
                        break;
                    case 2:
                        radioButton3.setChecked(true);
                        break;
                    case 3:
                        radioButton4.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void setPagerAdapter() {
        fragments = new ArrayList();
        fragments.add(new ContactFragment());
        fragments.add(new ContactFragment());
        fragments.add(new ContactFragment());
        fragments.add(new ContactFragment());

        MyPagerAdaper pagerAdaper = new MyPagerAdaper(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdaper);
    }

    private void initViews() {
        viewPager = findViewById(R.id.viewPager);
        radioGroup = findViewById(R.id.radioGroup);
        radioButton1 = findViewById(R.id.radioButton1);
        radioButton2 = findViewById(R.id.radioButton2);
        radioButton3 = findViewById(R.id.radioButton3);
        radioButton4 = findViewById(R.id.radioButton4);
    }

    class MyPagerAdaper extends FragmentPagerAdapter{

        public MyPagerAdaper(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值