android click button to display the secondary linkage menu of popupwindow

Turn from android click button to display the secondary linkage menu of popupwindow

android click button to display the secondary linkage menu of popupwindow
<div class="article-info-box">
    <div class="article-bar-top d-flex">
                                            <span class="time">2015 September 21, 2009:37:45</span>
        <div class="float-right">
            <span class="read-count">Reading: 5123</span>
                                        </div>
    </div>
</div>
<article>
    <div id="article_content" class="article_content csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
                <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/htmledit_views-0a60691e80.css">
        <div class="htmledit_views">

In the project, I met a two-level linkage menu of popupwindow, so I spent some time on it. Let's see the effect first. I took a simple picture when I tested it with a real machine.

Demo structure:


1, Layout file

1.activity.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/btn1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="display popupwindow" />  
  11.   
  12. </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "show popupwindow" / >

</RelativeLayout>

2.popupwindow_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"   
  6.     android:id="@+id/popupwindow">  
  7.   
  8.     <FrameLayout  
  9.         android:id="@+id/root_popupwindow"  
  10.         android:layout_width="0dp"  
  11.         android:layout_height="200dp"  
  12.         android:layout_weight="1" >  
  13.   
  14.         <ListView  
  15.             android:id="@+id/root_listview"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"   
  18.             android:background="#bdbdbd"  
  19.             android:cacheColorHint="#00000000"  
  20.             android:listSelector="#00000000"  
  21.             >  
  22.         </ListView>  
  23.     </FrameLayout>  
  24.   
  25.     <FrameLayout  
  26.         android:id="@+id/sub_popupwindow"  
  27.         android:layout_width="0dp"  
  28.          android:layout_height="200dp"  
  29.         android:layout_weight="1"  
  30.         android:background="#00000000"  
  31.         >  
  32.   
  33.         <ListView  
  34.             android:id="@+id/sub_listview"  
  35.             android:layout_width="match_parent"  
  36.             android:layout_height="match_parent"   
  37.             android:background="#ffffff"  
  38.             >  
  39.         </ListView>  
  40.     </FrameLayout>  
  41.   
  42. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:id="@+id/popupwindow">

    <FrameLayout
        android:id="@+id/root_popupwindow"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/root_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:background="#bdbdbd"
            android:cacheColorHint="#00000000"
            android:listSelector="#00000000"
            >
        </ListView>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/sub_popupwindow"
        android:layout_width="0dp"
         android:layout_height="200dp"
        android:layout_weight="1"
        android:background="#00000000"
        >

        <ListView
            android:id="@+id/sub_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:background="#ffffff"
            >
        </ListView>
    </FrameLayout>

</LinearLayout>

3.root_listview_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:id="@+id/root_item">  
  7.       
  8.       
  9.     <TextView  
  10.         android:gravity="center_vertical"  
  11.         android:layout_marginLeft="20dp"  
  12.         android:id="@+id/item_name_text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="40dp"  
  15.         android:text="20sp"  
  16.         ></TextView>  
  17.   
  18. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/root_item">


    <TextView
        android:gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:id="@+id/item_name_text"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="20sp"
        ></TextView>

</LinearLayout>

2, java code

