系列文章:
- 联系人项目(知识预热)
- 联系人项目(ViewPager和底部联动)
- 联系人项目(查询联系人数据)
- 联系人项目(显示联系人数据)
- 联系人项目(显示通话记录)
- 联系人项目(显示拨号界面)
- 联系人项目(短信显示)
- 联系人项目(短信详情显示)
- 联系人项目(总)
目录结构
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();
}
}
}