将上下文菜单添加到Recycler视图

时间:2016-08-20 09:38:46

标签: android

我有两个类1st class Loader有recycleler视图,第二个类有onBinderViewHolder,所以问题是我应该在哪个类中注册上下文视图,哪个类应该声明上下文菜单类。任何帮助将不胜感激。

    public class LoaderActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Music>> {

        private RecyclerView mRecyclerView;
        private MusicAdapter musicAdapter;
        public static final String TAG = "YOUR-TAG-NAME";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            onContentChanged();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_loader);
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayShowCustomEnabled(true);

            LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.custom_imageview, null);

            actionBar.setCustomView(v);
            mRecyclerView = (RecyclerView) findViewById(R.id.recyclerVw);
            // Use a LinearLayoutManager to make the RecyclerView display the Music in a vertically scrolling list.
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            musicAdapter = new MusicAdapter(this, new ArrayList<Music>());
            mRecyclerView.setAdapter(musicAdapter);

            final LoaderManager supportLoaderManager = getSupportLoaderManager();
            supportLoaderManager.initLoader(1, null, this);
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }``
  @Override
    public Loader<List<Music>> onCreateLoader(int id, Bundle args) {
        return new MusicLoader(this);

    }
    @Override
    public void onLoadFinished(Loader<List<Music>> loader, List<Music> data) {
        // Add the newly loaded music to adapter.
        musicAdapter.clearItem();
        musicAdapter.addItems(data);

    }
    @Override
    public void onLoaderReset(Loader<List<Music>> loader) {
        // Clear the old music because a new list is going to be coming.
        musicAdapter.clearItem();

    }

MusicAdapter.java

public class MusicAdapter  extends RecyclerView.Adapter<MusicAdapter.MusicViewHolder>  {

    Context mContext;
    List<Music> mMusic;
    BitmapDrawable mPlaceholder;
    LruCache<Long, Bitmap> mBitmapCache;
    public Uri track;
    private int selectedPosition;


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

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


                                            }
                                            });


    }

0 个答案:

没有答案