如何使用基本适配器为Grid View项目创建上下文菜单?

时间:2019-05-20 15:24:37

标签: android contextmenu baseadapter

问题:

我想用网格视图制作视频缩略图淋浴。在右下角应该有三个点按钮,按下这些按钮可以打开上下文菜单。

  

更新的getView()

以下是来自YouTube应用的示例图片:

 YouTube sample image https://i.imgur.com/Uqvf6JD.jpg

我的代码:(我认为您仍然不需要代码)

OnCreate()

scrollView

BaseAdapter for GridView的我的getView()方法


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        search_bar=findViewById(R.id.toolbar_search_slot);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        MainActivity.this.setTitle(" ");
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_white_24dp);


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search_bar.setVisibility(View.GONE);

                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                getSupportActionBar().setHomeButtonEnabled(false);
            }
        });

checkpermission();

        videogridView=findViewById(R.id.video_grid_view);


        Thread thread =new GetAllMediaTasks();
        thread.start();







    }

item.xml


public class VideoItemAdapter extends BaseAdapter {
    private static final String TAG = "VideoItemAdapter";
    private Context context;
    private ArrayList<Video> videolist;
    ViewHolder viewHolder;
    private  String videoname;
    private String  videopath;
    private long videoduration;
    private Bitmap video_thumbnail_bitmap;


    public VideoItemAdapter(Context context,ArrayList<Video> videolist)
    {
        this.context=context;
        this.videolist=videolist;
    }



       @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        videoname = videolist.get(position).getVideoname();
        videopath = videolist.get(position).getVideopath();
        videoduration = videolist.get(position).getDuration();
        video_thumbnail_bitmap=videolist.get(position).getThumbnail();


        Log.d(TAG, "getView: videopath : "+videopath);




        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.video_item, parent,false);

            final ImageView imageView= (ImageView)convertView.findViewById(R.id.video_item_bg);
            final TextView nameTextView = (TextView)convertView.findViewById(R.id.video_name_txt);
            final TextView videodurationTextView = (TextView)convertView.findViewById(R.id.video_duration_txt);
            final Button optionsmenubtn = convertView.findViewById(R.id.video_item_options_btn);
            final CardView cardView = convertView.findViewById(R.id.card_view_video_item);

            final ViewHolder viewHolder = new ViewHolder(nameTextView,videodurationTextView,imageView,optionsmenubtn,cardView);
            convertView.setTag(viewHolder);
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
            int size = dm.widthPixels / 3;
            convertView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, size));

        }



        viewHolder = (ViewHolder)convertView.getTag();
        viewHolder.videodurationTextView.setText(msToString(videoduration));
        viewHolder.nameTextView.setText(videoname);

        Log.d(TAG, "getView: video path  :"+videolist.get(position).getVideopath()+" position "+position);

       // Bitmap bitmap3 = ThumbnailUtils.createVideoThumbnail(videopath, MediaStore.Video.Thumbnails.MINI_KIND);
        setThumbNailtoView(video_thumbnail_bitmap);

        // new MakeVideoThumbnail().execute(videopath);

        CardView cardView =viewHolder.cardView;
        Button optionsbutton = viewHolder.optionsmenubtn;




        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: clicked position : "+position);
                Intent intent =new Intent(context,VideoPlayer.class);
                Bundle bundle =new Bundle();
                bundle.putString("VideoPath",videopath);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });


        optionsbutton.setOnClickListener(new View.OnClickListener() {  // Three dot  button
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Clicked position : "+position, Toast.LENGTH_SHORT).show();
                v.setOnCreateContextMenuListener((Activity)context);

            }
        });

        //  viewHolder.cardView.setBackground(new BitmapDrawable(createThumbnailAtTime(videopath, 900)));

viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: clicked position : "+position);
        Intent intent =new Intent(context,VideoPlayer.class);
        Bundle bundle =new Bundle();
        bundle.putString("VideoPath",videopath);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
});

   return convertView;

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Toast.makeText(context, "Context Menu Clicked !", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Do whatever");
        doWhatever.setOnMenuItemClickListener(this);

    }
}
  

LogCat

Logcat

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

在item_xml文件中放置一个三个点的按钮,然后将OnClickListener设置为您的按钮,然后单击该按钮即可:

现在创建一个菜单xml文件并按如下所示进行设置,它应该可以很好地工作。

 viewHolder.navigicon.setOnClickListener(new View.OnClickListener() {    
        @Override
        public void onClick(View v) {  
            PopupMenu popupMenu = new PopupMenu(mContext,v);
            popupMenu.inflate(R.menu.list_menu);
            popupMenu.show();
        }
    });