android中,專案架構的搭建
一.首先搭建這個專案框架的時候需要關聯兩個庫檔案,分別是menu_library和xutillibrary。
二.現在把專案架構中需要建立的包展示如下:
三.把搭建的專案架構展示如下:
四.現在開始寫具體實現這個效果的邏輯:
1.首先先把專案搭建的三個佈局寫出來:
a. activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</FrameLayout>
b.frag_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="@drawable/bottom_tab_bg"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="2dp">
<RadioButton
android:id="@+id/rb_function"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_function"
android:text="@string/tab_function"/>
<RadioButton
android:id="@+id/rb_news_center"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_newscenter"
android:text="@string/tab_news_center" />
<RadioButton
android:id="@+id/rb_smart_service"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_smartservice"
android:text="@string/tab_smart_service" />
<RadioButton
android:id="@+id/rb_gov_affairs"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_govaffairs"
android:text="@string/tab_gov_affairs" />
<RadioButton
android:id="@+id/rb_setting"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_setting"
android:text="@string/tab_setting" />
</RadioGroup>
</LinearLayout>
c.menu_frame
<?xml version="1.0" encoding="utf-8"?>
<!-- 側拉欄Menu佈局 -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
2.其次開始寫邏輯:
a. BasePager.java
// BasePager的目的就是把ViewPager5張顯示page的公共部分抽取出來,初始化檢視和初始化資料
package com.saiermeng.jiagou.base;
import android.content.Context;
import android.view.View;
public abstract class BasePager {
public Context context;
public View view;
//private Object view;
public BasePager(Context context){
this.context=context;
view =initView();
}
/*private Object initView() {
// TODO Auto-generated method stub
return null;*/
public abstract View initView();
public abstract void initData();
public View getRootView(){
return view;
}
}
b. BaseFragment.java
//BaseFragment的目的就是把HomeFragment,MenuFragment的公共部分抽取出來,拿到上下文,初始化檢視,初始化資料
package com.saiermeng.jiagou.base;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment {
public Context context;
private View view;
//private Object view;
//private FragmentActivity context;
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
//拿到上下文
context=getActivity();
super.onCreate(savedInstanceState);
}
//初始化檢視
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=initView();
return view;
// super.onCreateView(savedInstanceState);
}
//初始化資料
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
initData();
super.onActivityCreated(savedInstanceState);
}
/*private void initData() {
// TODO Auto-generated method stub
}*/
/*private View initView() {
// TODO Auto-generated method stub
return null;
}*/
public abstract View initView();
public abstract void initData();
//抽象方法的作用是,你在此類裡不必實現它,只是一個虛方法,所有的實現可以到繼承此類的子類裡面去做.
//你也可以理解為,抽象方法就是用來被過載的方法.你可以在子類裡對它進行過載,也可以不進行過載.
}
c.HomeFragment.java
// HomeFragment的目的就是:1.點選哪個RadioButton,頁面上顯示對應的圖片資訊 2.把首頁,新聞中心...新增到list集合中,通過Adapter顯示出來
package com.saiermeng.jiagou.Fragment;
import java.util.ArrayList;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.saiermeng.jiagou.R;
import com.saiermeng.jiagou.base.BaseFragment;
import com.saiermeng.jiagou.base.BasePager;
import com.saiermeng.jiagou.pager.NewCenterPager;
import com.saiermeng.jiagou.pager.FunctionPager;
import com.saiermeng.jiagou.pager.GovAffairsPager;
import com.saiermeng.jiagou.pager.SettingPager;
import com.saiermeng.jiagou.pager.SmartServicePager;
public class HomeFragment extends BaseFragment {
private ArrayList<BasePager> list=new ArrayList<BasePager>();
private View view;
@ViewInject(R.id.layout_content)
private ViewPager layout_content;
@ViewInject(R.id.main_radio)
private RadioGroup main_radio;
@Override
public View initView() {
// TODO Auto-generated method stub
//載入佈局
view=View.inflate(context, R.layout.frag_home, null);
//繫結view操作通過註解去拿到控制元件的id
ViewUtils.inject(this, view);
main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.rb_function:
layout_content.setCurrentItem(0);
break;
case R.id.rb_news_center:
layout_content.setCurrentItem(1);
break;
case R.id.rb_smart_service:
layout_content.setCurrentItem(2);
break;
case R.id.rb_gov_affairs:
layout_content.setCurrentItem(3);
break;
case R.id.rb_setting:
layout_content.setCurrentItem(4);
break;
default:
break;
}
}
});
//你滑動的時候viewpager換頁的操作
layout_content.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//
list.get(arg0).initData();
}
});
//預設的選中他(首頁)
main_radio.check(R.id.rb_function);
return view;
}
@Override
public void initData() {
// TODO Auto-generated method stub
//初始化一下,保證這個list集合中的元素為空
list.clear();
list.add(new FunctionPager(context));
list.add(new NewCenterPager(context));
list.add(new SmartServicePager(context));
list.add(new GovAffairsPager(context));
list.add(new SettingPager(context));
layout_content.setAdapter(new MyAdapter());
}
public class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(list.get(position).getRootView());
return list.get(position).getRootView();
}
}
}
d.MenuFragment.java
/* MenuFragment 的目的就是載入側拉欄裡面的資訊,因為它和HomeFragment一樣,需要initView和initData,所以把它們的共同的部分抽取成BaseFragment讓
MenuFragment和HomeFragment繼承*/
package com.saiermeng.jiagou.Fragment;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BaseFragment;
public class MenuFragment extends BaseFragment {
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("側拉欄");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
e.FunctionPager.java
package com.saiermeng.jiagou.pager;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BasePager;
public class FunctionPager extends BasePager {
public FunctionPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("首頁");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
f.GovAffairsPager.java
package com.saiermeng.jiagou.pager;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BasePager;
public class GovAffairsPager extends BasePager {
public GovAffairsPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("政務中心");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
g. NewCenterPager.java
package com.saiermeng.jiagou.pager;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BasePager;
public class NewCenterPager extends BasePager {
public NewCenterPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("新聞中心");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
h.SettingPager .java
package com.saiermeng.jiagou.pager;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BasePager;
public class SettingPager extends BasePager {
public SettingPager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("設定中心");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
i.SmartServicePager .java
package com.saiermeng.jiagou.pager;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
import com.saiermeng.jiagou.base.BasePager;
public class SmartServicePager extends BasePager {
public SmartServicePager(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public View initView() {
// TODO Auto-generated method stub
TextView textView = new TextView(context);
textView.setText("智慧服務");
return textView;
}
@Override
public void initData() {
// TODO Auto-generated method stub
}
}
j. MainActivity.java
package com.saiermeng.jiagou;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.saiermeng.jiagou.Fragment.HomeFragment;
import com.saiermeng.jiagou.Fragment.MenuFragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu slidingMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//載入佈局
setContentView(R.layout.activity_main);
//載入佈局
setBehindContentView(R.layout.menu_frame);
//拿到SlidingMenu這個物件
slidingMenu=getSlidingMenu();
//設定的側滑為左側滑
slidingMenu.setMode(SlidingMenu.LEFT);
//設定左邊側滑的寬度
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
//側滑分界線的陰影部分
slidingMenu.setShadowDrawable(R.drawable.shadow);
//側滑分界線陰影部分的寬度
slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
//整個螢幕可以拖拽
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//拿到MenuFragment的物件
MenuFragment menuFragment = new MenuFragment();
//替換左側幀佈局操作
getSupportFragmentManager().beginTransaction().replace(R.id.menu, menuFragment, "Menu").commit();
//替換內容頁的幀佈局操作
//拿到HomeFragment的物件
HomeFragment homeFragment = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.layout_container, homeFragment, "Home").commit();
}
}