1. Get the tool class of screen width and height

  1. package com.lql.multilistviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.Rect;  
  7. import android.util.DisplayMetrics;  
  8. import android.view.View;  
  9. import android.view.WindowManager;  
  10.   
  11. / 
  12.   Screen related functions╃cover 
  13.    
  14.   @author zhy 
  15.    
  16.  /  
  17. public class ScreenUtils  
  18. {  
  19.     private ScreenUtils()  
  20.     {  
  21.         / cannot be instantiated /  
  22.         throw new UnsupportedOperationException("cannot be instantiated");  
  23.     }  
  24.   
  25.     / 
  26.       Set the screen to work 
  27.        
  28.       @param context 
  29.       @return 
  30.      /  
  31.     public static int getScreenWidth(Context context)  
  32.     {  
  33.         WindowManager wm = (WindowManager) context  
  34.                 .getSystemService(Context.WINDOW_SERVICE);  
  35.         DisplayMetrics outMetrics = new DisplayMetrics();  
  36.         wm.getDefaultDisplay().getMetrics(outMetrics);  
  37.         return outMetrics.widthPixels;  
  38.     }  
  39.   
  40.     / 
  41.       Setting screen settings 
  42.        
  43.       @param context 
  44.       @return 
  45.      /  
  46.     public static int getScreenHeight(Context context)  
  47.     {  
  48.         WindowManager wm = (WindowManager) context  
  49.                 .getSystemService(Context.WINDOW_SERVICE);  
  50.         DisplayMetrics outMetrics = new DisplayMetrics();  
  51.         wm.getDefaultDisplay().getMetrics(outMetrics);  
  52.         return outMetrics.heightPixels;  
  53.     }  
  54.   
  55.     / 
  56.       Luan Feng gouzhong rudder�High temperature� 
  57.        
  58.       @param context 
  59.       @return 
  60.      /  
  61.     public static int getStatusHeight(Context context)  
  62.     {  
  63.   
  64.         int statusHeight = -1;  
  65.         try  
  66.         {  
  67.             Class<?> clazz = Class.forName("com.android.internal.R$dimen");  
  68.             Object object = clazz.newInstance();  
  69.             int height = Integer.parseInt(clazz.getField("status_bar_height")  
  70.                     .get(object).toString());  
  71.             statusHeight = context.getResources().getDimensionPixelSize(height);  
  72.         } catch (Exception e)  
  73.         {  
  74.             e.printStackTrace();  
  75.         }  
  76.         return statusHeight;  
  77.     }  
  78.   
  79.     / 
  80.       Set the current screen orFigure, includingPickaxe 
  81.        
  82.       @param activity 
  83.       @return 
  84.      /  
  85.     public static Bitmap snapShotWithStatusBar(Activity activity)  
  86.     {  
  87.         View view = activity.getWindow().getDecorView();  
  88.         view.setDrawingCacheEnabled(true);  
  89.         view.buildDrawingCache();  
  90.         Bitmap bmp = view.getDrawingCache();  
  91.         int width = getScreenWidth(activity);  
  92.         int height = getScreenHeight(activity);  
  93.         Bitmap bp = null;  
  94.         bp = Bitmap.createBitmap(bmp, 00, width, height);  
  95.         view.destroyDrawingCache();  
  96.         return bp;  
  97.   
  98.     }  
  99.   
  100.     / 
  101.       Set the current screen orFigure: not included�Chinese style 
  102.        
  103.       @param activity 
  104.       @return 
  105.      /  
  106.     public static Bitmap snapShotWithoutStatusBar(Activity activity)  
  107.     {  
  108.         View view = activity.getWindow().getDecorView();  
  109.         view.setDrawingCacheEnabled(true);  
  110.         view.buildDrawingCache();  
  111.         Bitmap bmp = view.getDrawingCache();  
  112.         Rect frame = new Rect();  
  113.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  114.         int statusBarHeight = frame.top;  
  115.   
  116.         int width = getScreenWidth(activity);  
  117.         int height = getScreenHeight(activity);  
  118.         Bitmap bp = null;  
  119.         bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height  
  120.                 - statusBarHeight);  
  121.         view.destroyDrawingCache();  
  122.         return bp;  
  123.   
  124.     }  
  125.   
  126. }  
package com.lql.multilistviewdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
*To obtain the screen's relevant forces
*
* @author zhy
*
*/
public class ScreenUtils
{
private ScreenUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}

/**
 * Set the screen to work
 * 
 * @param context
 * @return
 */
public static int getScreenWidth(Context context)
{
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}

/**
 * Setting screen settings
 * 
 * @param context
 * @return
 */
public static int getScreenHeight(Context context)
{
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.heightPixels;
}

/**
 * To obtain the height required
 * 
 * @param context
 * @return
 */
public static int getStatusHeight(Context context)
{

    int statusHeight = -1;
    try
    {
        Class&lt;?&gt; clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height")
                .get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e)
    {
        e.printStackTrace();
    }
    return statusHeight;
}

/**
 * When the current screen or image is displayed, it contains all the details
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithStatusBar(Activity activity)
{
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
    view.destroyDrawingCache();
    return bp;

}

/**
 * Setting the current screen or image does not include the status quo
 * 
 * @param activity
 * @return
 */
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;

    int width = getScreenWidth(activity);
    int height = getScreenHeight(activity);
    Bitmap bp = null;
    bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    return bp;

}

}


