2016-01-20 20 views
-2

我有三个布局文件。一个名为activity_home,另一个名为activity_main,最后一个名为activity_test。 activity_main只是一个简单的布局,只有一个textview和一个按钮。 activity_test是一个包含文本视图的布局文件hello world.activity_home就像下面这样。如何在线性布局内开始新的活动?

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<com.example.shan.real_connect.widget.RevealFrameLayout 
    android:id="@+id/conteiner_frame" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:id="@+id/content_overlay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"/> 
    <LinearLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"/> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary"/> 

</com.example.shan.real_connect.widget.RevealFrameLayout> 

<ScrollView 
    android:id="@+id/scrollView" 
    android:scrollbarThumbVertical="@android:color/transparent" 
    android:layout_width="80dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start|bottom"> 

    <LinearLayout 
     android:id="@+id/left_drawer" 
     android:orientation="vertical" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="@android:color/transparent"> 
     <!-- Layout of Drawer --> 
    </LinearLayout> 
</ScrollView> 
</android.support.v4.widget.DrawerLayout> 

我想开我的activity_home和我activity_test加载到名为“content_frame”里面activity_home当我在我的主要活动点击按钮的线性布局。我怎么做那件事?我的home.java文件的代码也在下面。

public class Home extends ActionBarActivity implements ViewAnimator.ViewAnimatorListener,View.OnClickListener { 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle drawerToggle; 
    private List<SlideMenuItem> list = new ArrayList<>(); 
    private ContentFragment contentFragment; 
    private ViewAnimator viewAnimator; 
    private int res = R.drawable.content_music; 
    private LinearLayout linearLayout; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     contentFragment = ContentFragment.newInstance(R.drawable.content_music); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.content_frame, contentFragment) 
       .commit(); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawerLayout.setScrimColor(Color.TRANSPARENT); 
     linearLayout = (LinearLayout) findViewById(R.id.left_drawer); 
     linearLayout.setOnClickListener(this); 


     setActionBar(); 
     createMenuList(); 
     viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this); 
    } 

    private void createMenuList() { 
     SlideMenuItem menuItem0 = new SlideMenuItem(ContentFragment.CLOSE, R.drawable.icn_close); 
     list.add(menuItem0); 
     SlideMenuItem menuItem = new SlideMenuItem(ContentFragment.BUILDING, R.drawable.icn_1); 
     list.add(menuItem); 
     SlideMenuItem menuItem2 = new SlideMenuItem(ContentFragment.BOOK, R.drawable.icn_2); 
     list.add(menuItem2); 
     SlideMenuItem menuItem3 = new SlideMenuItem(ContentFragment.PAINT, R.drawable.icn_3); 
     list.add(menuItem3); 
     SlideMenuItem menuItem4 = new SlideMenuItem(ContentFragment.CASE, R.drawable.icn_4); 
     list.add(menuItem4); 
     SlideMenuItem menuItem5 = new SlideMenuItem(ContentFragment.SHOP, R.drawable.icn_5); 
     list.add(menuItem5); 
     SlideMenuItem menuItem6 = new SlideMenuItem(ContentFragment.PARTY, R.drawable.icn_6); 
     list.add(menuItem6); 
     SlideMenuItem menuItem7 = new SlideMenuItem(ContentFragment.MOVIE, R.drawable.icn_7); 
     list.add(menuItem7); 
    } 


    private void setActionBar() { 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     drawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       drawerLayout,   /* DrawerLayout object */ 
       toolbar, /* nav drawer icon to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description */ 
       R.string.drawer_close /* "close drawer" description */ 
     ) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       linearLayout.removeAllViews(); 
       linearLayout.invalidate(); 
      } 

      @Override 
      public void onDrawerSlide(View drawerView, float slideOffset) { 
       super.onDrawerSlide(drawerView, slideOffset); 
       if (slideOffset > 0.6 && linearLayout.getChildCount() == 0) 
        viewAnimator.showMenuContent(); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     drawerToggle.onConfigurationChanged(newConfig); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    private ScreenShotable replaceFragment(ScreenShotable screenShotable, int topPosition) { 
     //get the top position. it generates unique link according to the clicked view 
     this.res = this.res == R.drawable.content_music ? R.drawable.content_films : R.drawable.content_music; 
     View view = findViewById(R.id.content_frame); 
     int finalRadius = Math.max(view.getWidth(), view.getHeight()); 
     SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius); 
     animator.setInterpolator(new AccelerateInterpolator()); 
     animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION); 

     findViewById(R.id.content_overlay).setBackgroundDrawable(new BitmapDrawable(getResources(), screenShotable.getBitmap())); 
     animator.start(); 
     ContentFragment contentFragment = ContentFragment.newInstance(this.res); 
     getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, contentFragment).commit(); 
     return contentFragment; 
    } 

    @Override 
    public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) { 
     switch (slideMenuItem.getName()) { 
      case ContentFragment.CLOSE: 
       return screenShotable; 
      default: 
       return replaceFragment(screenShotable, position); 
     } 
    } 

    @Override 
    public void disableHomeButton() { 
     getSupportActionBar().setHomeButtonEnabled(false); 

    } 

    @Override 
    public void enableHomeButton() { 
     getSupportActionBar().setHomeButtonEnabled(true); 
     drawerLayout.closeDrawers(); 

    } 

    @Override 
    public void addViewToContainer(View view) { 
     linearLayout.addView(view); 
    } 

    @Override 
    public void onClick(View v) { 
     try { 
      if(v == linearLayout){ 
       drawerLayout.closeDrawers(); 
      } 
     }catch(Exception e){ 
      Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 
    } 
+2

您不能加载一个活动中的另一个,但你可以加载一个“片段” ......如果你要共享显示内容,使该共享元素融入您在包括“片段”这两个活动。 –

回答

3

而不是使用活动你需要你的片段,这就是他们的意思。

http://developer.android.com/guide/components/fragments.html

+0

我可以开始一个不同的活动作为片段? – ShanWave007

+0

不,但您可以将您的活动更改为片段。而不是让'public class Home扩展ActionBarActivity',你会得到'public class Home extends Fragment'。然后你将不得不从fragment类实现一些方法,而不是实现activity类中的一个。 – Distwo

+0

之后,我是否可以使用该片段实现的类与正常活动相同的扩展视图?我的意思是我可以做一些正常的活动,如设置点击列表到按钮,在实现片段后在同一类上绘制图表? – ShanWave007