SlidingMenu實現側滑微商城開發找捌躍科技
/**
- Mactivity */
package xixinxin.bawie.com.slidingmenu_study.activity;
import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.widget.FrameLayout;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import xixinxin.bawie.com.slidingmenu_study.R; import xixinxin.bawie.com.slidingmenu_study.fragment.HomeFragment; import xixinxin.bawie.com.slidingmenu_study.fragment.MenuFragment;
public class MainActivity extends AppCompatActivity {
private FrameLayout fram;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控制元件
initView();
//建立SlidingMenu
SlidingMenu slidingMenu=new SlidingMenu(this);
//設定側滑方向 從左往右
slidingMenu.setMode(SlidingMenu.LEFT);
//設定整屏滑出
private void initFragment() {
//開啟事務 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //替換 transaction.replace(R.id.menu,new MenuFragment()); //提交 transaction.commit(); getSupportFragmentManager().beginTransaction().replace(R.id.fram,new HomeFragment()).commit(); }
private void initView() {
fram = (FrameLayout) findViewById(R.id.fram);
}
}
/**
-
介面卡 */ public class MyViewAdapter extends FragmentPagerAdapter{ private List list; private List title;
public MyViewAdapter(FragmentManager fm, List list, List title){ super(fm); this.list = list; this.title = title; } @Override public Fragment getItem(int position) { return list.get(position); }
@Override public int getCount() { return list.size(); }
//設定滑動頁與tablayout標題相關聯 @Override public CharSequence getPageTitle(int position) { return title.get(position); } }
/**
- //設定TabLayout模式 系統固定模式 tab.setTabMode(TabLayout.MODE_SCROLLABLE); MyViewAdapter adapter=new MyViewAdapter(getActivity().getSupportFragmentManager(),fragments,titles); vp.setAdapter(adapter); //將tablayout和viewpager關聯 tab.setupWithViewPager(vp); tab.setTabsFromPagerAdapter(adapter); */
/**
- 佈局檔案 */
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="48dp"
app:tabSelectedTextColor="#ff0000"
app:tabTextColor="@android:color/black"
app:tabIndicatorColor="#ff0000"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>