2.RootListViewAdapter.java (root ListView adapter)

  1. package com.lql.multilistviewdemo;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.graphics.Color;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.TextView;  
  12.   
  13. / 
  14.   Data adapter for the root of the secondary directory 
  15.   @author Administrator 
  16.   
  17.  /  
  18. public class RootListViewAdapter extends BaseAdapter {  
  19.   
  20.   
  21.     private Context context;  
  22.   
  23.     private LayoutInflater inflater;  
  24.       
  25.     private String[] items;  
  26.       
  27.     private int selectedPosition = -1;  
  28.       
  29.     public void setSelectedPosition(int selectedPosition) {  
  30.         this.selectedPosition = selectedPosition;  
  31.     }  
  32.   
  33.   
  34.     public RootListViewAdapter(Context context){  
  35.         this.context = context;  
  36.         inflater = LayoutInflater.from(context);  
  37.     }  
  38.       
  39.       
  40.       
  41.     public void setItems(String[] items) {  
  42.         this.items = items;  
  43.     }  
  44.   
  45.   
  46.   
  47.     @Override  
  48.     public int getCount() {  
  49.         // TODO Auto-generated method stub  
  50.         return items.length;  
  51.     }  
  52.   
  53.     @Override  
  54.     public Object getItem(int position) {  
  55.         // TODO Auto-generated method stub  
  56.         return null;  
  57.     }  
  58.   
  59.     @Override  
  60.     public long getItemId(int position) {  
  61.         // TODO Auto-generated method stub  
  62.         return 0;  
  63.     }  
  64.   
  65.     @Override  
  66.     public View getView(int position, View convertView, ViewGroup parent) {  
  67.         // TODO Auto-generated method stub  
  68.           
  69.         ViewHolder holder;  
  70.         if(convertView == null){  
  71.               
  72.             holder = new ViewHolder();  
  73.             convertView = (View)inflater.inflate(R.layout.root_listview_item, parent , false);  
  74.             holder.item_text =(TextView) convertView.findViewById(R.id.item_name_text);  
  75.             holder.item_layout = (LinearLayout)convertView.findViewById(R.id.root_item);  
  76.             convertView.setTag(holder);  
  77.         }else{  
  78.             holder = (ViewHolder)convertView.getTag();  
  79.         }  
  80.           
  81.         / 
  82.           Change the background color when this item is selected 
  83.          /  
  84.         if(selectedPosition == position){  
  85. //          Drawable item_bg = new ColorDrawable(R.color.sub_list_color);  
  86.             holder.item_layout.setBackgroundColor(Color.WHITE);  
  87.         }else{  
  88. //          Drawable item_bg = new ColorDrawable(R.color.sub_list_color);  
  89.             holder.item_layout.setBackgroundColor(Color.TRANSPARENT);  
  90.         }  
  91.         holder.item_text.setText(items[position]);  
  92.         return convertView;  
  93.     }  
  94.     class ViewHolder{  
  95.         TextView item_text;  
  96.         LinearLayout item_layout;  
  97.     }  
  98.   
  99. }  
package com.lql.multilistviewdemo;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
*Data adapter for the root of the secondary directory
* @author Administrator
*
*/
public class RootListViewAdapter extends BaseAdapter {

private Context context;

private LayoutInflater inflater;

private String[] items;

private int selectedPosition = -1;

public void setSelectedPosition(int selectedPosition) {
    this.selectedPosition = selectedPosition;
}


public RootListViewAdapter(Context context){
    this.context = context;
    inflater = LayoutInflater.from(context);
}



public void setItems(String[] items) {
    this.items = items;
}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder holder;
    if(convertView == null){

        holder = new ViewHolder();
        convertView = (View)inflater.inflate(R.layout.root_listview_item, parent , false);
        holder.item_text =(TextView) convertView.findViewById(R.id.item_name_text);
        holder.item_layout = (LinearLayout)convertView.findViewById(R.id.root_item);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }

    /**
     * Change the background color when this item is selected
     */
    if(selectedPosition == position){

// Drawable item_bg = new ColorDrawable(R.color.sub_list_color);
holder.item_layout.setBackgroundColor(Color.WHITE);
}else{
// Drawable item_bg = new ColorDrawable(R.color.sub_list_color);
holder.item_layout.setBackgroundColor(Color.TRANSPARENT);
}
holder.item_text.setText(items[position]);
return convertView;
}
class ViewHolder{
TextView item_text;
LinearLayout item_layout;
}

}


