How to realize this custom popup menu with Material Design Android?

前端 未结 7 2171
夕颜
夕颜 2020-12-29 04:01

I want to realize a custom popup menu like Twitter in Android for example with item and picture but I don\'t know what\'s the component used for that.

In Material

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 04:47

    I have same issue.but finally i found at my own solution that i am sharing you my code. Hope that will help you.

    popupWindowDogs = popupWindowDogs();
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // popupWindowDogs.showAsDropDown(v, -5, 0);
                popupWindowDogs().showAtLocation(v, Gravity.CENTER, 0, 0);
            }
        });
        // Detect touched area
        detector = new SimpleGestureFilter(this, this);
    

    }

    public PopupWindow popupWindowDogs()
    {
    
        // initialize a pop up window type
        PopupWindow popupWindow = new PopupWindow(this);
    
        // the drop down list is a list view
        final ListView listView = new ListView(this);
        // set our adapter and pass our pop up window contents
        listView.setAdapter(dogsAdapter(popUpContents));
        // listView.setBackgroundColor(Color.DKGRAY);
        listView.setBackgroundResource(R.drawable.ss4);
        listView.setPadding(0, 0, 0, 10);
        listView.setDivider(null);
        try {
    
            listView.setOnScrollListener(new OnScrollListener() {
    
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    View c = listView.getChildAt(0);
                    String cc = listView.getChildAt(0).toString();
                    int scrolly = -c.getTop() + listView.getFirstVisiblePosition() * c.getHeight();
                    /*
                     * Toast.makeText(getApplicationContext(), scrolly + "", Toast.LENGTH_SHORT)
                     * .show();
                     */}
    
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                        int totalItemCount) {
    
                }
            });
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString() + "", Toast.LENGTH_SHORT)
                    .show();
        }
        listView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView arg0, View v, int arg2,
                    long arg3) {
                try {
    
                    // TODO Auto-generated method stub
                    Context mContext = v.getContext();
                    Swipetouch mainActivity = ((Swipetouch) mContext);
                    // add some animation when a list item was clicked
                    Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(),
                            android.R.anim.fade_in);
                    fadeInAnimation.setDuration(10);
                    v.startAnimation(fadeInAnimation);
                    // dismiss the pop up
                    mainActivity.popupWindowDogs.dismiss();
                    // get the text and set it as the button text
                    String val = (String) arg0.getItemAtPosition(arg2);
                    // Toast.makeText(mContext, val, Toast.LENGTH_SHORT).show();
                    if (val.equals("Signup Now")) {
                        Intent ii = new Intent(getApplicationContext(), Registration.class);
                        startActivity(ii);
                        stopService(new Intent(Swipetouch.this, MyService.class));
                        stopService(new Intent(Swipetouch.this, MyService.class));
                    } else if (val.equals("Login")) {
                        Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                        startActivity(ii);
                        stopService(new Intent(Swipetouch.this, MyService.class));
    
                    } else if (val.equals("Exit")) {
                        finish();
                        stopService(new Intent(Swipetouch.this, MyService.class));
                    } else if (val.equals("Friends")) {
                        Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                        startActivity(ii);
                    } else if (val.equals("Exit")) {
                        stopService(new Intent(Swipetouch.this, MyService.class));
                        finish();
                    }
    
                } catch (Exception e) {
                    Toast.makeText(Swipetouch.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
    
        });
        // some other visual settings
        popupWindow.setFocusable(true);
        popupWindow.setWidth(250);
        // popupWindow.setHeight(300);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    
        // set the list view as pop up window content
        // SET WALLPAPER IMAGE
        /*
         * popupWindow.setBackgroundDrawable(getWallpaper()); popupWindow.setHeight(300);
         */
        // layout.setBackgroundResource(R.drawable.sshadow);
        // layout.setBackgroundColor(Color.TRANSPARENT);
        // popupWindow.setContentView(layout);
    
        popupWindow.setBackgroundDrawable(new ColorDrawable(
                android.graphics.Color.TRANSPARENT));
        popupWindow.setContentView(listView);
    
        return popupWindow;
    }
    

提交回复
热议问题