导航抽屉中的onitemclicklistener无法在TabActivity中使用

时间:2014-12-23 16:52:21

标签: android listview navigation-drawer onitemclicklistener android-tabactivity

我的问题是onItemClickListener在我的导航抽屉中不起作用..我也在我的MainActivity中使用了TabActivity .. TabActivity中的所有其他东西都运行良好.. 我也试过setOnItemSelectedListener ..它也无法正常工作....提前致谢

activity_main.xml中

<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawerlayout"
    tools:context=".MainActivity" >

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

    </FrameLayout>

    <ListView android:id="@+id/listview"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#18FFFF">

    </ListView>

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

        </LinearLayout>

    </TabHost>

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

MainActivity.java

public class MainActivity extends TabActivity implements AdapterView.OnItemClickListener {

    ActionBar actionbar;
    private DrawerLayout dl;
    private ListView l;
    private String[] navimenu = {"Home", "Sell My Mobile", "Top 20 List", "Log Out"};
    private ActionBarDrawerToggle drawerlistener;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        dl = (DrawerLayout)findViewById(R.id.drawerlayout);
        l = (ListView)findViewById(R.id.listview);
        l.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navimenu));
        l.setOnItemClickListener(this);

        drawerlistener = new ActionBarDrawerToggle(this, dl, R.drawable.hi, R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(drawerView);
            }
        };

        dl.setDrawerListener(drawerlistener);
        actionbar = getActionBar();
        actionbar.setHomeButtonEnabled(true);
        actionbar.setDisplayHomeAsUpEnabled(true);

        // for Tab
        TabHost tabhost = getTabHost();

        TabHost.TabSpec newspec = tabhost.newTabSpec("New Phones");
        newspec.setIndicator("New Phones");
        Intent ni = new Intent(this, Newphone.class);
        newspec.setContent(ni);

        TabHost.TabSpec oldspec = tabhost.newTabSpec("Old Phones");
        oldspec.setIndicator("Old Phones");
        Intent oi = new Intent(this, Oldphone.class);
        oldspec.setContent(oi);

        TabHost.TabSpec sspec = tabhost.newTabSpec("Services");
        sspec.setIndicator("services");
        Intent si = new Intent(this, Services.class);
        sspec.setContent(si);

        tabhost.addTab(newspec);
        tabhost.addTab(oldspec);
        tabhost.addTab(sspec);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);

        drawerlistener.onConfigurationChanged(newConfig);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(drawerlistener.onOptionsItemSelected(item)){

            return true;

        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {

        super.onPostCreate(savedInstanceState);

        drawerlistener.syncState();

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

        Toast.makeText(this, "jello", Toast.LENGTH_SHORT).show();

        switch(position){

            case 0 :
                Intent home = new Intent(this, MainActivity.class);
                startActivity(home);

            case 1 :
                Intent smm = new Intent(this, Sellmymobile.class );
                startActivity(smm);

            case 2:
                Intent t2l = new Intent(this, Top2l.class);
                startActivity(t2l);

            case 3:
                Intent lo = new Intent(this, Login.class);
                startActivity(lo);

        }

        selectItem(position);

    }
    private void selectItem(int position) {

        l.setItemChecked(position, true);

        setTitle(navimenu[position]);

    }

    @Override
    public void setTitle(CharSequence title) {

        actionbar.setTitle(title);

    }

}

1 个答案:

答案 0 :(得分:1)

我做了一些改变。你可以看看这个。

activity_main.xml中

<LinearLayout
    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:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"/>

                </LinearLayout>

            </TabHost>

        </FrameLayout>

        <ListView
            android:id="@+id/listview"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#18FFFF">

        </ListView>

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

</LinearLayout>

MainActivity.java

public class MainActivity extends TabActivity implements AdapterView.OnItemClickListener {

    ActionBar actionbar;
    private DrawerLayout dl;
    private ListView l;
    private String[] navimenu = {"Home", "Sell My Mobile", "Top 20 List", "Log Out"};
    private ActionBarDrawerToggle drawerlistener;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        dl = (DrawerLayout)findViewById(R.id.drawerlayout);
        l = (ListView)findViewById(R.id.listview);

        l.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navimenu));

        l.setOnItemClickListener(this);

        drawerlistener = new ActionBarDrawerToggle(this, dl, R.drawable.hi, R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {

                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {

                super.onDrawerClosed(drawerView);

            }

        };

        dl.setDrawerListener(drawerlistener);

        actionbar = getActionBar();

        actionbar.setHomeButtonEnabled(true);
        actionbar.setDisplayHomeAsUpEnabled(true);

        // for Tab
        TabHost tabhost = getTabHost();

        TabHost.TabSpec newspec = tabhost.newTabSpec("New Phones");
        newspec.setIndicator("New Phones");
        Intent ni = new Intent(this, Newphone.class);
        newspec.setContent(ni);

        TabHost.TabSpec oldspec = tabhost.newTabSpec("Old Phones");
        oldspec.setIndicator("Old Phones");
        Intent oi = new Intent(this, Oldphone.class);
        oldspec.setContent(oi);

        TabHost.TabSpec sspec = tabhost.newTabSpec("Services");
        sspec.setIndicator("services");
        Intent si = new Intent(this, Services.class);
        sspec.setContent(si);

        tabhost.addTab(newspec);
        tabhost.addTab(oldspec);
        tabhost.addTab(sspec);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);

        drawerlistener.onConfigurationChanged(newConfig);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(drawerlistener.onOptionsItemSelected(item)){

            return true;

        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        drawerlistener.syncState();

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

        selectItem(position);

        dl.closeDrawer(l);

        switch(position){

            case 0 :{

                Intent home = new Intent(this, MainActivity.class);
                startActivity(home);

                break;
            }
            case 1 :{

                Intent smm = new Intent(this, Sellmymobile.class );
                startActivity(smm);

                break;
            }
            case 2:{

                Intent t2l = new Intent(this, Top2l.class);
                startActivity(t2l);

                break;
            }
            case 3:{

                Intent lo = new Intent(this, Login.class);
                startActivity(lo);

                break;
            }

        }

    }

    private void selectItem(int position) {

        l.setItemChecked(position, true);

        setTitle(navimenu[position]);

    }

    @Override
    public void setTitle(CharSequence title) {

        actionbar.setTitle(title);

    }

}

我希望它可以帮到你;)