3.SubListViewAdapter.java

  1. package com.lql.multilistviewdemo;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.TextView;  
  10.   
  11. /* 
  12.   Data adapter for subdirectories of the secondary directory 
  13.   @author Administrator 
  14.   
  15.  */  
  16. public class SubListViewAdapter extends BaseAdapter {  
  17.   
  18.     private String[][] sub_items;  
  19.     private Context context;  
  20.     private int root_position;  
  21.     private LayoutInflater inflater;  
  22.   
  23.     public SubListViewAdapter(Context context, String[][] sub_items,  
  24.             int position) {  
  25.         this.context = context;  
  26.         inflater = LayoutInflater.from(context);  
  27.         this.sub_items = sub_items;  
  28.         this.root_position = position;  
  29.     }  
  30.   
  31.     @Override  
  32.     public int getCount() {  
  33.         // TODO Auto-generated method stub  
  34.         return sub_items[root_position].length;  
  35.     }  
  36.   
  37.     @Override  
  38.     public Object getItem(int position) {  
  39.         // TODO Auto-generated method stub  
  40.         return sub_items[root_position][position];  
  41.     }  
  42.   
  43.     @Override  
  44.     public long getItemId(int position) {  
  45.         // TODO Auto-generated method stub  
  46.         return 0;  
  47.     }  
  48.   
  49.     @Override  
  50.     public View getView(int position, View convertView, ViewGroup parent) {  
  51.         // TODO Auto-generated method stub  
  52.         ViewHolder holder;  
  53.         if (convertView == null) {  
  54.   
  55.             holder = new ViewHolder();  
  56.             convertView = (View) inflater.inflate(R.layout.root_listview_item,  
  57.                     parent, false);  
  58.             holder.item_text = (TextView) convertView  
  59.                     .findViewById(R.id.item_name_text);  
  60.             convertView.setTag(holder);  
  61.         } else {  
  62.             holder = (ViewHolder) convertView.getTag();  
  63.         }  
  64.         holder.item_text.setText(sub_items[root_position][position]);  
  65.         return convertView;  
  66.     }  
  67.     class ViewHolder{  
  68.         TextView item_text;  
  69.     }  
  70.   
  71. }  
package com.lql.multilistviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
*Data adapter for subdirectories of the secondary directory
* @author Administrator
*
*/
public class SubListViewAdapter extends BaseAdapter {

private String[][] sub_items;
private Context context;
private int root_position;
private LayoutInflater inflater;

public SubListViewAdapter(Context context, String[][] sub_items,
        int position) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    this.sub_items = sub_items;
    this.root_position = position;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return sub_items[root_position].length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return sub_items[root_position][position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if (convertView == null) {

        holder = new ViewHolder();
        convertView = (View) inflater.inflate(R.layout.root_listview_item,
                parent, false);
        holder.item_text = (TextView) convertView
                .findViewById(R.id.item_name_text);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.item_text.setText(sub_items[root_position][position]);
    return convertView;
}
class ViewHolder{
    TextView item_text;
}

}


4.MainActivity.java

