XSuperMES移動端運用FragmentActivity適配大屏幕(二)

今天繼續完成剩下的學習部分,如今項目不少地方使用viewpager來提供滑動,今天記錄學習viewpager配合fragment的顯示,增長一個CallLogsFragment配合以前SMSListFragment
1.CallLogsFragment,主要顯示手機的電話記錄,這都是以前的代碼,因此只要把Activity轉換成Fragment就能夠了,也同樣, 
Activity.setContentView(int) 轉變成Fragment.onCreateView(View) 而且 findViewById也在裏面了
Activity.onCreate() 代碼 轉成  Fragment.onActivityCreated()中
Activity.onResume() 代碼 轉成 Fragment.onResume() 中android

 代碼以下:ide

public class CallLogsFragment extends Fragment{  佈局

 

 

private ListView vList;  學習

 

private CallPanelView vDial;  xml

 

private TextView vShowDial;  utf-8

 

private CallListAdapter mAdapter;  get

 

 

 

private Animation mDialHidAnim;  animation

 

private Animation mDialShowAnim;  源碼

 

 

@Override  it

 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 

 

Bundle savedInstanceState) { 

 

// TODO Auto-generated method stub 

 

View root = inflater.inflate(R.layout.activity_calls_list_screen, null); 

 

vList = (ListView) root.findViewById(R.id.activity_call_list_lv); 

 

vShowDial = (TextView) root.findViewById(R.id.activity_call_show_dial); 

 

vDial = (CallPanelView) root.findViewById(R.id.dial_panel_view); 

 

return root; 

 

 

 

@Override 

 

public void onActivityCreated(Bundle savedInstanceState) { 

 

// TODO Auto-generated method stub 

 

super.onActivityCreated(savedInstanceState); 

 

prepareResourceAndData(); 

 

bindView(); 

 

 

 

@Override 

 

public void onResume() { 

 

// TODO Auto-generated method stub 

 

super.onResume(); 

 

mAdapter.setCallNumbers(CallCache.getInstance().getCallNumbers()); 

 

 

 

private void prepareResourceAndData() { 

 

mAdapter = new CallListAdapter(getActivity()); 

 

mDialHidAnim = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), 

 

R.anim.dial_panel_hidden); 

 

mDialHidAnim.setAnimationListener(new AnimationListener() { 

 

public void onAnimationStart(Animation animation) {} 

 

public void onAnimationRepeat(Animation animation) {} 

 

public void onAnimationEnd(Animation animation) { 

 

// End 

 

vShowDial.setVisibility(View.VISIBLE); 

 

vDial.setVisibility(View.GONE); 

 

 

}); 

 

 

 

mDialShowAnim = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), 

 

R.anim.dial_panel_show); 

 

mDialShowAnim.setAnimationListener(new AnimationListener() { 

 

public void onAnimationStart(Animation animation) {} 

 

public void onAnimationRepeat(Animation animation) {} 

 

public void onAnimationEnd(Animation animation) { 

 

// End 

 

vShowDial.setVisibility(View.GONE); 

 

 

}); 

 

 

  

private void bindView() { 

 

vList.setAdapter(mAdapter); 

 

vList.setOnItemClickListener(new OnItemClickListener() { 

 

 

 

@Override 

 

public void onItemClick(AdapterView<?> parent, View view, 

 

int position, long id) { 

 

// TODO Auto-generated method stub 

 

CallItemView itemview = (CallItemView) view; 

 

itemview.toggleExpand(); 

 

hidDialPanel(true); 

 

 

}); 

 

 

 

vList.setOnScrollListener(new OnScrollListener() { 

 

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {} 

 

@Override 

 

public void onScrollStateChanged(AbsListView view, int scrollState) { 

 

// TODO Auto-generated method stub 

 

if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { 

 

hidDialPanel(false); 

 

 

 

}); 

 

 

 

vDial.setPanelAnim(mDialShowAnim, mDialHidAnim); 

 

 

 

 

vShowDial.setVisibility(View.GONE); 

 

vShowDial.setOnClickListener(new OnClickListener() { 

 

 

 

@Override 

 

public void onClick(View v) { 

 

// TODO Auto-generated method stub 

 

vDial.show(); 

 

 

}); 

 

 

 

 

private void hidDialPanel(boolean anim) { 

 

if (vDial.getVisibility() == View.VISIBLE) { 

 

if(anim){ 

 

vDial.dismiss(); 

 

}else{ 

 

vDial.setVisibility(View.GONE); 

 

vShowDial.setVisibility(View.VISIBLE); 

 

 

 

2.佈局修改爲,增長ViewPager,去掉原有的容器framelayout:

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 

  

    android:id="@+id/door_root_content_fl" 

  

    android:layout_width="match_parent" 

  

    android:layout_height="match_parent" > 

 

 

 

    <LinearLayout 

  

        android:layout_width="match_parent" 

  

        android:layout_height="match_parent" 

  

        android:orientation="vertical" > 

 

 

 

        <android.support.v4.view.ViewPager 

  

            android:id="@+id/door_viewpager_content_vp" 

  

            android:layout_width="match_parent" 

  

            android:layout_height="0dp" 

  

            android:layout_weight="1" /> 

 

 

 

        <LinearLayout 

  

            android:layout_width="match_parent" 

  

            android:layout_height="55dp" 

  

            android:orientation="horizontal" > 

 

 

 

            <Button 

  

                android:id="@+id/door_menu_btn" 

  

                android:layout_width="match_parent" 

  

                android:layout_height="match_parent" 

  

                android:gravity="center" 

  

                android:text="菜單" /> 

  

        </LinearLayout> 

  

    </LinearLayout> 

</FrameLayout> 

3.V4 support 包裏面已經幫咱們寫好了 FragmentPagerAdapter,看源碼能夠知道在 PageAdapter.instantiateItem中,有:

Fragment fragment = mFragmentManager.findFragmentByTag(name); 

  

        if (fragment != null) { 

  

            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment); 

  

            mCurTransaction.attach(fragment); 

  

        } else { 

  

            fragment = getItem(position); 

  

            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment); 

  

            mCurTransaction.add(container.getId(), fragment, 

  

                    makeFragmentName(container.getId(), itemId)); 

  

        } 

 能夠看出 ViewPager在添加fragment 的時候先在FragmentManager裏面找有沒有,若是沒有就會去請求PageAdapter.getItem(position)來生成fragment,進行添加,所以咱們只要重寫FragmentPagerAdapter並實現getItem方法就能夠了,代碼以下:

class FragmentAdapter extends FragmentPagerAdapter{ 

public FragmentAdapter(FragmentManager fm) { 

 

super(fm); 

 

// TODO Auto-generated constructor stub 

 

     

@Override 

 

public Fragment getItem(int position) { 

 

// TODO Auto-generated method stub 

 

return getPagerFragmentByPosition(position); 

 

 

 

 

@Override 

 

public int getCount() { 

 

// TODO Auto-generated method stub 

 

return 2; 

 

   

}private Fragment getPagerFragmentByPosition(int position){ 

 

if(position == FRAG_INDEX_CALL_LOG){ 

 

return new CallLogsFragment(); 

 

 

if(position == FRAG_INDEX_SMS){ 

 

return new SMSListFragment(); 

 

 

return null; 

 

4.頁面完整代碼:

public class DoorFragmentActivity extends FragmentActivity{ 

    

public static final String FRAG_SMS = "sms_list_frag"; 

 

public static final String FRAG_TEXT = "text_frag"; 

   

public static final int FRAG_INDEX_CALL_LOG = 0; 

 

public static final int FRAG_INDEX_SMS = 1; 

  

private Fragment mTextFragment; 

 

private FragmentManager mFragMgr; 

 

 

private ViewPager vViewPager; 

 

private Button mMenuBtn; 

   

@Override 

 

protected void onCreate(Bundle savedInstanceState) { 

 

// TODO Auto-generated method stub 

 

super.onCreate(savedInstanceState); 

 

setContentView(R.layout.door_fragment_activity_layout); 

 

initFragments(); 

 

bindViews(); 

 

 

private void bindViews(){ 

 

mMenuBtn = (Button) findViewById(R.id.door_menu_btn); 

 

mMenuBtn.setOnClickListener(new OnClickListener() { 

 

 

@Override 

 

public void onClick(View v) { 

 

// TODO Auto-generated method stub 

 

showOutFragments(FRAG_TEXT, true); 

 

 

}); 

 

vViewPager = (ViewPager) findViewById(R.id.door_viewpager_content_vp); 

 

vViewPager.setAdapter(new FragmentAdapter(mFragMgr)); 

 

 

 

private void initFragments(){ 

 

mFragMgr = getSupportFragmentManager(); 

 

TextFragment textfrag = new TextFragment(); 

 

textfrag.setMessage("這是 菜單界面"); 

 

mTextFragment = textfrag; 

 

 

 

private void showOutFragments(String tag, boolean needback){ 

 

FragmentTransaction trans = mFragMgr.beginTransaction(); 

 

trans.setCustomAnimations(R.anim.frag_enter,   

 

R.anim.frag_exit); 

 

trans.add(R.id.door_root_content_fl, getFragmentByTag(tag), tag); 

 

if(needback){ 

 

trans.addToBackStack(tag); 

 

 

trans.commit(); 

  

 

 

private Fragment getFragmentByTag(String tag){ 

 

if(FRAG_TEXT.equals(tag)){ 

 

return mTextFragment; 

 

 

return null; 

 

 

 

private Fragment getPagerFragmentByPosition(int position){ 

 

if(position == FRAG_INDEX_CALL_LOG){ 

 

return new CallLogsFragment(); 

 

 

if(position == FRAG_INDEX_SMS){ 

 

return new SMSListFragment(); 

 

 

return null; 

 

 

 

class FragmentAdapter extends FragmentPagerAdapter{ 

 

 

 

public FragmentAdapter(FragmentManager fm) { 

 

super(fm); 

 

// TODO Auto-generated constructor stub 

 

 

 

 

@Override 

 

public Fragment getItem(int position) { 

 

// TODO Auto-generated method stub 

 

return getPagerFragmentByPosition(position); 

 

 

 

 

@Override 

 

public int getCount() { 

 

// TODO Auto-generated method stub 

 

return 2; 

 

}   

相關文章
相關標籤/搜索