DrawerItemClickListener

DrawerItemClickListener

我正在设置DrawerItemClickListener,但是却收到一条错误消息:即使我在代码中进一步声明了该方法,也无法将DrawerItemClickListener解析为一种类型。我已经看了好几次了,但看不到做错了什么。

    private void _initMenu() {

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());

    }

    Handler responseHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(this, Player.class);
        intent.putExtra("id", video.getId());
        intent.putExtra("title", video.getTitle());
        intent.putExtra("uploader", video.getUploader());
        intent.putExtra("viewCount", video.getviewCount());
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class ImagePagerAdapter extends PagerAdapter {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        // this is your constructor
        public ImagePagerAdapter() {
            super();

        }

        private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn, R.drawable.country_up_btn,
                R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                R.drawable.island_up_btn, R.drawable.latin_up_btn,
                R.drawable.pop_up_btn, R.drawable.samba_up_btn };

        private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
        "EminemVEVO‎" };

        @Override
        public int getCount() {
            return mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }

        private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {

                drawerListView.setItemChecked(position, true);


                String text = "menu click... should be implemented";
                Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);

            }
        }
    }
}

最佳答案

您需要创建class DrawerItemClickListener implements ListView.OnItemClickListener
Android文档https://developer.android.com/training/implementing-navigation/nav-drawer.html#ListItemClicks中的完整示例和说明

片段:

private 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) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

关于java - DrawerItemClickListener无法解析为一种类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20572329/

10-12 06:13