2016-08-21 56 views
-3

我做了一个像facebook那样的滑动菜单,按照以下网站提供的教程: http://www.devexchanges.info/2016/05/creating-your-own-sliding-menu-like.html?m=1 现在在主布局中,我添加了一段文字,以便查看我使用ScrollView的一切。但是当我使用ScrollView时,我无法将菜单拉出来,只能通过单击按钮打开抽屉。请帮助我如何滚动浏览段落,也能够拉菜单抽屉。滑动菜单像Facebook中的材料设计主题在Android

在MainActivity XML文件:

<info.devexchanges.slidingmenu.SlidingLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/sliding_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- This holds our menu --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:scrollbars="vertical"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbarmenu" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#102961" 
     android:orientation="horizontal" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 


    <ListView 
     android:id="@+id/activity_main_menu_listview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbarmenu" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

<!-- This holds our content--> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@drawable/toolbar" 
     android:orientation="horizontal" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <ImageView 
      android:id="@+id/menu_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="@string/app_name" 
      android:onClick="toggleMenu" 
      android:src="@drawable/menu" /> 

     <TextView 
      android:id="@+id/title" 
      style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="@dimen/activity_horizontal_margin" 
      android:layout_marginStart="@dimen/activity_horizontal_margin" 
      android:gravity="center" 
      android:textColor="@android:color/white" /> 

    </android.support.v7.widget.Toolbar> 


    <!-- Fragments container layout --> 
    <FrameLayout 
     android:id="@+id/activity_main_content_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


    </FrameLayout> 

</LinearLayout> 

the_fiesta片段(家庭片段)的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@android:color/white"> 

<ScrollView 
    android:layout_width="800dp" 
    android:layout_height="fill_parent"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="75dp" 
    android:src="@drawable/fiesta_title" 
    android:layout_marginRight="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_alignParentTop="true" 
    android:id="@+id/imageView" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/the_fiesta" 
    android:id="@+id/textView" 
    android:textSize="17sp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_below="@+id/imageView" 
    android:layout_marginTop="15dp" 
    android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 

</ScrollView> 

MainActivity中的Java文件:

package info.devexchanges.slidingmenu; 
import android.annotation.SuppressLint; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 


public class MainActivity extends AppCompatActivity { 
// The SlidingLayout which will hold both the sliding menu and our main content 
// Main content will holds our Fragment respectively 
SlidingLayout slidingLayout; 

// ListView menu 
private ListView listMenu; 
private String[] listMenuItems; 

private Toolbar toolbar; 
private TextView title; //page title 
private ImageView btMenu; // Menu button 
private Fragment currentFragment; 

@SuppressLint("SetTextI18n") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Inflate the mainLayout 
    setContentView(R.layout.activity_main); 
    slidingLayout = (SlidingLayout) findViewById(R.id.sliding_layout); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    title = (TextView) findViewById(R.id.title); 
    setSupportActionBar(toolbar); 

    // Init menu 
    listMenuItems = getResources().getStringArray(R.array.menu_items); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    listMenu = (ListView) findViewById(R.id.activity_main_menu_listview); 
    listMenu.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listMenuItems)); 
    listMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      onMenuItemClick(parent, view, position, id); 
     } 

    }); 

    // handling menu button event 
    btMenu = (ImageView) findViewById(R.id.menu_icon); 
    btMenu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Show/hide the menu 
      toggleMenu(v); 
     } 
    }); 

    // Replace fragment main when activity start 
    FragmentManager fm = MainActivity.this.getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 

    TheFiesta fragment = new TheFiesta(); 
    ft.add(R.id.activity_main_content_fragment, fragment); 
    ft.commit(); 

    currentFragment = fragment; 
    title.setText("The Fiesta"); 
} 

public void toggleMenu(View v) { 
    slidingLayout.toggleMenu(); 
} 

// Perform action when a menu item is clicked 
private void onMenuItemClick(AdapterView<?> parent, View view, int position, long id) { 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    Fragment fragment; 

    if (position == 0) { 
     fragment = new TheFiesta(); 
     title.setText("The Fiesta"); 
    } else if (position == 1) { 
     fragment = new ListViewFragment(); 
     title.setText("Events"); 
    } else if (position == 2) { 
     fragment = new TextViewFragment(); 
     Bundle args = new Bundle(); 
     args.putString("KEY_STRING", "This is a TextView in the Fragment"); 
     fragment.setArguments(args); 
     title.setText("Schedule"); 
    } else if (position == 3) { 
     fragment = new ListViewFragment(); 
     title.setText("Schools"); 
    } else if (position == 4) { 
     fragment = new ListViewFragment(); 
     title.setText("Updates"); 
    } else if (position == 5) { 
     fragment = new ListViewFragment(); 
     title.setText("Our Team"); 
    } /*else if (position == 6) { 
     fragment = new ListViewFragment(); 
     title.setText("About Us"); 
    }*/ else { 
     fragment = new DummyFragment(); 
     title.setText("About Us"); 
    } 

    if(!fragment.getClass().equals(currentFragment.getClass())) { 
     // Replace current fragment by this new one 
     ft.replace(R.id.activity_main_content_fragment, fragment); 
     ft.commit(); 

     currentFragment = fragment; 
    } 

    // Hide menu anyway 
    slidingLayout.toggleMenu(); 

} 

@Override 
public void onBackPressed() { 
    if (slidingLayout.isMenuShown()) { 
     slidingLayout.toggleMenu(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    getSupportActionBar().setTitle(""); 
} 

}

+0

分享你的xml文件 –

+0

Ashish你想要的活动或片段的XML文件? –

+0

活动和片段。 –

回答

0

试试这个而不是Fragments容器。在 </android.support.v7.widget.Toolbar>以下添加此代码可能有效。

<LinearLayout 
    android:id="@+id/activity_main_content_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:id="@+id/scrollView" > 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:id="@+id/textView1" /> 

    </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
+0

Ashish当我使用这个我不能滑动菜单从左到右 –

+0

我想要片段滚动,也是菜单从左到右滑动,当我们拖动 –