导航抽屉Android获取自定义视图

时间:2014-07-14 19:02:57

标签: android android-layout layout navigation-drawer

我在抽屉导航中使用自定义视图:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.extasis.musichunter.NavigationDrawerFragment" >

<EditText
    android:id="@+id/buscador"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text">

    <requestFocus />
</EditText>

<ListView
    android:id="@+id/lista_log"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccc"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

我的导航抽屉课上有这个,效果很好:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        //Creamos el LinearLayout
        mDrawerView = (LinearLayout) inflater.inflate(R.layout.fragment_navigation_drawer, container, false);

        return mDrawerView;


    }

我可以编译并尝试我的应用,但我想提取视图的子节点(在这种情况下,列表视图为变量)

View to extract:  android:id="@+id/lista_log"

尝试:

mDrawerListView = (ListView) getActivity().findViewById(R.id.lista_log);
mDrawerListView = (ListView) container.findViewById(R.id.lista_log);
mDrawerListView = (ListView) getView().findViewById(R.id.lista_log);

对不起我的英语,我希望你能理解我。感谢

MainActivity.java http://pastebin.com/F7jpaNZr

NavigationDrawerFragment.java http://pastebin.com/cEtD24zv

2 个答案:

答案 0 :(得分:1)

以下xml显示了如何为Activity设置navigationDrawer:

<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">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

活动中的onCreate()方法:

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  
                mDrawerLayout,         
                R.drawable.ic_drawer,  
                R.string.drawer_open,  
                R.string.drawer_close  
                ) 
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }
}

答案 1 :(得分:0)

我可以这样做:

<德尔>     mDrawerListView =(ListView)getActivity()。findViewById(R.id.lista_log);
    mDrawerListView =(ListView)container.findViewById(R.id.lista_log);
    mDrawerListView =(ListView)getView()。findViewById(R.id.lista_log);
mDrawerListView = (ListView) mDrawerView.findViewById(R.id.lista_log);