  1. package com.lql.multilistviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.BitmapDrawable;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.ViewGroup.LayoutParams;  
  12. import android.widget.AdapterView;  
  13. import android.widget.Button;  
  14. import android.widget.FrameLayout;  
  15. import android.widget.LinearLayout;  
  16. import android.widget.ListView;  
  17. import android.widget.PopupWindow;  
  18. import android.widget.Toast;  
  19.   
  20. public class MainActivity extends Activity implements OnClickListener {  
  21.   
  22.     private Button showPopBtn;  
  23.   
  24.     / 
  25.       popupwindow 
  26.        
  27.      /  
  28.     private PopupWindow mPopupWindow;  
  29.   
  30.     / 
  31.       Root of the secondary menu 
  32.      /  
  33.     private ListView rootListView;  
  34.   
  35.     / 
  36.       Node at root 
  37.      /  
  38.     private String[] roots = new String[] { "Nearby ""Sort ""Screening " };  
  39.   
  40.     / 
  41.       Subdirectory node 
  42.      /  
  43.     private String[][] sub_items = new String[][] {  
  44.             new String[] { "Haidian District ""Xicheng District ""Shijingshan District ""Dongcheng District ""Chaoyang District " },  
  45.             new String[] { "Closest to me ""The most popular ""Best evaluated ""Lowest per capita ""Highest per capita " },  
  46.             new String[] { "All types ""Coupon merchant ""Flash merchants ""Appointment service ""Door to door service " } };  
  47.     private ListView subListView;  
  48.   
  49.     / 
  50.       Pop-up popupWindow layout 
  51.      /  
  52.     private LinearLayout popupLayout;  
  53.   
  54.     / 
  55.       Layout of subdirectories 
  56.      /  
  57.     private FrameLayout subLayout;  
  58.   
  59.     / 
  60.       Root selected node 
  61.      /  
  62.     private int selectedPosition;  
  63.   
  64.     @Override  
  65.     protected void onCreate(Bundle savedInstanceState) {  
  66.         super.onCreate(savedInstanceState);  
  67.         setContentView(R.layout.activity_main);  
  68.         showPopBtn = (Button) findViewById(R.id.btn1);  
  69.         showPopBtn.setOnClickListener(this);  
  70.     }  
  71.   
  72.     @Override  
  73.     public void onClick(View v) {  
  74.         // TODO Auto-generated method stub  
  75.         switch (v.getId()) {  
  76.         case R.id.btn1:  
  77.             showPopBtn(ScreenUtils.getScreenWidth(MainActivity.this),  
  78.                     ScreenUtils.getScreenHeight(MainActivity.this));  
  79.             break;  
  80.         default:  
  81.             break;  
  82.         }  
  83.     }  
  84.   
  85.     private void showPopBtn(int screenWidth, int screenHeight) {  
  86.         // TODO Auto-generated method stub  
  87.         LayoutInflater inflater = LayoutInflater.from(MainActivity.this);  
  88.         popupLayout = (LinearLayout) inflater.inflate(  
  89.                 R.layout.popupwindow_layout, nullfalse);  
  90.         rootListView = (ListView) popupLayout.findViewById(R.id.root_listview);  
  91.         final RootListViewAdapter adapter = new RootListViewAdapter(  
  92.                 MainActivity.this);  
  93.         adapter.setItems(roots);  
  94.         rootListView.setAdapter(adapter);  
  95.   
  96.         / 
  97.           son popupWindow 
  98.          /  
  99.         subLayout = (FrameLayout) popupLayout.findViewById(R.id.sub_popupwindow);  
  100.   
  101.         / 
  102.           Initialization subListview 
  103.          /  
  104.         subListView = (ListView) popupLayout.findViewById(R.id.sub_listview);  
  105.   
  106.         / 
  107.           Eject popupwindow When you click an item, the secondary menu will pop up again 
  108.          /  
  109.         subLayout.setVisibility(View.INVISIBLE);  
  110.   
  111.         / 
  112.           Initialization mPopupWindow 
  113.          /  
  114.         mPopupWindow = new PopupWindow(popupLayout, screenWidth,  
  115.                 LayoutParams.WRAP_CONTENT, true);  
  116.   
  117.         / 
  118.           Yes. mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); 
  119.           This sentence can make you click popupwindow Outside the area popupwindow Automatic disappearance But this sentence must be put in showAsDropDown before 
  120.          /  
  121.         mPopupWindow.setBackgroundDrawable(new BitmapDrawable());  
  122.   
  123.         / 
  124.           popupwindow The first parameter indicates under which control The second parameter represents the offset to the left and right, a positive number represents the offset to the left and a negative number represents the offset to the right 
  125.           The third parameter represents the offset in the up and down direction, a positive number represents the down offset, and a negative number represents the up offset 
  126.            
  127.          /  
  128.         mPopupWindow.showAsDropDown(showPopBtn, -55);//Show popwindow below control  
  129.   
  130.         mPopupWindow.update();  
  131.   
  132.         rootListView  
  133.                 .setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  134.   
  135.                     @Override  
  136.                     public void onItemClick(AdapterView<?> parent, View view,  
  137.                             int position, long id) {  
  138.                         // TODO Auto-generated method stub  
  139.   
  140.                         / 
  141.                           Selection root Change the ListView item Background color 
  142.                          /  
  143.                         adapter.setSelectedPosition(position);  
  144.                         adapter.notifyDataSetInvalidated();  
  145.                           
  146.                         selectedPosition = position;  
  147.   
  148.                         SubListViewAdapter subAdapter = new SubListViewAdapter(  
  149.                                 MainActivity.this, sub_items, position);  
  150.                         subListView.setAdapter(subAdapter);  
  151.   
  152.                         /* 
  153.                           When a root node is selected, the corresponding subdirectories are visible 
  154.                          */  
  155.                         subLayout.setVisibility(View.VISIBLE);  
  156.                         subListView  
  157.                                 .setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  158.   
  159.                                     @Override  
  160.                                     public void onItemClick(  
  161.                                             AdapterView<?> parent, View view,  
  162.                                             int position, long id) {  
  163.                                         // TODO Auto-generated method stub  
  164.                                         popupLayout.setVisibility(View.GONE);  
  165.                                         Toast.makeText(  
  166.                                                 MainActivity.this,  
  167.                                                 sub_items[selectedPosition][position],  
  168.                                                 Toast.LENGTH_SHORT).show();  
  169.                                     }  
  170.                                 });  
  171.   
  172.                     }  
  173.                 });  
  174.     }  
  175.   
  176. }  
