CAB不会显示在RecyclerView中

时间:2016-08-22 15:54:59

标签: android android-layout

我已经在现有的RecyclerView上添加了Cab的代码,在Android studio中它没有显示任何错误,而在模拟器中OnLongClick没有任何反应。任何帮助表示赞赏。 似乎代码什么也没做。

 public MusicAdapter(Context context, List<Music> music) {
        mMusic = new ArrayList<>();
        if(music != null) {
            mMusic.addAll(music);
        }
        mContext = context;
        mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp);
        // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes.
        int maxSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
        // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps.
        int cacheSize = maxSize / 8;
        mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) {

            @Override
            protected int sizeOf(Long key, Bitmap value) {
                // returns the size of bitmaps in kilobytes.
                return value.getByteCount() / 1024;
            }
        };
    }
    /**
     * Adds a {@link Music} item to the Adapter.
     * @param
     */
    /**
     * Adds a {@link List} of {@link Music} to the adapters.
     * This method replaces the current music items inside of the adapter with the specified music items.
     * @param
     */
    public void clearItem() {
        mMusic.clear();
    }
    public void addItems(List<Music> music) {
        // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items.
        mMusic.clear();
        // Add the new music list.
        mMusic.addAll(music);
        notifyItemRangeInserted(0, music.size());
    }
    /**
     * Clears the {@link Music} items inside of this adapter.
     */
    @Override
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.list_item, parent, false);
        MusicViewHolder musicViewHolder = new MusicViewHolder(v);
        return musicViewHolder;
    }

    @Override
    public void onBindViewHolder(MusicViewHolder holder, final int position) {
        final Music music = mMusic.get(position);
        holder.itemView.setLongClickable(true);
        // Check the Bitmap cache for the album art first..
        final Bitmap bitmap = mBitmapCache.get(music.getAlbumId());
        // If the bitmap is not null, then use the cached images.
        if(bitmap != null){
            holder.icon.setImageBitmap(bitmap);
        }
        else {
            // No album art could be found in the cache try reloading it.
            // In a real work example you should check that this value is not some junk value indicating that their is no album artwork.
            loadAlbumArt(holder.icon, music.getAlbumId());
        }

        holder.artist.setText(music.getArtist());
        holder.title.setText(music.getTitle());
        final Uri trackuri= ContentUris.withAppendedId(
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                                Intent intent = new Intent(mContext, Playrecord.class);
                                                intent.setData(trackuri);
                                                mContext.startActivity(intent);


                                            }
                                            });





    }



    /**
     * Helper method for asynchronously loading album art.
     * @param icon
     * @param albumId
     */
    public void loadAlbumArt(ImageView icon, long albumId) {
        // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id.
        if(cancelLoadTask(icon, albumId)) {
             // There was either no task running or it was loading a different image so create a new one to load the proper image.
            LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext);
            // Store the task inside of the async drawable.
            AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt);
            icon.setImageDrawable(drawable);
            loadAlbumArt.execute(albumId);
        }
    }

    /**
     * Helper method cancelling {@link LoadAlbumArt}.
     *
     * @param icon
     * @param albumId
     * @return
     */
    public boolean cancelLoadTask(ImageView icon, long albumId) {
        LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon);
        // If the task is null return true because we want to try and load the album art.
        if(loadAlbumArt == null) {
            return true;
        }
        if(loadAlbumArt != null) {
            // If the album id differs cancel this task because it cannot be recycled for this imageview.
            if(loadAlbumArt.albumId != albumId) {
                loadAlbumArt.cancel(true);
                return true;
            }
        }
        return false;
    }

    /**
     * Helper method for extracting an {@link LoadAlbumArt}.
     * @param icon
     * @return
     */
    public AsyncTask getLoadTask(ImageView icon) {
        LoadAlbumArt task = null;
        Drawable drawable = icon.getDrawable();
        if(drawable instanceof AsyncDrawable) {
            task = ((AsyncDrawable) drawable).getLoadArtworkTask();
        }
        return task;
    }

    public void remove(long itemId) {
    }


    private class LoadAlbumArt extends AsyncTask<Long, Void, Bitmap> {

        // URI that points to the AlbumArt database.
        private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart");
        public WeakReference<ImageView> mIcon;
        // Holds a publicly accessible albumId to be checked against.
        public long albumId;
        public Context mContext;
        int width, height;

        public LoadAlbumArt(ImageView icon, Context context) {
            // Store a weak reference to the imageView.
            mIcon = new WeakReference<ImageView>(icon);
            // Store the width and height of the imageview.
            // This is necessary for properly scalling the bitmap.
            width = icon.getWidth();
            height = icon.getHeight();
            mContext = context;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(isCancelled() || bitmap == null){
                return;
            }
            // Check to make sure that the imageview has not been garbage collected as well as the
            // LoadArtworkTask is the same as this one.
            if(mIcon != null && mIcon.get() != null) {
                ImageView icon = mIcon.get();
                Drawable drawable = icon.getDrawable();
                if(drawable instanceof AsyncDrawable) {
                    LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask();
                    // Make sure that this is the same task as the one current stored inside of the ImageView's drawable.
                    if(task != null && task == this) {
                        icon.setImageBitmap(bitmap);
                    }
                }
            }
            mBitmapCache.put(albumId, bitmap);
            super.onPostExecute(bitmap);
        }

        @Override
        protected Bitmap doInBackground(Long... params) {
            // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground.
            if(isCancelled()){
                return null;
            }
            albumId = params[0];
            // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist.
            Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId);
            Bitmap bmp = null;

            return bmp;
        }
    }
    /**
     * Custom drawable that holds a LoadArtworkTask
     */

    private static class AsyncDrawable extends BitmapDrawable {
        WeakReference<LoadAlbumArt> loadArtworkTaskWeakReference;

        public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) {
            super(resources, bitmap);
            // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected.
            loadArtworkTaskWeakReference = new WeakReference<LoadAlbumArt>(task);
        }

        public LoadAlbumArt getLoadArtworkTask() {
            return loadArtworkTaskWeakReference.get();
        }
    }

    @Override
    public int getItemCount() {
        return mMusic.size();
    }

    /**
     * Custom ViewHolder that represents the List Item.
     */
    public static class MusicViewHolder extends RecyclerView.ViewHolder  {

        ImageView icon;
        TextView title;
        TextView artist;

        public MusicViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            title = (TextView) itemView.findViewById(R.id.title);
            artist = (TextView)itemView.findViewById(R.id.subtitle);
        }



 }

