2016-07-05 48 views
0

說明: 我想更多的,但我沒有找到如何設置在Viewpager透明片段。我正在申請。我的客戶的要求是誰會讓一個應用程序像下面網址如何在android中的Viewpager上創建片段?

https://play.google.com/store/search?q=munchery&hl=en

在上述網址的應用。在這種應用中,他們增加了更多的功能,例如navigationDrawer,與viewpager等標籤

有一件事是,當我在ActionBar今天的菜單上單擊它是開放的東西。當我點擊Munchery應用程序中的actionBar時,我無法識別它的實際打開情況。

我試圖實現,但沒有一樣munchery應用程序實現的。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.millu.whatsappdemo.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" > 

      <TextView 
       android:id="@+id/action_text" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:layout_marginLeft="25dp" 
       android:layout_marginRight="25dp" 
       android:textSize="20sp" 
       android:clickable="true" 
       android:textColor="#FFFFFF" 
       android:text="Text view" 
       android:drawableRight="@drawable/ic_up_arrow"> 
      </TextView> 
     </android.support.v7.widget.Toolbar> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      app:tabMode="fixed" 
      app:tabGravity="fill"/> 
    </android.support.design.widget.AppBarLayout> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    public TextView action_text; 
    public Toolbar toolbar; 

    TabLayout tabs; 
    ViewPager viewPager; 

    List<Fragment> fragmentList; 
    List<String> tab_title; 
    boolean flag=false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     tabs = (TabLayout) findViewById(R.id.tabs); 
     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     action_text = (TextView) toolbar.findViewById(R.id.action_text); 

     fragmentList = new ArrayList<>(); 
     tab_title = new ArrayList<>(); 

     tab_title.add("ONE"); 
     tab_title.add("TWO"); 
     fragmentList.add(new OneFragment()); 
     fragmentList.add(new TwoFragment()); 

     ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title); 
     viewPager.setAdapter(viewPagerAdapter); 
     viewPager.setCurrentItem(0); 
     tabs.setupWithViewPager(viewPager); 

     action_text.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Fragment fragment=new ThirdFragment(); 
       FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); 
       if(!fragment.isHidden()) { 
        fragmentTransaction.replace(R.id.container,fragment); 
        flag=true; 
       } 
       else{ 
        fragmentTransaction.remove(fragment); 
        flag=false; 
       } 
       fragmentTransaction.commit(); 
      } 
     }); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()){ 
      case R.id.action_settings: 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 

    } 
} 

在上述代碼中,我在動作條設置的TextView的平坦的onClick用於隱藏和顯示的片段。在我的演示中,問題是當我的第三個片段打開並且不隱藏時,顯示選項卡。

請幫我認識到什麼是它的動作條中點擊打開?

+0

我看到了應用程序,它剛剛打開dialogfragment或從上面的onClick的actionbartext一些自定義對話框。 – Drv

+0

我試過對話框片段,但dialogFragment隱藏了actionBar –

+0

嘿,你是否明白我的觀點?它隱藏了我想隱藏標籤的操作欄 –

回答

0

嘗試下面的代碼:activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".ScrollingActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <TextView 
     android:id="@+id/action_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="25dp" 
     android:layout_marginRight="25dp" 
     android:clickable="true" 
     android:drawableRight="@mipmap/ic_launcher" 
     android:gravity="center" 
     android:text="Text view" 
     android:textColor="#FFFFFF" 
     android:textSize="20sp"></TextView> 
</android.support.v7.widget.Toolbar> 

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

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     app:tabGravity="fill" 
     app:tabMode="fixed" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/tabs" 
     android:background="@android:color/white" /> 

</RelativeLayout> 


<RelativeLayout 
    android:id="@+id/reltv" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar" 
    android:background="#80ffffff" 
    android:visibility="gone"> 

    <RelativeLayout 
     android:id="@+id/reltv2" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:background="@android:color/white"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="Do whatever you like it here." 
      android:textAppearance="?attr/textAppearanceLargePopupMenu" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_alignParentBottom="true" 
      android:background="@android:color/darker_gray" /> 
    </RelativeLayout> 
