无法使用supportv7修改导航抽屉

时间:2014-09-03 14:00:07

标签: java android navigation-drawer android-support-library android-actionbar-compat

我会使用supportv7库实现导航抽屉,所以我将使用应用程序,即使是2.3 Android ..我不能这样做..我按照一些例子但仍然有错误..这是我到目前为止所写的:

public class MainActivity extends ActionBarActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    CustomDrawerAdapter adapter;

    List<DrawerItem> dataList;

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

        /** Customizzo la actionbar */
        ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.actionbarcustom);
        setTitle("Hi");
        int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        if (actionBarTitleId > 0) {
            TextView title = (TextView) findViewById(actionBarTitleId);
            if (title != null) {
                title.setTextColor(Color.WHITE);
            }
        }

        // Initializing
        dataList = new ArrayList<DrawerItem>();
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);


        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        dataList.add(new DrawerItem("Hi All")); // adding a header to the list
        dataList.add(new DrawerItem("News", R.drawable.news));
        dataList.add(new DrawerItem("Second Tab", R.drawable.arrow));
        dataList.add(new DrawerItem("Third tab", R.drawable.high_importance));
        dataList.add(new DrawerItem("4 tab", R.drawable.star));
        dataList.add(new DrawerItem("5 Tab", R.drawable.spam));
        dataList.add(new DrawerItem("6 Tab", R.drawable.group));

        adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
                dataList);

        mDrawerList.setAdapter(adapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); 
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); 
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {

            if (dataList.get(0).isSpinner()
                    & dataList.get(1).getTitle() != null) {
                SelectItem(2);
            } else if (dataList.get(0).getTitle() != null) {
                SelectItem(1);
            } else {
                SelectItem(0);
            }
        }

    }

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

    public void SelectItem(int possition) {

        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (possition) {
        case 1:
            fragment = new FragmentThree();
            break;  
        case 2:
            fragment = new FragmentOne();
            break;
        case 3:
            fragment = new FragmentOne();
            break;
        case 4:
            fragment = new FragmentOne();
            break;
        case 5:
            fragment = new FragmentOne();
            break;
        case 6:
            fragment = new FragmentTwo();

            break;

        default:
            break;
        }

        fragment.setArguments(args);
        FragmentManager frgManager = getSupportFragmentManager();
        FragmentTransaction ft = frgManager.beginTransaction();
        ft.replace(R.id.content_frame, fragment)
                .commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);

    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        switch (item.getItemId()){
            case R.id.action_settings:      
                Intent i = new Intent(MainActivity.this, settings.class);
                startActivity(i);           
                return true;
            }   

        return false;
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            if (dataList.get(position).getTitle() == null) {
                SelectItem(position);
            }

        }
    }

  }

这些是进口商品:

import android.app.*;
import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.os.*;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.*;
import android.support.v4.widget.DrawerLayout;
import android.view.*;
import android.widget.*;
import com.readystatesoftware.systembartint.*;
import java.util.*;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.*;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

错误在以下几行:

fragment.setArguments(args);
    FragmentManager frgManager = getSupportFragmentManager();
    FragmentTransaction ft = frgManager.beginTransaction();
    ft.replace(R.id.content_frame, fragment)
            .commit();

似乎该方法不适用于android.app.Fragment ..我试图在各方面都没有成功..还有其他的东西要改变吗?

2 个答案:

答案 0 :(得分:0)

您需要使用支持库中的Fragment类。 SupportFragmentManager适用于android.support.v4.app.Fragment,而不适用于android.app.Fragment

答案 1 :(得分:0)

你需要特别小心使用外卡。在您的情况下,导入android.app。*会导致问题,因为android.app.Fragment是包的一部分。

您需要确保导入android.support.v4.app.Fragment,以匹配FragmentTransaction.replace(int, Fragment)的参数。