图标未显示在列表视图导航抽屉中

时间:2017-08-03 07:55:51

标签: android android-layout listview icons

我正在尝试在抽屉布局中实现导航抽屉。到目前为止,我已经创建了抽屉并夸大了布局,但问题是在导航抽屉的listview中,图标没有沿着标题显示。除此之外,listview项目也会显示在操作栏中的操作溢出中。

我还在所有onClick项上实施了listview。我已经以编程方式添加了项目和图标,但问题仍然存在。这些代码片段将帮助您清楚地了解我正在做的事情。

main_activity:

    private static String TAG=MainActivity.class.getSimpleName();
    ListView mDrawerList;
    RelativeLayout mDrawerPane;
    private ActionBarDrawerToggle mdrawerToggle;
    private DrawerLayout mDrawerLayout;
    ArrayList<NavItem> mNavItems=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavItems.add(new NavItem("Feed", "Checkout new stories",R.drawable.feed));
        mNavItems.add(new NavItem("Source", "Authentic Sources", R.drawable.source));
        mNavItems.add(new NavItem("Bookmarks","Saved Stories",R.drawable.bookmark));

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
        mDrawerList = (ListView) findViewById(R.id.nav_list);
        DrawerListAdapter adapter= new DrawerListAdapter(this,mNavItems);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
              selectItemFromDrawer(position);
          }

          });
        mdrawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.navigation_drawer_open,R.string.navigation_drawer_close){
            @Override
            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }
            @Override
            public void onDrawerClosed(View drawerView){
                super.onDrawerClosed(drawerView);
                Log.d(TAG, "onDrawerClosed: " + getTitle());
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.addDrawerListener(mdrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
      private void selectItemFromDrawer(int position){
          Fragment fragment=new preferenceFragment();
          FragmentManager fragmentManager = getSupportFragmentManager();
          fragmentManager.beginTransaction().replace(R.id.main_content,fragment).commit();
          mDrawerList.setItemChecked(position,true);
          setTitle(mNavItems.get(position).mtitle);

          mDrawerLayout.closeDrawer(mDrawerPane);
      }


    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(mdrawerToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mdrawerToggle.syncState();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {


        return false;
    }


    class NavItem{
        String mtitle;
        String mSubtitle;
        int mIcon;
        public NavItem(String title, String subtitle, int icon){

            mtitle=title;
            mSubtitle=subtitle;
            mIcon=icon;

        }

    }
}

抽屉列表适配器:

public class DrawerListAdapter extends BaseAdapter {

    Context mcontext;
    ArrayList<MainActivity.NavItem> mNavItems;

    public DrawerListAdapter(Context context, ArrayList<MainActivity.NavItem> navItems){
        mcontext=context;
        mNavItems=navItems;
    }
    @Override
    public int getCount() {
        return mNavItems.size();
    }

    @Override
    public Object getItem(int position  ) {
        return mNavItems.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if(convertView==null){

            LayoutInflater inflater=(LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.drawer_item,null);
        }
        else{
            view=convertView;
        }
        TextView titleView=(TextView)view.findViewById(R.id.title);
        TextView subtitleView=(TextView) view.findViewById(R.id.subtitle);
        ImageView iconView=(ImageView) view.findViewById(R.id.icon);

        titleView.setText(mNavItems.get(position).mtitle);
        subtitleView.setText(mNavItems.get(position).mSubtitle);
        iconView.setImageResource(mNavItems.get(position).mIcon);
        return view;
    }
}

主要布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dpl_it.m.hamzam.reader.MainActivity"
    tools:openDrawer="start" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_content"/>
    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">
        <RelativeLayout
            android:id="@+id/profileBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/colorPrimaryDark"
            android:padding="8dp" >
        <ImageView
            android:id="@+id/avatar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher"
            android:layout_marginTop="15dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:orientation="vertical"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/avatar">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/username"
                android:text="Dabir"
                android:textColor="#fff"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/description"
                android:text="View Profile"
                android:textColor="#fff"
                android:textSize="12sp"
                android:layout_gravity="bottom"
                android:layout_marginTop="4dp"/>
        </LinearLayout>
    </RelativeLayout>
        <ListView
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/nav_list"
        android:layout_below="@+id/profileBox"
        android:choiceMode="singleChoice"
        android:background="#fff">

        </ListView>

</RelativeLayout>

</android.support.v4.widget.DrawerLayout>
抽屉物品的

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/icon"
        android:src="@mipmap/ic_launcher"
       android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:textSize="18sp"
        android:gravity="center_vertical"
        android:textColor="@color/textcolor"
        android:text="line 1"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/icon"
        android:layout_toEndOf="@+id/icon" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="line 2"
        android:id="@+id/subtitle"
        android:layout_toRightOf="@+id/icon"
        android:layout_below="@+id/title"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="@color/textcolor"/>

</RelativeLayout>

0 个答案:

没有答案