</RelativeLayout> 


</RelativeLayout> 

而且下面你ManiActivity.java:

public class MainActivity extends AppCompatActivity { 

public TextView action_text; 
public Toolbar toolbar; 

TabLayout tabs; 
ViewPager viewPager; 

List<Fragment> fragmentList; 
List<String> tab_title; 
boolean flag = false; 

RelativeLayout reltv; 
RelativeLayout reltv2; 

boolean isShown = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    tabs = (TabLayout) findViewById(R.id.tabs); 
    viewPager = (ViewPager) findViewById(R.id.viewpager); 
    action_text = (TextView) toolbar.findViewById(R.id.action_text); 
    reltv = (RelativeLayout) findViewById(R.id.reltv); 
    reltv2 = (RelativeLayout) findViewById(R.id.reltv2); 

    fragmentList = new ArrayList<>(); 
    tab_title = new ArrayList<>(); 

    tab_title.add("ONE"); 
    tab_title.add("TWO"); 
    fragmentList.add(new OneFragment()); 
    fragmentList.add(new TwoFragment()); 

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList, tab_title); 
    viewPager.setAdapter(viewPagerAdapter); 
    viewPager.setCurrentItem(0); 
    tabs.setupWithViewPager(viewPager); 

    action_text.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!isShown) { 
       isShown=true; 
       reltv.setVisibility(View.VISIBLE); 
      }else { 
       isShown=false; 
       reltv.setVisibility(View.GONE); 
      } 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_scrolling, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case R.id.action_settings: 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 

} 
} 
+0

@Milan Gajera請檢查以上代碼是否有幫助。 – Drv

0

首先,你應該用一些半透明或透明色爲Android:背景片段佈局中的父屬性的屬性,例如DayFragment。這是您在疊加FrameLayout中膨脹的片段。

對於片段的順利地膨脹時,可以使用自定義動畫,使過渡平滑像在Munchery應用(使用滑動從頂部動畫)

protected void replaceFragment(int containerViewId, Fragment fragment, boolean isAddToBackStack, boolean isAnimateTransition) { 
     String backStateName = fragment.getClass().getName(); 
     FragmentManager fm = this.getSupportFragmentManager(); 
     boolean fragmentPopped = fm.popBackStackImmediate(backStateName, 0); 

     if (!fragmentPopped && getSupportFragmentManager().findFragmentByTag(backStateName) == null) { 
      FragmentTransaction ft = fm.beginTransaction(); 
      if(isAnimateTransition) { 
       animateFragmentTransition(ft); 
      } 
      ft.replace(containerViewId, fragment, backStateName); 
      if (isAddToBackStack) { 
       ft.addToBackStack(backStateName); 
      } 
      ft.commit(); 
     } 
    } 


private void animateFragmentTransition(FragmentTransaction ft) { 
    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.slide_in_from_top, R.anim.slide_out_to_top); 
} 


DayFragment fragment = DayFragment.newInstance(null); 
     replaceFragment(R.id.container, fragment, false, true); 

Alternitively,可以使用ObjectAnimators到revael和隱藏視圖,如你所願。例如: -

private void animate() { 
     View viewToShiftOut = mNormalModelayoutContainer; 
     View viewToShiftIn = mEditModelayoutContainer; 
     ObjectAnimator outAnim = ObjectAnimator.ofFloat(viewToShiftOut, "y", 0, -heightOfScreen); 
     ObjectAnimator inAnim = ObjectAnimator.ofFloat(viewToShiftIn, "y", heightOfScreen , 0); 
     outAnim.setDuration(1000); 
     inAnim.setDuration(1000); 
     outAnim.start(); 
     inAnim.start(); 
    } 
相關問題