标签:
<?xml version="1.0" encoding="UTF-8"?> <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/sp_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/lv_sliding" android:layout_width="150dp" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_sliding" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffdd" android:paddingLeft="100dp" android:text="打开侧滑菜单" android:textColor="#000000" android:textSize="20sp" /> <android.support.v4.view.ViewPager android:id="@+id/vp_sliding" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.v4.widget.SlidingPaneLayout>
import java.util.ArrayList; import com.example.exmdrawer.fragment.ColorFragment; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class SlidingActivity extends FragmentActivity implements OnClickListener, OnItemClickListener { private final static String TAG = "SlidingActivity"; private SlidingPaneLayout sp_layout; private ListView lv_sliding; private ViewPager vp_sliding; private TextView tv_sliding; private String[] colorDescArray = {"红色", "绿色", "蓝色", "白色", "黑色"}; private int[] colorArray = {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE, Color.BLACK}; private ColorPagerAdapter vp_adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sliding); sp_layout = (SlidingPaneLayout) findViewById(R.id.sp_layout); lv_sliding = (ListView) findViewById(R.id.lv_sliding); vp_sliding = (ViewPager) findViewById(R.id.vp_sliding); tv_sliding = (TextView) findViewById(R.id.tv_sliding); tv_sliding.setOnClickListener(this); //sp_layout.setSliderFadeColor(Color.YELLOW); sp_layout.setCoveredFadeColor(Color.RED); sp_layout.setPanelSlideListener(new SlidingPanelListener()); ArrayAdapter<String> lv_adapter = new ArrayAdapter<String>(this, R.layout.list_item, colorDescArray); lv_sliding.setAdapter(lv_adapter); lv_sliding.setOnItemClickListener(this); vp_adapter = new ColorPagerAdapter(getSupportFragmentManager()); vp_sliding.setAdapter(vp_adapter); vp_sliding.addOnPageChangeListener(new ColorPagerListener()); } @Override public void onClick(View v) { if (v.getId() == R.id.tv_sliding) { if (sp_layout.isOpen()) { sp_layout.closePane(); } else { sp_layout.openPane(); } } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { vp_sliding.setCurrentItem(position); sp_layout.closePane(); } public class SlidingPanelListener implements PanelSlideListener { @Override public void onPanelClosed(View arg0) { tv_sliding.setText("打开侧滑菜单"); } @Override public void onPanelOpened(View arg0) { tv_sliding.setText("关闭侧滑菜单"); } @Override public void onPanelSlide(View arg0, float arg1) { } } public class ColorPagerListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { //tv_sliding.setText(colorDescArray[position]); } } private ArrayList<Fragment> mFragments; public class ColorPagerAdapter extends FragmentPagerAdapter { public ColorPagerAdapter(FragmentManager fm) { super(fm); mFragments = new ArrayList<Fragment>(); for (int color : colorArray) { mFragments.add(new ColorFragment(color)); } } @Override public int getCount() { return mFragments.size(); } @Override public Fragment getItem(int position) { return mFragments.get(position); } } }
import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; public class ColorFragment extends Fragment { private static final String TAG = "ColorFragment"; protected Context mContext; private int mColor = -1; public ColorFragment(int colorRes) { mColor = colorRes; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContext = getActivity(); if (savedInstanceState != null) { mColor = savedInstanceState.getInt("mColor"); } LinearLayout layout = new LinearLayout(mContext); layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); layout.setBackgroundColor(mColor); return layout; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mColor != -1) { outState.putInt("mColor", mColor); } } }
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/dl_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_drawer_left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="打开左边侧滑" android:textColor="#000000" android:textSize="20sp" /> <TextView android:id="@+id/tv_drawer_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="打开右边侧滑" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout> <TextView android:id="@+id/tv_drawer_center" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top|center" android:paddingTop="30dp" android:text="首页" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout> <ListView android:id="@+id/lv_drawer_left" android:layout_width="150dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ffdd99" /> <ListView android:id="@+id/lv_drawer_right" android:layout_width="150dp" android:layout_height="match_parent" android:layout_gravity="right" android:background="#99ffdd" /> </android.support.v4.widget.DrawerLayout>
import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout.DrawerListener; import android.support.v4.widget.ViewDragHelper; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class DrawerActivity extends Activity implements OnClickListener { private final static String TAG = "DrawerActivity"; private DrawerLayout dl_layout; private TextView tv_drawer_left; private TextView tv_drawer_right; private TextView tv_drawer_center; private ListView lv_drawer_left; private ListView lv_drawer_right; private String[] titleArray = {"首页", "新闻", "娱乐", "博客", "论坛"}; private String[] settingArray = {"我的", "设置", "关于"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer); dl_layout = (DrawerLayout) findViewById(R.id.dl_layout); dl_layout.addDrawerListener(new SlidingListener()); //setDrawerLeftEdgeSize(this, dl_layout, 0.3f); tv_drawer_left = (TextView) findViewById(R.id.tv_drawer_left); tv_drawer_right = (TextView) findViewById(R.id.tv_drawer_right); tv_drawer_center = (TextView) findViewById(R.id.tv_drawer_center); tv_drawer_left.setOnClickListener(this); tv_drawer_right.setOnClickListener(this); lv_drawer_left = (ListView) findViewById(R.id.lv_drawer_left); ArrayAdapter<String> left_adapter = new ArrayAdapter<String>(this, R.layout.list_item, titleArray); lv_drawer_left.setAdapter(left_adapter); lv_drawer_left.setOnItemClickListener(new LeftListListener()); lv_drawer_right = (ListView) findViewById(R.id.lv_drawer_right); ArrayAdapter<String> right_adapter = new ArrayAdapter<String>(this, R.layout.list_item, settingArray); lv_drawer_right.setAdapter(right_adapter); lv_drawer_right.setOnItemClickListener(new RightListListener()); } @Override public void onClick(View v) { if (v.getId() == R.id.tv_drawer_left) { if (dl_layout.isDrawerOpen(lv_drawer_left)) { dl_layout.closeDrawer(lv_drawer_left); } else { dl_layout.openDrawer(lv_drawer_left); } } else if (v.getId() == R.id.tv_drawer_right) { if (dl_layout.isDrawerOpen(lv_drawer_right)) { dl_layout.closeDrawer(lv_drawer_right); } else { dl_layout.openDrawer(lv_drawer_right); } } } private class LeftListListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String text = titleArray[position]; tv_drawer_center.setText(text+text+text+text+text); dl_layout.closeDrawers(); } } private class RightListListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String text = settingArray[position]; tv_drawer_center.setText(text+text+text+text+text); dl_layout.closeDrawers(); } } private class SlidingListener implements DrawerListener { @Override public void onDrawerSlide(View paramView, float paramFloat) { } @Override public void onDrawerOpened(View paramView) { if (paramView.getId() == R.id.lv_drawer_left) { tv_drawer_left.setText("关闭左边侧滑"); } else { tv_drawer_right.setText("关闭右边侧滑"); } } @Override public void onDrawerClosed(View paramView) { if (paramView.getId() == R.id.lv_drawer_left) { tv_drawer_left.setText("打开左边侧滑"); } else { tv_drawer_right.setText("打开右边侧滑"); } } @Override public void onDrawerStateChanged(int paramInt) { } } //设置左边侧滑的边缘大小 private void setDrawerLeftEdgeSize(Activity act, DrawerLayout layout, float percentage) { if (act == null || layout == null) return; try { Field leftDraggerField = layout.getClass().getDeclaredField("mLeftDragger"); leftDraggerField.setAccessible(true); ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(layout); Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize"); edgeSizeField.setAccessible(true); int edgeSize = edgeSizeField.getInt(leftDragger); DisplayMetrics dm = new DisplayMetrics(); act.getWindowManager().getDefaultDisplay().getMetrics(dm); edgeSizeField.setInt(leftDragger, Math.max(edgeSize, (int) (dm.widthPixels * percentage))); } catch (Exception e) { e.printStackTrace(); } } }
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/52252835