SlidingMenu framework loads ListView layout menu

A lot of times, we just envy others and think we can’t do it, so we don’t do it, and we don’t understand it.

···························

===================================================================================

#use#

Now similar to QQ, and even the built-in gesture sliding menu of our Android phones is not rare. In addition to this cool effect, it also greatly improves the user experience and greatly saves space. Maybe we can't do it yet. So good-looking, then today we will implement the basic slidingmenu, maybe you will say that learning it needs to use GestureDetector and will not handle it. It doesn't matter. There is a SlidingMenu written on the Internet. The key is to see how we configure and use it. I also encountered a lot of trouble when using it, so let's start from the beginning.

===================================================================================

#Import slidingmenu package#

Slidingmenu.jar is uploaded in the attachment, you can download and import libs by yourself



 All is ready except for the opportunity

=================================================================================

Joining slidingmenu is very easy

First create it directly in oncreate:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		/*
		 * slidingmenu configuration
		 */
		SlidingMenu menu = new SlidingMenu(this);
		menu.setMode(SlidingMenu.LEFT);//Set to swipe on the left
		// Set the touch screen mode
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//The slidingmenu can be called up by swiping the full screen gesture
		menu.setShadowWidthRes(R.dimen.shadow_width);//Set the shadow effect range
		menu.setShadowDrawable(R.drawable.shadow);//Shadow effect

		// Set the width of the sliding menu view
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// menu.setBehindWidth(200);
		// Set the value of the fade-in and fade-out effect
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		// Set the layout for the side slide menu
		menu.setMenu(getLeftMenu());
	}

 The last sentence setMenu is to get the configured View

Of course, there is another method, which is to manually configure the layout to be loaded, for example,

 and then write it as menu.setMenu(R.layout.left_menu);

In this way, the layout file you have configured is loaded directly, and the display is also displayed.

But once you add something to xml in the code, the menu will not respond, that is to say, it will not be displayed, so what you get is just a dead layout, because what I want to slide out when I do is a ListView, the fact In the above, many slide-out menus, QQ, and WeChat are basically Listview, but I didn’t know it at first, it was written in a dead layout, and then I edited the ListView in the code. , just teaching how to configure slidingmenu, and no one uses dynamic layout. Therefore, after asking the great god, I realized that I had written the layout to death. Add the method of getMenu to dynamically edit the ListView. Now the xml of the menu is like this

<?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" >

    <ListView
        android:id="@+id/left_menu_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

 Layout preview:



 =================================================================================

【Edit ListView】

public View getLeftMenu() {
		//Calling another layout file from an Activity bound to the main layout file must call LayoutInflater
		LayoutInflater inflater = getLayoutInflater();
		//Get the menu's View
		View v = inflater.inflate(R.layout.left_menu, null);
		ListView listview = (ListView) v.findViewById(R.id.left_menu_listview);
		listview.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, getData()));
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				//Listen for listview item click
			}
		});
		return v;
	}
	private List<String> getData() {

		List<String> data = new ArrayList<String>();
		data.add("Test data 1");
		data.add("Test data 2");
		data.add("Test data 3");
		data.add("Test data 4");
       
		return data;
	}

 The overall effect is as follows:



 

 

The link has the entire project project file. You can download this demo for in-depth analysis. For reference

 

Download link: http://download.csdn.net/detail/u012763405/9616554

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326672295&siteId=291194637