Android - 用于实施活动更改的导航抽屉

时间:2017-02-13 11:12:34

标签: java android android-activity

我一直在阅读很多关于实现导航抽屉以在片段之间进行更改的问题和教程。对于这个问题,我不希望这些实现,因为我希望从一个活动更改为下一个活动。

我已经使用扩展ArrayAdapter<String>实现了自定义边栏适配器。这允许我的导航抽屉实现我自己的样式。请参阅下面的第一个代码段。

public class SidebarAdaptor extends ArrayAdapter<String> {

    Context context;
    int layoutResourceId;
    String data[] = null;

    public SidebarAdaptor(Context context, int layoutResourceId, String[] data)
    {
        // Initiate the ArrayAdapter
        super(context, layoutResourceId, data);

        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ControlHolder holder;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ControlHolder();
            TextView control = (TextView)row.findViewById(R.id.textItem);
            holder.control = control;

            row.setTag(holder);
        }
        else
        {
            holder = (ControlHolder)row.getTag();
        }

        String text = data[position];
        holder.control.setText(text);

        return row;
    }

    static class ControlHolder
    {
        TextView control;
    }

}

然后在我的onCreate类的MainActivity函数中设置此适配器。见下文。

private void customiseSidebar() {
        SidebarAdaptor sidebarAdaptor = new SidebarAdaptor(
                this,
                R.layout.side_bar_custom_row,
                new String[]{"Login", "Offline Access", "Register", "Forgotten Password"}
        );

        ListView listView1 = (ListView) findViewById(R.id.navList);

        listView1.setAdapter(sidebarAdaptor);
        listView1.setOnItemClickListener(new DrawerItemClickListener());
}

一旦用户按下导航抽屉中的任何选项,DrawerItemClickListener类就会保留要切换到的活动。请参阅下面的最终代码段。

package ap.classes;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import ap.ForgottenPassword;
import apMainActivity;
import ap.OfflineAccess;
import ap.RegisterAccount;

public class DrawerItemClickListener implements ListView.OnItemClickListener {

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

        }

            /** Swaps fragments in the main content view */
        private void selectItem(int position) {

            switch(position)
            {
                case 0:
                Intent intent = new Intent(MainActivity.this, OfflineAccess.class);
                startActivity(intent);
                break;
            case 1:
                Intent intent = new Intent(MainActivity.this, RegisterAccount.class);
                startActivity(intent);
                break;
            case 2:
                Intent intent = new Intent(MainActivity.this, ForgottenPassword.class);
                startActivity(intent);
                break;

        }
    }

现在我似乎有三个相当重要的问题:

  1. 第一个是以下错误消息:MainActivity is not an enclossing class
  2. 第二个说它; Cannont resolve method startActivity in android.content.Intent
  3. 最后一个问题是,只要我点击NavigationDrawer中的任何内容,抽屉就会关闭而不会注册任何按键。
  4. 谢谢Stackoverflow。

2 个答案:

答案 0 :(得分:1)

首先解决前两个问题:

  

第一个是以下错误消息:MainActivity不是一个包含类

     

第二个说它; Cannont解析android.content.Intent

中的方法startActivity

根据您提供的代码,您在拨打电话时不在Activity类的范围内

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

上面告诉编译器调用方法DrawerItemClickListener.startActivity(Intent intent),并且没有这样的方法。相反,您应该传递一个活动的实例并调用其 startActivty 方法。

第二个 MainActivity不是一个包含类因为使用 MainActivity.this 而抛出错误而不是传递上下文实例。

总结一下,你可以这样做:

public class DrawerItemClickListener implements ListView.OnItemClickListener {

    private Activity mActivity;
    public DrawerItemClickListener(Activity activity){
        mActivity = activity;
    }

        /** Swaps fragments in the main content view */
    private void selectItem(int position) {

        switch(position)
        {
            case 0:
            Intent intent = new Intent(mActivity, OfflineAccess.class);
            mActivity.startActivity(intent);
            break;
        /.../

    }
}

答案 1 :(得分:0)

是的,我遇到了类似的问题。虽然带有导航抽屉的碎片很容易但是使用Activity。您必须创建自定义导航抽屉。我提出了我的想法。enter code here

public class DrawerItemClickListener extends AppCompatActivity{

    private ListView mDrawerList;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private String mActivityTitle;
    private List<DrawerList> drawerListList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());

        drawerListList = new ArrayList<>();
        mDrawerList = (ListView) findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

    }

    protected abstract int getContentView();

    private void addDrawerItems() {
        drawerListList.add(new DrawerList("1", R.drawable.1));
        drawerListList.add(new DrawerList("2", R.drawable.2));
        drawerListList.add(new DrawerList("3", R.drawable.3));
        drawerListList.add(new DrawerList("4", R.drawable.4));
        drawerListList.add(new DrawerList("5", R.drawable.5));

        CustomDrawerAdapter adapter = new CustomDrawerAdapter(this, R.layout.list_view_menu, drawerListList);
        mDrawerList.setAdapter(adapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    if (id == 0) {
                        intent = new Intent(DrawerItemClickListener .this, FirstActivity.class);

                        startActivity(intent);
                        onDrawerClose();
                    } else if (id == 1) {
                        intent = new Intent(DrawerItemClickListener .this, SecondActivity.class);
                        startActivity(intent);
                        onDrawerClose();
                    } else if (id == 2) {

                        intent = new Intent(DrawerItemClickListener .this, ThirdActivity.class);

                        startActivity(intent);
                        onDrawerClose();
                    } else if (id == 3) {
                        intent = new Intent(DrawerItemClickListener .this, FouthActivity.class);
                        startActivity(intent);
                        onDrawerClose();
                    }
                }
            }
        });
    }

    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Navigation!");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    public void onDrawerClose() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            onBackPressed();
        }
    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @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();

        // Activate the navigation drawer toggle
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

自定义适配器位于

之下
public class CustomDrawerAdapter extends ArrayAdapter<DrawerList> {
    private Context context;
    private List<DrawerList> drawerItemList;
    private int layoutResID;

    public CustomDrawerAdapter(Context context, int layoutResourceID, List<DrawerList> listItems) {
        super(context, layoutResourceID, listItems);
        this.context = context;
        this.drawerItemList = listItems;
        this.layoutResID = layoutResourceID;
    }

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

        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            drawerHolder = new DrawerItemHolder();

            view = inflater.inflate(layoutResID, parent, false);
            drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_itemName);
            drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);

            view.setTag(drawerHolder);

        } else {
            drawerHolder = (DrawerItemHolder) view.getTag();
        }

        DrawerList dItem = this.drawerItemList.get(position);
        drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getMenuImage()));
        drawerHolder.ItemName.setText(dItem.getMenuName());

        return view;
    }

    private static class DrawerItemHolder {
        TextView ItemName;
        ImageView icon;
    }
}

你可以从每个活动中扩展它,比如

public class FirstActivity extends DrawerItemClickListener {
..
}

并且还在每个xml布局中添加了Listview。 希望它会有所帮助。