2015-10-16 65 views
0

我有一個活動,當單擊菜單項時顯示抽屜佈局,因此其方向是從右到左。我設法使用下面的代碼顯示導航菜單。無法單擊右側至左側DrawerLayout項目

public class Forecast_details extends ActionBarActivity implements OnChartValueSelectedListener { 

.... 
@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     //return false; 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     if(id==R.id.Options){ 

      if (mDrawerlayout.isDrawerOpen(Gravity.END)){ 
        mDrawerlayout.closeDrawer(Gravity.END); 

       }else 
        mDrawerlayout.openDrawer(Gravity.END); 

     } 
     return super.onOptionsItemSelected(item); 

    } 

我的抽屜式導航欄包含一個靜態列表,所以我也懶得做一個列表視圖出來的,如下圖所示,

enter image description here

但是我不能點擊抽屜佈局內我的項目,或者視圖中的任何內容,甚至v4.widget.DrawerLayout在我的xml中都是不可點擊的。下面是我使用的兩個xml文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     ></include> 
     <android.support.v4.widget.DrawerLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/drawer_layout_right" 
      android:layout_gravity="end" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true"> 

      <include layout="@layout/nav_drawer_right" /> 

      <ScrollView 
       android:id="@+id/scrollView1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="2.36" > 

      .... 

       </LinearLayout> 
      </ScrollView> 
    </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

nav_drawer_right.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView_right" 
    android:layout_width="100dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="end" 
    android:background="#2c3e50" 
    android:paddingTop="20dp" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/container_fragment2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="1dp" > 

     </FrameLayout> 

      <LinearLayout 
       android:id="@+id/linearLayout_menuconatiner" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:clickable="true" 
       android:orientation="vertical" 
       android:paddingLeft="2dp" 
       android:paddingRight="2dp" > 

       <ImageView 
        android:id="@+id/imageView_forward" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/darkblue" 
        android:clickable="true" 
        android:onClick="SendClick" 
        android:padding="5dp" 
        android:scaleType="fitCenter" 
        android:src="@drawable/ic_forward" /> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="3dip" 
        android:background="@color/separatorcolor" 
        android:paddingLeft="2dp" 
        android:paddingRight="2dp" /> 

       <ImageView 
        android:id="@+id/imageView_request" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/darkblue" 
        android:padding="5dp" 
        android:src="@drawable/ic_request" /> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="3dip" 
        android:background="@color/separatorcolor" 
        android:paddingLeft="2dp" 
        android:paddingRight="2dp" /> 

       <ImageView 
        android:id="@+id/imageView_reloadvnet" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/darkblue" 
        android:padding="5dp" 
        android:src="@drawable/ic_updatevnet" /> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="3dip" 
        android:background="@color/separatorcolor" 
        android:paddingLeft="2dp" 
        android:paddingRight="2dp" /> 

       <ImageView 
        android:id="@+id/imageView_reloadvsms" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/darkblue" 
        android:padding="5dp" 
        android:src="@drawable/ic_updatevsms" /> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="3dip" 
        android:background="@color/separatorcolor" 
        android:paddingLeft="2dp" 
        android:paddingRight="2dp" /> 

       <ImageView 
        android:id="@+id/imageView_feedback" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/darkblue" 
        android:padding="5dp" 
        android:src="@drawable/ic_feedback" /> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="3dip" 
        android:background="@color/separatorcolor" 
        android:paddingLeft="2dp" 
        android:paddingRight="2dp" /> 

      </LinearLayout> 
     </RelativeLayout> 


</ScrollView> 

請幫幫忙。我嘗試了很多東西,使它象使用列表視圖,而不是工作,但我仍然沒有運氣..請不要幫忙,林新在使用抽屜佈局時,可能會出現一些我需要知道的內容..預先感謝您。

回答

0

爲了防止返回OnClickListener父級佈局需要在drawerLayout容器中設置android:clickable="true"

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

     <ScrollView 
      android:id="@+id/scrollView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="2.36" > 

      .... 

     </ScrollView> 

     <include layout="@layout/nav_drawer_right" /> 

</android.support.v4.widget.DrawerLayout> 

nav_drawer_right.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollView_right" 
android:layout_width="100dp" 
android:layout_height="match_parent" 
android:layout_gravity="end" 
android:background="#2c3e50" 
android:paddingTop="20dp" > 

    .... 

</ScrollView> 

在活動

public class MainActivity extends Activity{ 

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

    DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout_right); 
    ScrollView sV = (ScrollView) findViewById(R.id.scrollView1); 

    mDrawerLayout.openDrawer(Gravity.RIGHT); 

    sV.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      System.out.println("drawer click >>>>>>>>>>>>>>>>>>"); 
     } 
    }); 

    } 
}