package com.lql.multilistviewdemo;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private Button showPopBtn;

/**
 * popupwindow
 * 
 */
private PopupWindow mPopupWindow;

/**
 * Root of the secondary menu
 */
private ListView rootListView;

/**
 * Node at root
 */
private String[] roots = new String[] { "nearby", "sort", "screen" };

/**
 * Subdirectory node
 */
private String[][] sub_items = new String[][] {
        new String[] { "Haidian District", "Xicheng District", "Shijingshan District", "Dongcheng District", "Chaoyang District" },
        new String[] { "Closest to me", "Highest popularity", "Best evaluation", "Per capita minimum", "Per capita maximum" },
        new String[] { "All types", "Coupon merchant", "Flash merchants", "reservation service", "Door-to-door service" } };
private ListView subListView;

/**
 * Pop up popupWindow layout
 */
private LinearLayout popupLayout;

/**
 * Layout of subdirectories
 */
private FrameLayout subLayout;

/**
 * Root selected node
 */
private int selectedPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showPopBtn = (Button) findViewById(R.id.btn1);
    showPopBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn1:
        showPopBtn(ScreenUtils.getScreenWidth(MainActivity.this),
                ScreenUtils.getScreenHeight(MainActivity.this));
        break;
    default:
        break;
    }
}

private void showPopBtn(int screenWidth, int screenHeight) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    popupLayout = (LinearLayout) inflater.inflate(
            R.layout.popupwindow_layout, null, false);
    rootListView = (ListView) popupLayout.findViewById(R.id.root_listview);
    final RootListViewAdapter adapter = new RootListViewAdapter(
            MainActivity.this);
    adapter.setItems(roots);
    rootListView.setAdapter(adapter);

    /**
     * Child popupWindow
     */
    subLayout = (FrameLayout) popupLayout.findViewById(R.id.sub_popupwindow);

    /**
     * Initialize subListview
     */
    subListView = (ListView) popupLayout.findViewById(R.id.sub_listview);

    /**
     * When popupwindow pops up, the secondary menu is hidden by default. When an item is clicked, the secondary menu pops up again
     */
    subLayout.setVisibility(View.INVISIBLE);

    /**
     * Initializing the popupwindow
     */
    mPopupWindow = new PopupWindow(popupLayout, screenWidth,
            LayoutParams.WRAP_CONTENT, true);

    /**
     * With popupwindow. Setbackgrounddrawable (New bitmapdrawable());
     * This sentence can make the popupwindow disappear automatically when clicking on an area other than the popupwindow, but this sentence must be placed before showAsDropDown
     */
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

    /**
     * popupwindow The first parameter indicates under which control the second parameter indicates the offset to the left and right, a positive number indicates the offset to the left, a negative number indicates the offset to the right
     * The third parameter represents the offset in the up and down direction, a positive number represents the down offset, and a negative number represents the up offset
     * 
     */
    mPopupWindow.showAsDropDown(showPopBtn, -5, 5);// Show popwindow below control

    mPopupWindow.update();

    rootListView
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    /**
                     * Change the background color of the ListView item when root is selected
                     */
                    adapter.setSelectedPosition(position);
                    adapter.notifyDataSetInvalidated();

                    selectedPosition = position;

                    SubListViewAdapter subAdapter = new SubListViewAdapter(
                            MainActivity.this, sub_items, position);
                    subListView.setAdapter(subAdapter);

                    /**
                     * When a root node is selected, the corresponding subdirectories are visible
                     */
                    subLayout.setVisibility(View.VISIBLE);
                    subListView
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(
                                        AdapterView&lt;?&gt; parent, View view,
                                        int position, long id) {
                                    // TODO Auto-generated method stub
                                    popupLayout.setVisibility(View.GONE);
                                    Toast.makeText(
                                            MainActivity.this,
                                            sub_items[selectedPosition][position],
                                            Toast.LENGTH_SHORT).show();
                                }
                            });

                }
            });
}

}


There are two color items in the color file. It's very simple not to post them separately


Tags: Android xml encoding Java

Posted on Thu, 19 Mar 2020 11:57:24 -0400 by derrick24