一、启动页面
SplashActivity: public class SplashActivity extends Activity { RelativeLayout animaContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); initView(); initAnim(); } /** * 动画相关 */ private void initAnim() { 加载动画 Animation anim = AnimationUtils.loadAnimation(this, R.anim.splash_anim); animaContainer.startAnimation(anim); // new Handler().postDelayed(new Runnable() { 线程处理 // // @Override // public void run() { // Intent intent = new Intent(SplashActivity.this,MainActivity.class); // startActivity(intent); // finish(); // } // }, 3000); anim.setAnimationListener(new AnimationListener() { 动画监听器 @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override 动画结束后 public void onAnimationEnd(Animation animation) { Intent intent = new Intent(SplashActivity.this,MainActivity.class); startActivity(intent); finish(); } }); } /** * 初始化各种视图 */ private void initView() { animaContainer=(RelativeLayout) findViewById(R.id.rl_splash_animcontainer); } } |
二、ViewPager与Fragment联动
public class MyPagertAdapter extends FragmentPagerAdapter{ List<Fragment> list ; public MyPagertAdapter(FragmentManager fm) { super(fm); list= new ArrayList<Fragment>(); list.add(new CalllogFragment()); list.add(new ContactFragment()); list.add(new SmsFragment()); list.add(new DialpadFragment()); } public MyPagertAdapter(FragmentManager fm, List<Fragment> list) { super(fm); 方便以后拓展 this.list = list; } @Override public Fragment getItem(int pos) { return list.get(pos); } @Override public int getCount() { return list.size(); } } |
public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnCheckedChangeListener { private ViewPager vPager; private RadioGroup rGroup; private MyPagertAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListeners(); } private void setListeners() { //加入监听器 vPager.setOnPageChangeListener(this); rGroup.setOnCheckedChangeListener(this); } private void initView() { //初始化 rGroup =(RadioGroup) findViewById(R.id.radioGroup1); vPager=(ViewPager) findViewById(R.id.pagerId); adapter=new MyPagertAdapter(getSupportFragmentManager()); //关联适配器 vPager.setAdapter(adapter); vPager.setCurrentItem(1); 设置开始时默认页面 rGroup.check(rGroup.findViewById(R.id.contact).getId()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } //滑动页面时,按钮联动 @Override public void onPageSelected(int pos) { rGroup.check(rGroup.getChildAt(pos).getId()); } //点击按钮,页面联动 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { vPager.setCurrentItem(rGroup.indexOfChild(rGroup.findViewById(checkedId)),false); } false可以取消动画 } |
三、查询数据
public class ContactFragment extends Fragment { private GridView gView; private List<Contact> contacts; public ContactFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view =inflater.inflate(R.layout.fragment_contact, null); initGridView(view);传一个View,找到字View setGridView(); return view; } private void setGridView() { gView.setAdapter(new ContactGridAdapter(getActivity(),R.layout.item_contact_gridview,contacts) ); } private void initGridView(View view) { gView=(GridView) view.findViewById(R.id.contactGridview); contacts=new ArrayList<Contact>(); ContentResolver cr1 = getActivity().getContentResolver(); Cursor cursor1 = cr1.query(Contacts.CONTENT_URI, new String[]{Contacts._ID,Contacts.PHOTO_ID}, null, null, null); while(cursor1.moveToNext()){ Contact contact =new Contact(); contact.set_id(cursor1.getInt(0)); contact.setPhoto_id(cursor1.getInt(1)); ContentResolver cr2 = getActivity().getContentResolver(); 获取资源 Cursor cursor2 = cr2.query(Data.CONTENT_URI, new String[]{Data.MIMETYPE,Data.DATA1,Data.DATA15}, Data.RAW_CONTACT_ID+"=?", new String[]{String.valueOf(contact.get_id())}, null); while(cursor2.moveToNext()){ 两个表联合查询 String mimeType = cursor2.getString(0); if(mimeType.equals("vnd.android.cursor.item/email_v2")){ contact.setEmail(cursor2.getString(1)); }else if(mimeType.equals("vnd.android.cursor.item/organization")){ contact.setCompany(cursor2.getString(1)); }else if(mimeType.equals("vnd.android.cursor.item/name")){ contact.setName(cursor2.getString(1)); }else if(mimeType.equals("vnd.android.cursor.item/phone_v2")){ contact.setNumber(cursor2.getString(1)); }else if(mimeType.equals("vnd.android.cursor.item/postal-address_v2")){ contact.setAddress(cursor2.getString(1)); }else if(mimeType.equals("vnd.android.cursor.item/photo")){ byte[] bytes = cursor2.getBlob(2); 获取字节数数组 Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 生成BitMap contact.setPhotoBitmap(bitmap); } } contacts.add(contact); cursor2.close(); } Log.i("tag", contacts.toString()); Log.i("tag", contacts.size()+""); cursor1.close(); } } |