Implement level-2 Menu

Source: Internet
Author: User

The requirements in the company's projects, in the lower left corner of the mall, require a Menu of classification.

Figure:

1. Click Level 1 category to modify the content in the Level 2 menu.

2. At the same time, the background in the level-1 menu changes to a white triangle background on the right.

3. When you click an entry in the level-2 menu, Popupwindow disappears and the product data of the corresponding item is updated on the interface.

My implementation idea is: Popupwindow pops up in the lower left corner, which uses two ListView implementations.

My Demo results:

1. Click the level-1 menu category to modify the content in the level-2 menu:

ListView. setOnItemClickListener (new OnItemClickListener (){

@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int position, long arg3 ){
Final int location = position;
MyAdapter. setSelectedPosition (position); // The method in the adapter to tell the adapter about the current position. The adapter sets the response position data based on position.
MyAdapter. notifyDataSetInvalidated ();
SubAdapter = new SubAdapter (MenuPop. this. context, cities,
Images, position );
SubListView. setAdapter (subAdapter );
SubListView. setOnItemClickListener (new OnItemClickListener () {// level-2 menu, which is modified according to level-1 menu data.

@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int position, long arg3 ){
Toast. makeText (MenuPop. this. context,
Cities [location] [position], Toast. LENGTH_SHORT)
. Show ();
}
});
}
});

2. Data settings in the adapter

// Set the selected effect (change the background image)
If (selectedPosition = position ){
Holder. textView. setTextColor (Color. BLUE );
Holder. layout. setBackgroundResource (R. drawable. fenlei_blue_on );
} Else {
Holder. textView. setTextColor (Color. WHITE );
Holder. layout. setBackgroundColor (Color. TRANSPARENT );
}

 

// Set the data. The data is now fixed in the String array.

If (position = 0 ){
Holder. imageView. setBackgroundResource (R. color. transparent );
} Else if (position = 1 ){
Holder. imageView. setBackgroundResource (R. color. transparent );

} Else {
Holder. imageView. setBackgroundResource (images [position]);

}

3. Class for generating Popupwindow. Use the new object to obtain the new Popupwindow.

// In the constructor, input the current context, initialize the dual ListView layout, and set the adapter.

Public MenuPop (Context context, SecondFragmentCallback callback ){
Super ();
This. context = context;
This. callback = callback;

View view = LayoutInflater. from (context)
. Inflate (R. layout. popmenu, null );

// Set listview
ListView = (MyListView) view. findViewById (R. id. listView );
SubListView = (MyListView) view. findViewById (R. id. subListView );
MyAdapter = new MyAdapter (context, foods, images );
ListView. setAdapter (myAdapter );
SelectDefult ();

 

// Popupwindow display Position Control

// Hide the Popupwindow menu

// ShowAsDropDown. The default position is in the lower left corner of the anchor. If there is no space in the lower left corner, it is displayed in the upper left corner.
Public void showAsDropUp (View parent ){
PopupWindow
. ShowAtLocation (parent, Gravity. LEFT | Gravity. BOTTOM, 0, 90 );
PopupWindow. showAsDropDown (parent, 0, 0 );

// Display position

// Aggregation
PopupWindow. setFocusable (true );
// Enable it to disappear after clicking
PopupWindow. setOutsideTouchable (true );
// Refresh status
PopupWindow. update ();
}

// Hide the menu
Public void dismiss (){
PopupWindow. dismiss ();
}

Layout file: Two ListView lists are arranged horizontally and side by side in Popupwindow;

The left image of ListView-item in the adapter and textview on the right.

**************************************** *

Custom MyListView: enables sub-threads and dynamically updates the UI.

Class MyListView extends ListView implements Runnable = inherits the ListView and implements the Runnable method.

// Main override method:

@ Override
Public boolean onTouchEvent (MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
If (mLastDownY = 0f & mDistance = 0 ){
MLastDownY = event. getY ();
Return true;
}
Break;

Case MotionEvent. ACTION_CANCEL:
Break;

Case MotionEvent. ACTION_UP:
If (mDistance! = 0 ){
MStep = 1;
MPositive = (mDistance> = 0 );
/**
* Post
*
* Causes the Runnable to be added to the message queue.
* Runnable will be run on the user interface thread.
* Trigger a subthread to be added to a message queue event. The sub-thread will run in the user interface thread. This method can be invoked
* From outside of the UI thread only when this View is attached
* To a window. This method is called from an external UI thread only when the View control is added to a form.
*/
This. post (this );
Return true;
}
MLastDownY = 0f;
MDistance = 0;
Break;

Case MotionEvent. ACTION_MOVE:
If (mLastDownY! = 0f ){
MDistance = (int) (mLastDownY-event. getY ());
If (mDistance <0 & getFirstVisiblePosition () = 0 & getChildAt (
0). getTop () = 0)
| (MDistance> 0 & getLastVisiblePosition () = getCount ()-1 )){
MDistance/= 2;
ScrollTo (0, mDistance );
Return true;
}
}
MDistance = 0;
Break;
}
Return super. onTouchEvent (event );
}

 

Note: At this stage, I am still proficient in code. I am not clear about many things. I need to learn too much.

 

Lines of the polar chaos: No strange feeling, just the wind.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.