Android:使用ActionBarActivity向Listview添加图标

Android:使用ActionBarActivity向Listview添加图标,android,listview,android-actionbar,navigation-drawer,Android,Listview,Android Actionbar,Navigation Drawer,我使用了navigtion抽屉,您可以在首次设置应用程序时选择。当你第一次创建应用程序时,你可以选择导航类型,我选择了Activobar Activity 这将使用片段和一个类似于以下内容的listview xml文件 <ListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layou

我使用了navigtion抽屉,您可以在首次设置应用程序时选择。当你第一次创建应用程序时,你可以选择导航类型,我选择了Activobar Activity

这将使用片段和一个类似于以下内容的listview xml文件

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#333"
    android:choiceMode="singleChoice"
    android:divider="#404040"
    android:dividerHeight="1dp"
    tools:context="com.appname.NavigationDrawerFragment" />
下面是mainactivity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.Thenewboston.MainActivity" >


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

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.Thenewboston.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

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


如何向列表中的每个项目添加图标?

此listview将有适配器,您可以根据您在适配器的getView中的要求自定义视图…

我添加了适配器,你能告诉我在哪里做这件事吗?如果你必须在组视图中设置,在组视图的getView中进行更改,或者如果你必须在子视图中设置,在子视图getView中进行更改,你必须在哪里设置图标。我不明白。我在NavigationDrawerFragmentI中找不到getView。虽然在组getView()方法中进行了更改,但我记不起您的代码。getView方法为emptysee
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1)).commit();

        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    }

    public void onSectionAttached(int number) {

        switch (number) {
        case 0:
            mTitle = getString(R.string.title_section1);
            break;
        case 1:
            mTitle = getString(R.string.title_section2);
            break;
        case 2:
            mTitle = getString(R.string.title_section3);
            break;
        case 3:
            mTitle = getString(R.string.title_section4);
            break;
        case 4:
            mTitle = getString(R.string.title_section5);
            break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        // actionBar.setTitle(R.string.app_name);

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setIcon(R.drawable.tnb);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
//          getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment implements OnClickListener {


        private static final String ARG_SECTION_NUMBER = "section_number";

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

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        }

        public static PlaceholderFragment newInstance(int sectionNumber) {

            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle bundle = new Bundle();

            switch (sectionNumber) {
            case 1:
                bundle.putInt(ARG_SECTION_NUMBER, 05);
                break;
            case 2:
                bundle.putInt(ARG_SECTION_NUMBER, 16);
                break;
            case 3:
                bundle.putInt(ARG_SECTION_NUMBER, 1991);
                break;
            }

            fragment.setArguments(bundle);
            return fragment;

        }

        public PlaceholderFragment() {}

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }


        }
    }
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.Thenewboston.MainActivity" >


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

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.Thenewboston.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

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