如何在滑动菜单中显示列表视图?

时间:2014-05-21 07:53:58

标签: android android-listview

我有一个主要活动和下面的滑动菜单我想在滑动菜单中添加一个列表项我认为我的代码是正确的但是列表仍未在滑动部分显示:

package com.example.example;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
public class ExampleActivity extends Activity {

    private ListView mainMenu ;  
    private ArrayAdapter<String> listAdapter ;  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set the content view
        setContentView(R.layout.activity_example);
        View v = getLayoutInflater().inflate(R.layout.sliding_menu, null,true);
       // setBehindContentView(v);

        // Find the ListView resource.     
        mainMenu = (ListView) v.findViewById( R.id.exampleMenu );


        // Create and populate a List of menu items names.  
        String[] menuText = new String[] { "Top 20", "League"};    
        ArrayList<String> menuList = new ArrayList<String>();  
        menuList.addAll( Arrays.asList(menuText) );  

        // Create ArrayAdapter using the menu list.  
        listAdapter = new ArrayAdapter<String>(this, R.layout.menu_item, menuList);  

        // Set the ArrayAdapter as the ListView's adapter.  
        mainMenu.setAdapter( listAdapter ); 

        // configure the SlidingMenu
        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.sliding_menu);

    }


}

我是android新手我只是想知道我在这里想念一下吗?因为该应用程序正在运行,但菜单未显示。我应该在代码中添加一些内容吗?

编辑:滑动菜单lib的SlidingMenu.java中描述的setMenu:

/**
     * Set the behind view (menu) content from a layout resource. The resource will be inflated, adding all top-level views
     * to the behind view.
     *
     * @param res the new content
     */
    public void setMenu(int res) {
        setMenu(LayoutInflater.from(getContext()).inflate(res, null));
    }

    /**
     * Set the behind view (menu) content to the given View.
     *
     * @param view The desired content to display.
     */
    public void setMenu(View v) {
        mViewBehind.setContent(v);
    }

    /**

1 个答案:

答案 0 :(得分:0)

我对Sliding菜单库不太熟悉,但我从代码中看到的是这两行是你问题的原因

setBehindContentView(R.layout.sliding_menu);
View v = getLayoutInflater().inflate(R.layout.sliding_menu, null);

他们创建两个不同的视图,一个用于actulay布局渲染,另一个用于创建和使用listview设置数据但从未添加到活动内容。

我希望如果你改变这两行,这样的事情可能会起作用

View v = getLayoutInflater().inflate(R.layout.sliding_menu, null);
//set this v to setMenu instead of R.layout.sliding_menu
//your codes......
//
//
//menu.setMenu(R.layout.sliding_menu);
menu.setMenu(v);