2013-12-13 41 views
1

我正在設置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); 

      } 
     } 
    } 
} 

回答

0

移動DrawerItemClickListener類出ImagePagerAdapter類的如目前它是不可見的。

0

您的監聽器對象在ImagePagerAdapter類中定義,它的父類中將無法訪問。

可以移動DrawerItemClickListener到它的父類,那麼就應該是訪問:

private void _initMenu() { 

     drawerListView.setOnItemClickListener(new DrawerItemClickListener()); 
    } 

    private class DrawerItemClickListener implements ListView.OnItemClickListener { 

     @Override 
     public void onItemClick(AdapterView parent, View view, int position, long id) { 
      // do your thing 
     } 
    } 
1

您需要在Android的文檔https://developer.android.com/training/implementing-navigation/nav-drawer.html#ListItemClicks

片段創建class DrawerItemClickListener implements ListView.OnItemClickListener

完整的示例和解釋從它:

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); 
}