代码之家  ›  专栏  ›  技术社区  ›  Genaut

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

  •  0
  • Genaut  · 技术社区  · 10 年前

    我有一个自定义视图可用于抽屉导航:

    <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);
    

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

    主要活动.java http://pastebin.com/F7jpaNZr

    导航抽屉碎片.java http://pastebin.com/cEtD24zv

    2 回复  |  直到 10 年前
        1
  •  1
  •   Ali    10 年前

    以下xml显示了如何为“活动”设置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);
            }
        }
    }
    
        2
  •  0
  •   Genaut    10 年前

    我可以这样做:

    mDrawerListView=(ListView)getActivity().findViewById(R.id.lista_log);
    mDrawerListView=(ListView)容器.findViewById(R.id.lista_log);
    mDrawerListView=(ListView)getView().findViewById(R.id.lista_log);
    mDrawerListView = (ListView) mDrawerView.findViewById(R.id.lista_log);