音乐适配器

Xamarin.Android

1 个答案:

答案 0 :(得分:1)

感谢您上传适配器,问题是您在长项目单击侦听器上设置了recycleler视图本身而不是适配器上的视图,您需要做的是在适配器上创建一个接口来监听长时间点击就像你点击触发的意图一样,我将界面添加到你的适配器:

public class MusicAdaper {

    public interface onLongItemClickLLsitener {
        void onLonClick(View view);
    }

    private onLongItemClickLLsitener mCallback;

    public MusicAdapter(Context context, List<Music> music, onLongItemClickLLsitener listener) {

        mCallback = listener;
        mMusic = new ArrayList<>();
        if(music != null) {
            mMusic.addAll(music);
        }
        mContext = context;
        mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp);
        // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes.
        int maxSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
        // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps.
        int cacheSize = maxSize / 8;
        mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) {

            @Override
            protected int sizeOf(Long key, Bitmap value) {
                // returns the size of bitmaps in kilobytes.
                return value.getByteCount() / 1024;
            }
        };
    }
    /**
     * Adds a {@link Music} item to the Adapter.
     * @param
     */
    /**
     * Adds a {@link List} of {@link Music} to the adapters.
     * This method replaces the current music items inside of the adapter with the specified music items.
     * @param
     */
    public void clearItem() {
        mMusic.clear();
    }
    public void addItems(List<Music> music) {
        // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items.
        mMusic.clear();
        // Add the new music list.
        mMusic.addAll(music);
        notifyItemRangeInserted(0, music.size());
    }
    /**
     * Clears the {@link Music} items inside of this adapter.
     */
    @Override
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.list_item, parent, false);
        MusicViewHolder musicViewHolder = new MusicViewHolder(v);
        return musicViewHolder;
    }

    @Override
    public void onBindViewHolder(MusicViewHolder holder, final int position) {
        final Music music = mMusic.get(position);
        holder.itemView.setLongClickable(true);
        // Check the Bitmap cache for the album art first..
        final Bitmap bitmap = mBitmapCache.get(music.getAlbumId());
        // If the bitmap is not null, then use the cached images.
        if(bitmap != null){
            holder.icon.setImageBitmap(bitmap);
        }
        else {
            // No album art could be found in the cache try reloading it.
            // In a real work example you should check that this value is not some junk value indicating that their is no album artwork.
            loadAlbumArt(holder.icon, music.getAlbumId());
        }

        holder.artist.setText(music.getArtist());
        holder.title.setText(music.getTitle());
        final Uri trackuri= ContentUris.withAppendedId(
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId());

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mCallback.onLonClick(v);
                return false;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext, Playrecord.class);
                intent.setData(trackuri);
                mContext.startActivity(intent);


            }
        });





    }



    /**
     * Helper method for asynchronously loading album art.
     * @param icon
     * @param albumId
     */
    public void loadAlbumArt(ImageView icon, long albumId) {
        // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id.
        if(cancelLoadTask(icon, albumId)) {
            // There was either no task running or it was loading a different image so create a new one to load the proper image.
            LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext);
            // Store the task inside of the async drawable.
            AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt);
            icon.setImageDrawable(drawable);
            loadAlbumArt.execute(albumId);
        }
    }

    /**
     * Helper method cancelling {@link LoadAlbumArt}.
     *
     * @param icon
     * @param albumId
     * @return
     */
    public boolean cancelLoadTask(ImageView icon, long albumId) {
        LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon);
        // If the task is null return true because we want to try and load the album art.
        if(loadAlbumArt == null) {
            return true;
        }
        if(loadAlbumArt != null) {
            // If the album id differs cancel this task because it cannot be recycled for this imageview.
            if(loadAlbumArt.albumId != albumId) {
                loadAlbumArt.cancel(true);
                return true;
            }
        }
        return false;
    }

    /**
     * Helper method for extracting an {@link LoadAlbumArt}.
     * @param icon
     * @return
     */
    public AsyncTask getLoadTask(ImageView icon) {
        LoadAlbumArt task = null;
        Drawable drawable = icon.getDrawable();
        if(drawable instanceof AsyncDrawable) {
            task = ((AsyncDrawable) drawable).getLoadArtworkTask();
        }
        return task;
    }

    public void remove(long itemId) {
    }


    private class LoadAlbumArt extends AsyncTask<Long, Void, Bitmap> {

        // URI that points to the AlbumArt database.
        private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart");
        public WeakReference<ImageView> mIcon;
        // Holds a publicly accessible albumId to be checked against.
        public long albumId;
        public Context mContext;
        int width, height;

        public LoadAlbumArt(ImageView icon, Context context) {
            // Store a weak reference to the imageView.
            mIcon = new WeakReference<ImageView>(icon);
            // Store the width and height of the imageview.
            // This is necessary for properly scalling the bitmap.
            width = icon.getWidth();
            height = icon.getHeight();
            mContext = context;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(isCancelled() || bitmap == null){
                return;
            }
            // Check to make sure that the imageview has not been garbage collected as well as the
            // LoadArtworkTask is the same as this one.
            if(mIcon != null && mIcon.get() != null) {
                ImageView icon = mIcon.get();
                Drawable drawable = icon.getDrawable();
                if(drawable instanceof AsyncDrawable) {
                    LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask();
                    // Make sure that this is the same task as the one current stored inside of the ImageView's drawable.
                    if(task != null && task == this) {
                        icon.setImageBitmap(bitmap);
                    }
                }
            }
            mBitmapCache.put(albumId, bitmap);
            super.onPostExecute(bitmap);
        }

        @Override
        protected Bitmap doInBackground(Long... params) {
            // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground.
            if(isCancelled()){
                return null;
            }
            albumId = params[0];
            // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist.
            Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId);
            Bitmap bmp = null;

            return bmp;
        }
    }
    /**
     * Custom drawable that holds a LoadArtworkTask
     */

    private static class AsyncDrawable extends BitmapDrawable {
        WeakReference<LoadAlbumArt> loadArtworkTaskWeakReference;

        public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) {
            super(resources, bitmap);
            // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected.
            loadArtworkTaskWeakReference = new WeakReference<LoadAlbumArt>(task);
        }

        public LoadAlbumArt getLoadArtworkTask() {
            return loadArtworkTaskWeakReference.get();
        }
    }

    @Override
    public int getItemCount() {
        return mMusic.size();
    }

    /**
     * Custom ViewHolder that represents the List Item.
     */
    public static class MusicViewHolder extends RecyclerView.ViewHolder  {

        ImageView icon;
        TextView title;
        TextView artist;

        public MusicViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            title = (TextView) itemView.findViewById(R.id.title);
            artist = (TextView)itemView.findViewById(R.id.subtitle);
        }



    }
}

在你的活动中你必须做这样的事情:

MusicAdaper adaper = new MusicAdaper(this, new ArrayList<Music>(), new onLongItemClickLLsitener() {
            @Override
            public void onLonClick(View view) {
                // do something here
            }
        });