Android导航绘制不显示图像和文本

时间:2015-06-06 14:17:12

标签: android navigation-drawer

我正在尝试创建一个显示文本和图像的导航绘图。我的项目构建正常,但是当我打开导航绘图时,没有显示文本或图像。我已经为导航绘图中显示的图像和文本创建了一个布局文件,我已经为导航绘图设置了适配器,并创建了自己的Adapter类。

我看不出有什么问题,但很明显是:)任何帮助都非常感谢。

这是我的文件

navigation_draw_items.xml

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

    <!-- THis file will contain the list of items in the navigation draw -->

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"></LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_gravity="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3"
        android:layout_gravity="left"/>

</LinearLayout>

activity_main.xml中

<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">
    <!-- The main content view which will be a fragment-->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  <!--  <LinearLayout
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
-->

    <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="#ffff"/>
   <!-- </LinearLayout> -->


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

AdapterClass.java

public class AdapterClass extends BaseAdapter {
    private Context context;
    private NavigationItem[] navigationItems;

    public AdapterClass(Context context, NavigationItem[] items)
    {
        this.context = context;
        navigationItems = items;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.navigation_draw_items, parent,false);

        }
        else
        {
             row = convertView;
        }

        TextView tvTitle = (TextView) row.findViewById(R.id.textView3);
        ImageView tvImage = (ImageView) row.findViewById(R.id.imageView);

        tvTitle.setText(navigationItems[position].stringTitle);
        tvImage.setImageResource(navigationItems[position].drawableIcon);
    return row;

    }
}

NavigationItem.java

public class NavigationItem {

    public NavigationItem(int stringTitle, int drawableIcon)
    {
        this.stringTitle = stringTitle;
        this.drawableIcon = drawableIcon;
    }

    public int stringTitle;

    public int drawableIcon;

}

MainActivity.java

public class MainActivity extends Activity implements EditListFragment.OnFragmentInteractionListener {

    //private String[] items;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

      public void onFragmentInteraction(String id)
    {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // To do
        AdapterClass navigationMenuAdapter;
        NavigationItem[] navigationMenuItems = {
                new NavigationItem(R.string.cat, R.drawable.cat),
                new NavigationItem(R.string.dog, R.drawable.dog),
                new NavigationItem(R.string.fish, R.drawable.fish),
        };

       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navigationMenuAdapter = new AdapterClass(this, navigationMenuItems);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerList.setAdapter(navigationMenuAdapter);
        mDrawerList.setOnItemClickListener(new DrawItemClickListener());

        // Old code
        //mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
        //items = getResources().getStringArray(R.array.items_array);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class DrawItemClickListener implements ListView.OnItemClickListener
    {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectedItem(position);
        }
    }

    private void selectedItem(int position)
    {
        switch (position)
        {
            case 0:
                //Fragment addList = ItemList
                //Fragment addList = new ItemList();
                //Bundle args = new Bundle();
                //args.putInt(ItemList.LIST_NUMBER, position);
                //addList.setArguments(args);
                Fragment addList = ItemList.newInstance();
                FragmentManager addListFragmentManager = getFragmentManager();
                addListFragmentManager.beginTransaction().replace(R.id.content_frame, addList).commit();
                //mDrawerLayout.closeDrawer(mDrawerLayout);
                break;
            case 1:
                Fragment editList = EditListFragment.newInstance();
                FragmentManager editListFragmentManager = getFragmentManager();
                editListFragmentManager.beginTransaction().replace(R.id.content_frame, editList).commit();
                break;
            case 2:
                Fragment deleteList = EditListFragment.newInstance();
                FragmentManager deleteListFragmentManager = getFragmentManager();
                deleteListFragmentManager.beginTransaction().replace(R.id.content_frame, deleteList).commit();
                break;
        }
    }
}

0 个答案:

没有答案