Android开发----Slidingmenu的相关代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发----Slidingmenu的相关代码相关的知识,希望对你有一定的参考价值。
main
package com.example.projec;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnClosedListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SlidingMenu menu=new SlidingMenu(getApplicationContext());
//设置布局
View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.menu_layout,null);
ListView listView=(ListView) view.findViewById(R.id.listv);
final String[] items=new String[]{"首页","关于","设置","搜索"};
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(getApplicationContext(), R.layout.main_tem,
R.id.textView1, new String[]{"首页","关于","设置","搜索"});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(items[arg2].equals("首页")){
getSupportFragmentManager().beginTransaction().replace(R.id.fr, new FragMain()).commit();
//切换到页面
menu.toggle();
}else{
FragTest02 fragTest02=new FragTest02(items[arg2]);
getSupportFragmentManager().beginTransaction().replace(R.id.fr,fragTest02).commit();
menu.toggle();
}
}
});
menu.setMenu(view);
//设置位置
menu.setMode(SlidingMenu.LEFT);
//设置滑动的屏幕范围,该设置为全屏区域都可以滑动
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
//设置SlidingMenu菜单的宽度
menu.setBehindWidth(300);
//设置viewpager与fragment防止冲突
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
//绑定activity
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
getSupportFragmentManager().beginTransaction().replace(R.id.fr, new FragMain()).commit();
}
}
_________________
fragmain
package com.example.projec;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("InflateParams")
public class FragMain extends Fragment implements OnClickListener{
ViewPager viewPager;
String[] mains=new String[]{"首页","排行","用户","搜索"};
List<Fragment> fragments;
TextView t1,t2,t3,t4;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.main_layout, null);
viewPager=(ViewPager) view.findViewById(R.id.viewpager);
t1=(TextView) view.findViewById(R.id.one);
t2=(TextView) view.findViewById(R.id.two);
t3=(TextView) view.findViewById(R.id.three);
t4=(TextView) view.findViewById(R.id.four);
t1.setOnClickListener(this);
t2.setOnClickListener(this);
t3.setOnClickListener(this);
t4.setOnClickListener(this);
t1.setBackgroundColor(Color.RED);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.WHITE);
getFrag();
return view;
}
private void getFrag() {
fragments=new ArrayList<Fragment>();
for(int i=0;i<mains.length;i++){
if(i==0){
FragTest03 fragTest03=new FragTest03();
fragments.add(fragTest03);
}else{
FragTest01 fragTest=new FragTest01(mains[i]);
fragments.add(fragTest);
}
}
FragmentPagerAdapter adapter=new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
};
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
if(arg0==0){
t1.setBackgroundColor(Color.RED);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.WHITE);
}else if(arg0==1){
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.RED);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.WHITE);
}else if(arg0==2){
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.RED);
t4.setBackgroundColor(Color.WHITE);
}else if(arg0==3){
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.RED);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.one:
viewPager.setCurrentItem(0);
t1.setBackgroundColor(Color.RED);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.WHITE);
break;
case R.id.two:
viewPager.setCurrentItem(1);
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.RED);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.WHITE);
break;
case R.id.three:
viewPager.setCurrentItem(2);
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.RED);
t4.setBackgroundColor(Color.WHITE);
break;
case R.id.four:
viewPager.setCurrentItem(3);
t1.setBackgroundColor(Color.WHITE);
t2.setBackgroundColor(Color.WHITE);
t3.setBackgroundColor(Color.WHITE);
t4.setBackgroundColor(Color.RED);
break;
default:
break;
}
}
}
----------------------
fragtext01
package com.example.projec;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragTest01 extends Fragment {
TextView textView;
String text;
public FragTest01(String text) {
super();
this.text = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.main_tem, null);
textView=(TextView) view.findViewById(R.id.textView1);
textView.setText(text);
return view;
}
}
------------------------
fragtext02
package com.example.projec;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragTest02 extends Fragment {
TextView textView;
String text;
public FragTest02(String text) {
super();
this.text = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.main_tem, null);
textView=(TextView) view.findViewById(R.id.textView1);
textView.setText(text);
return view;
}
}
---------
fragment03
package com.example.projec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragTest03 extends Fragment {
String[] tv=new String[]{"主页","关于","设置","搜索"};
String[] string=new String[]{"HAM火锅","朱师傅老母鸡滋补火锅","燕家厨房","海底捞"};
int[] image=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4};
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=LayoutInflater.from(getActivity()).inflate(R.layout.listitem, null);
listView=(ListView)view. findViewById(R.id.listmain);
SimpleAdapter simple_adapter=new SimpleAdapter(getActivity(),
getMenu(), R.layout.left_main, new String[]{"string","img"},new
int[]{R.id.leftadapter_tv,R.id.leftadapter_image});
listView.setAdapter(simple_adapter);
return view;
}
private List<Map<String, Object>> getMenu() {
// TODO Auto-generated method stub
List<Map<String,Object>> maplist=new ArrayList<Map<String,Object>>();
for (int i = 0; i < string.length; i++) {
Map<String,Object> map=new HashMap<String, Object>();
map.put("string", string[i]);
map.put("img",image[i]);
maplist.add(map);
}
return maplist;
}
}
----------------
layoutbuju
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fr"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.test.MainActivity" >
</FrameLayout>
---------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/leftadapter_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/leftadapter_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/leftadapter_image"
android:layout_alignTop="@+id/leftadapter_image"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/leftadapter_image"
android:gravity="center"
android:text="TextView" />
</RelativeLayout>
-----------------------------------------------
<?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/listmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<TextView
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="首页"
/>
<TextView
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="排行"
/>
<TextView
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="用户"
/>
<TextView
android:id="@+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="搜索"
/>
</LinearLayout>
</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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</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" >
<ListView
android:id="@+id/listv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
以上是关于Android开发----Slidingmenu的相关代码的主要内容,如果未能解决你的问题,请参考以下文章
使用SlidingMenu的方法以及Android Studio 如何导入第三方库SlidingMenu