xutils3和tablayout 须要加的 在gradle里边 compile 'org.xutils:xutils:3.5.0' compile 'com.android.support:design:25.3.1'侧拉须要导包
package com.example.lixin.todaynews; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import com.example.lixin.todaynews.adapter.MyPageAdapter; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private SlidingMenu slidingMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout); ImageView sliding_menu = (ImageView) findViewById(R.id.sliding_menu); sliding_menu.setOnClickListener(this); ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(new MyPageAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); slidingMenu = new SlidingMenu(this); //设置一下侧滑菜单的位置 slidingMenu.setMode(SlidingMenu.LEFT); //设置触摸的区域 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); //设置菜单打开时,内容区域的宽度 slidingMenu.setBehindOffset(300); //范围是 0 - 1f ,当设置成1的时候菜单栏有明显的褪色效果 slidingMenu.setFadeDegree(1.0f); //将slidingMenu和Activity关联起来 slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT); slidingMenu.setMenu(R.layout.sliding_menu_layout); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.sliding_menu: //自动控制开关,当前是开会关闭,如果是关闭就会打开 slidingMenu.toggle(); //第二种方式,通过判断是否正在显示 if (slidingMenu.isMenuShowing()){ slidingMenu.showContent(); }else { slidingMenu.showMenu(); } break; } } }
package com.example.lixin.todaynews; 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.TextView; /** * Created by hua on 2017/8/2. */ public class MyFragment extends Fragment { private String text; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle arguments = getArguments(); text = arguments.getString("text"); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.myfragment, container, false); TextView textView = (TextView) inflate.findViewById(R.id.tv_fragment); textView.setText(text); return inflate; } }
package com.example.lixin.todaynews.adapter; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.example.lixin.todaynews.MyFragment; /** * Created by hua on 2017/8/2. */ public class MyPageAdapter extends FragmentPagerAdapter { private String[] titles = {"推荐", "热点", "北京", "视频", "军事娱乐", "热点", "北京", "视频", "军事娱乐"}; public MyPageAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { //new 我对应得fragment MyFragment myFragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("text",titles[position]); myFragment.setArguments(bundle); return myFragment; } //记得盘空 @Override public int getCount() { return titles.length; } //设置tablayout的每个tab的标题 @Override public CharSequence getPageTitle(int position) { return titles[position]; } }
activity_main
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <ImageView android:id="@+id/sliding_menu" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="40dp" app:tabMode="scrollable" app:tabIndicatorHeight="1dp" > </android.support.design.widget.TabLayout> <ImageView android:layout_width="40dp" android:layout_height="40dp" /> </LinearLayout> <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>
sliding_menu_layout(侧拉界面)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:background="#ff0000" android:layout_width="match_parent" android:layout_height="150dp"> </RelativeLayout> <LinearLayout android:background="#00ff00" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" ></LinearLayout> <LinearLayout android:background="#0000ff" android:layout_width="match_parent" android:layout_height="100dp"> </LinearLayout> </LinearLayout>
AndroidManifestxml(配置界面)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lixin.todaynews" > <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>