Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏
其实一种好的UI布局,可以使用户感到更加的亲切与方便。最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧边栏,其中运用的就是android3.0版本之后新加的Fragment碎片化,碎片 化的使用将来也是一个趋势,所以通过我这个程序你既可以学到侧边栏,也能让你更加熟悉碎片化的使用,一举两得的事。哈哈……废话不多说了,直接上图。图片 如下:
①、自定义一个View,把左侧边栏视图,中间内容视图,右侧边栏视图放在里面,这里给这个View起名叫:SlidingMenu.java
代码如下:
- package net.loonggg.view;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.RelativeLayout;
- public class SlidingMenu extends RelativeLayout {
- private SlidingView mSlidingView;
- private View mMenuView;
- private View mDetailView;
- public SlidingMenu(Context context) {
- super(context);
- }
- public SlidingMenu(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public void addViews(View left, View center, View right) {
- setLeftView(left);
- setRightView(right);
- setCenterView(center);
- }
- /**
- * 添加左侧边栏的view
- *
- * @param view
- */
- @SuppressWarnings("deprecation")
- public void setLeftView(View view) {
- LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.FILL_PARENT);
- behindParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);// 在父控件的左边
- addView(view, behindParams);
- mMenuView = view;
- }
- /**
- * 添加右侧边栏的view
- *
- * @param view
- */
- @SuppressWarnings("deprecation")
- public void setRightView(View view) {
- LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.FILL_PARENT);
- behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);// 在父控件的右边
- addView(view, behindParams);
- mDetailView = view;
- }
- /**
- * 添加中间内容的view
- *
- * @param view
- */
- @SuppressWarnings("deprecation")
- public void setCenterView(View view) {
- LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT);
- mSlidingView = new SlidingView(getContext());
- mSlidingView.setView(view);
- addView(mSlidingView, aboveParams);
- mSlidingView.setMenuView(mMenuView);
- mSlidingView.setDetailView(mDetailView);
- mSlidingView.invalidate();
- }
- public void showLeftView() {
- mSlidingView.showLeftView();
- }
- public void showRightView() {
- mSlidingView.showRightView();
- }
- }
②、通过一个中间的View,去控制左右侧边栏的滑进与滑出,这个也是自定义的一个View,名字叫:SlidingView.java
代码如下:
- package net.loonggg.view;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.ViewGroup;
- import android.widget.FrameLayout;
- import android.widget.Scroller;
- public class SlidingView extends ViewGroup {
- private FrameLayout mContainer;
- private Scroller mScroller;
- private VelocityTracker mVelocityTracker;
- private int mTouchSlop;
- private float mLastMotionX;
- private float mLastMotionY;
- private static final int SNAP_VELOCITY = 1000;
- private View mMenuView;
- private View mDetailView;
- public SlidingView(Context context) {
- super(context);
- init();
- }
- public SlidingView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public SlidingView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init();
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- mContainer.measure(widthMeasureSpec, heightMeasureSpec);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- final int width = r - l;
- final int height = b - t;
- mContainer.layout(0, 0, width, height);
- }
- private void init() {
- mContainer = new FrameLayout(getContext());
- mContainer.setBackgroundColor(0xff000000);
- mScroller = new Scroller(getContext());
- mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
- super.addView(mContainer);
- }
- public void setView(View v) {
- if (mContainer.getChildCount() > 0) {
- mContainer.removeAllViews();
- }
- mContainer.addView(v);
- }
- @Override
- public void scrollTo(int x, int y) {
- super.scrollTo(x, y);
- postInvalidate();
- }
- @Override
- public void computeScroll() {
- if (!mScroller.isFinished()) {
- if (mScroller.computeScrollOffset()) {
- int oldX = getScrollX();
- int oldY = getScrollY();
- int x = mScroller.getCurrX();
- int y = mScroller.getCurrY();
- if (oldX != x || oldY != y) {
- scrollTo(x, y);
- }
- // Keep on drawing until the animation has finished.
- invalidate();
- } else {
- clearChildrenCache();
- }
- } else {
- clearChildrenCache();
- }
- }
- private boolean mIsBeingDragged;
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- final int action = ev.getAction();
- final float x = ev.getX();
- final float y = ev.getY();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- mLastMotionX = x;
- mLastMotionY = y;
- mIsBeingDragged = false;
- break;
- case MotionEvent.ACTION_MOVE:
- final float dx = x - mLastMotionX;
- final float xDiff = Math.abs(dx);
- final float yDiff = Math.abs(y - mLastMotionY);
- if (xDiff > mTouchSlop && xDiff > yDiff) {
- mIsBeingDragged = true;
- mLastMotionX = x;
- }
- break;
- }
- return mIsBeingDragged;
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- if (mVelocityTracker == null) {
- mVelocityTracker = VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(ev);
- final int action = ev.getAction();
- final float x = ev.getX();
- final float y = ev.getY();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- if (!mScroller.isFinished()) {
- mScroller.abortAnimation();
- }
- mLastMotionX = x;
- mLastMotionY = y;
- if (getScrollX() == -getMenuViewWidth()
- && mLastMotionX < getMenuViewWidth()) {
- return false;
- }
- if (getScrollX() == getDetailViewWidth()
- && mLastMotionX > getMenuViewWidth()) {
- return false;
- }
- break;
- case MotionEvent.ACTION_MOVE:
- if (mIsBeingDragged) {
- enableChildrenCache();
- final float deltaX = mLastMotionX - x;
- mLastMotionX = x;
- float oldScrollX = getScrollX();
- float scrollX = oldScrollX + deltaX;
- if (deltaX < 0 && oldScrollX < 0) { // left view
- final float leftBound = 0;
- final float rightBound = -getMenuViewWidth();
- if (scrollX > leftBound) {
- scrollX = leftBound;
- } else if (scrollX < rightBound) {
- scrollX = rightBound;
- }
- // mDetailView.setVisibility(View.INVISIBLE);
- // mMenuView.setVisibility(View.VISIBLE);
- } else if (deltaX > 0 && oldScrollX > 0) { // right view
- final float rightBound = getDetailViewWidth();
- final float leftBound = 0;
- if (scrollX < leftBound) {
- scrollX = leftBound;
- } else if (scrollX > rightBound) {
- scrollX = rightBound;
- }
- // mDetailView.setVisibility(View.VISIBLE);
- // mMenuView.setVisibility(View.INVISIBLE);
- }
- scrollTo((int) scrollX, getScrollY());
- }
- break;
- case MotionEvent.ACTION_CANCEL:
- case MotionEvent.ACTION_UP:
- if (mIsBeingDragged) {
- final VelocityTracker velocityTracker = mVelocityTracker;
- velocityTracker.computeCurrentVelocity(1000);
- int velocityX = (int) velocityTracker.getXVelocity();
- velocityX = 0;
- Log.e("ad", "velocityX == " + velocityX);
- int oldScrollX = getScrollX();
- int dx = 0;
- if (oldScrollX < 0) {
- if (oldScrollX < -getMenuViewWidth() / 2
- || velocityX > SNAP_VELOCITY) {
- dx = -getMenuViewWidth() - oldScrollX;
- } else if (oldScrollX >= -getMenuViewWidth() / 2
- || velocityX < -SNAP_VELOCITY) {
- dx = -oldScrollX;
- }
- } else {
- if (oldScrollX > getDetailViewWidth() / 2
- || velocityX < -SNAP_VELOCITY) {
- dx = getDetailViewWidth() - oldScrollX;
- } else if (oldScrollX <= getDetailViewWidth() / 2
- || velocityX > SNAP_VELOCITY) {
- dx = -oldScrollX;
- }
- }
- smoothScrollTo(dx);
- clearChildrenCache();
- }
- break;
- }
- if (mVelocityTracker != null) {
- mVelocityTracker.recycle();
- mVelocityTracker = null;
- }
- return true;
- }
- private int getMenuViewWidth() {
- if (mMenuView == null) {
- return 0;
- }
- return mMenuView.getWidth();
- }
- private int getDetailViewWidth() {
- if (mDetailView == null) {
- return 0;
- }
- return mDetailView.getWidth();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- }
- public View getDetailView() {
- return mDetailView;
- }
- public void setDetailView(View mDetailView) {
- this.mDetailView = mDetailView;
- }
- public View getMenuView() {
- return mMenuView;
- }
- public void setMenuView(View mMenuView) {
- this.mMenuView = mMenuView;
- }
- // void toggle() {
- // int menuWidth = mMenuView.getWidth();
- // int oldScrollX = getScrollX();
- // if (oldScrollX == 0) {
- // smoothScrollTo(-menuWidth);
- // } else if (oldScrollX == -menuWidth) {
- // smoothScrollTo(menuWidth);
- // }
- // }
- /**
- * 左侧边栏的关闭与显示
- */
- public void showLeftView() {
- int menuWidth = mMenuView.getWidth();
- int oldScrollX = getScrollX();
- if (oldScrollX == 0) {
- smoothScrollTo(-menuWidth);
- } else if (oldScrollX == -menuWidth) {
- smoothScrollTo(menuWidth);
- }
- }
- /**
- * 右侧边栏的关闭与显示
- */
- public void showRightView() {
- int menuWidth = mDetailView.getWidth();
- int oldScrollX = getScrollX();
- if (oldScrollX == 0) {
- smoothScrollTo(menuWidth);
- } else if (oldScrollX == menuWidth) {
- smoothScrollTo(-menuWidth);
- }
- }
- void smoothScrollTo(int dx) {
- int duration = 500;
- int oldScrollX = getScrollX();
- mScroller.startScroll(oldScrollX, getScrollY(), dx, getScrollY(),
- duration);
- invalidate();
- }
- void enableChildrenCache() {
- final int count = getChildCount();
- for (int i = 0; i < count; i++) {
- final View layout = (View) getChildAt(i);
- layout.setDrawingCacheEnabled(true);
- }
- }
- void clearChildrenCache() {
- final int count = getChildCount();
- for (int i = 0; i < count; i++) {
- final View layout = (View) getChildAt(i);
- layout.setDrawingCacheEnabled(false);
- }
- }
- }
③、在MainActivity的布局文件中,引用咱们自定义的侧边栏View,MainActivity的布局文件代码如下:activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <net.loonggg.view.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/slidingMenu"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
④、MainActivity继承碎片化FragmentActivity,其代码如下:
- package net.loonggg.fragment;
- import net.loonggg.view.SlidingMenu;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentTransaction;
- import android.view.View;
- import android.view.Window;
- public class MainActivity extends FragmentActivity {
- private SlidingMenu mSlidingMenu;// 侧边栏的view
- private LeftFragment leftFragment; // 左侧边栏的碎片化view
- private RightFragment rightFragment; // 右侧边栏的碎片化view
- private SampleListFragment centerFragment;// 中间内容碎片化的view
- private FragmentTransaction ft; // 碎片化管理的事务
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 去标题栏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu);
- mSlidingMenu.setLeftView(getLayoutInflater().inflate(
- R.layout.left_frame, null));
- mSlidingMenu.setRightView(getLayoutInflater().inflate(
- R.layout.right_frame, null));
- mSlidingMenu.setCenterView(getLayoutInflater().inflate(
- R.layout.center_frame, null));
- ft = this.getSupportFragmentManager().beginTransaction();
- leftFragment = new LeftFragment();
- rightFragment = new RightFragment();
- ft.replace(R.id.left_frame, leftFragment);
- ft.replace(R.id.right_frame, rightFragment);
- centerFragment = new SampleListFragment();
- ft.replace(R.id.center_frame, centerFragment);
- ft.commit();
- }
- public void llronclick(View v) {
- switch (v.getId()) {
- case R.id.llr_energy_management:
- Intent intent = new Intent(this, DetailsActivity.class);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- public void showLeft() {
- mSlidingMenu.showLeftView();
- }
- public void showRight() {
- mSlidingMenu.showRightView();
- }
- }
⑤、左中右,左侧边栏,中间内容部分,右侧边栏,分别用Fragment代替,呈现出你想要的界面,这里我们只需要写Fragment就可以了,中间部分我们用SampleListFragment代替其中间内容部分,SampleListFragment的代码如下:
- package net.loonggg.fragment;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.ListFragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class SampleListFragment extends ListFragment {
- private ImageView lv_left;
- private ImageView iv_right;
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View mView = inflater.inflate(R.layout.list, null);
- lv_left = (ImageView) mView.findViewById(R.id.iv_left);
- iv_right = (ImageView) mView.findViewById(R.id.iv_right);
- return mView;
- }
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- Map<String, Object> item1 = new HashMap<String, Object>();
- item1.put("list_title", getString(R.string.title1));
- item1.put("list_image", R.drawable.p1);
- item1.put("list_contect", getString(R.string.test));
- Map<String, Object> item2 = new HashMap<String, Object>();
- item2.put("list_title", getString(R.string.title1));
- item2.put("list_image", R.drawable.p2);
- item2.put("list_contect", getString(R.string.test));
- Map<String, Object> item3 = new HashMap<String, Object>();
- item3.put("list_title", getString(R.string.title1));
- item3.put("list_image", R.drawable.p3);
- item3.put("list_contect", getString(R.string.test));
- Map<String, Object> item4 = new HashMap<String, Object>();
- item4.put("list_title", getString(R.string.title1));
- item4.put("list_image", R.drawable.p4);
- item4.put("list_contect", getString(R.string.test));
- Map<String, Object> item5 = new HashMap<String, Object>();
- item5.put("list_title", getString(R.string.title1));
- item5.put("list_image", R.drawable.p5);
- item5.put("list_contect", getString(R.string.test));
- Map<String, Object> item6 = new HashMap<String, Object>();
- item6.put("list_title", getString(R.string.title1));
- item6.put("list_image", R.drawable.p6);
- item6.put("list_contect", getString(R.string.test));
- Map<String, Object> item7 = new HashMap<String, Object>();
- item7.put("list_title", getString(R.string.title1));
- item7.put("list_image", R.drawable.p7);
- item7.put("list_contect", getString(R.string.test));
- List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
- data.add(item1);
- data.add(item2);
- data.add(item3);
- data.add(item4);
- data.add(item5);
- data.add(item6);
- data.add(item7);
- String[] from = new String[] { "list_title", "list_image",
- "list_contect" };
- int[] to = new int[] { R.id.list_title, R.id.list_image,
- R.id.list_contect };
- SimpleAdapter adapter = new SimpleAdapter(getActivity(), data,
- R.layout.list_item, from, to);
- setListAdapter(adapter);
- lv_left.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- ((MainActivity) getActivity()).showLeft();
- }
- });
- iv_right.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- ((MainActivity) getActivity()).showRight();
- }
- });
- }
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- Intent intent = new Intent(getActivity(), DetailsActivity.class);
- startActivity(intent);
- }
- }
其相应的布局文件代码如下:list.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#fff"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/banner_unit"
- android:padding="7dip" >
- <ImageView
- android:id="@+id/iv_left"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="0.1"
- android:clickable="true"
- android:src="@drawable/booklist_menu_normal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center_horizontal"
- android:textColor="#000"
- android:layout_weight="0.8"
- android:text="Fragment"
- android:textSize="17dp" />
- <ImageView
- android:id="@+id/iv_right"
- android:clickable="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:layout_weight="0.1"
- android:src="@drawable/back_normal" />
- </LinearLayout>
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- </ListView>
- </LinearLayout>
⑥、左侧边栏的View用LeftFragment替代,LeftFragment代码如下:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- public class LeftFragment extends Fragment {
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.left_fragment, null);
- LinearLayout userLayout = (LinearLayout) view
- .findViewById(R.id.userLayout);
- userLayout.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- UserFragment user = new UserFragment();
- FragmentTransaction ft = getActivity()
- .getSupportFragmentManager().beginTransaction();
- ft.replace(R.id.center_frame, user);
- ft.commit();
- ((MainActivity) getActivity()).showLeft();
- }
- });
- LinearLayout mainPage = (LinearLayout) view.findViewById(R.id.mainPage);
- mainPage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- FragmentTransaction ft = getActivity()
- .getSupportFragmentManager().beginTransaction();
- ft.replace(R.id.center_frame, new SampleListFragment());
- ft.commit();
- ((MainActivity) getActivity()).showLeft();
- }
- });
- return view;
- }
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
- }
其对应的布局文件代码如下:left_fragment.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/mlist"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/mainPage"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/oper_title2"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="主页"
- android:textColor="#000"
- android:textSize="17dip" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="@drawable/booklist_menu_bg_unit2"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/userLayout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.2"
- android:clickable="true"
- android:orientation="vertical"
- android:padding="5dip" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:src="@drawable/booklist_menu_user" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="用户"
- android:textColor="#000" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.2"
- android:clickable="true"
- android:orientation="vertical"
- android:padding="5dip" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:src="@drawable/booklist_menu_synchronize" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="收藏"
- android:textColor="#000" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.2"
- android:clickable="true"
- android:orientation="vertical"
- android:padding="5dip" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:src="@drawable/booklist_menu_std" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="标准"
- android:textColor="#000" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.2"
- android:clickable="true"
- android:orientation="vertical"
- android:padding="5dip" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:src="@drawable/booklist_menu_help" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="帮助"
- android:textColor="#000" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.2"
- android:clickable="true"
- android:orientation="vertical"
- android:padding="5dip" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:src="@drawable/booklist_menu_about" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="关于"
- android:textColor="#000" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
⑦、右侧边栏的View用RightFragment替代,RightFragment代码如下:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class RightFragment extends Fragment {
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.right_fragment, null);
- return view;
- }
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
- }
其对应的布局文件代码如下:right_fragment.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/oper_title"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:text="服 务"
- android:textColor="#000"
- android:textSize="17dip" />
- </LinearLayout>
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:background="@drawable/booklist_menu_bg_unit" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:padding="5dip" >
- <LinearLayout
- android:id="@+id/llr_energy_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="能源管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_hr_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="人力管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_business_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="商务管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_financial_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="金融管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_purchasing_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="采购管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_supply_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="供应管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <LinearLayout
- android:id="@+id/llr_project_management"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:clickable="true"
- android:onClick="llronclick" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:src="@drawable/lxqg" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.8"
- android:text="项目管理"
- android:textColor="#000" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="0.1"
- android:gravity="center"
- android:src="@drawable/arrow_to_right" />
- </LinearLayout>
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:src="@drawable/line_read_option" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="说明我是宽度自适应的..." />
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
⑧、在左侧边栏中有一个点击事件,点击用户,进入用户界面,这里的用户界面也是一个Fragment,点击事件的具体代码在⑥中已经有了,现在我把用户界面的Fragment代码贴出来:UserFragment.java:
- package net.loonggg.fragment;
- 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.ImageView;
- public class UserFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.user, null);
- ImageView left = (ImageView) view.findViewById(R.id.iv_user_left);
- left.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- ((MainActivity) getActivity()).showLeft();
- }
- });
- return view;
- }
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
- }
其对应的布局代码如下:user.xml:
- <?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:background="#ffffff"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/banner_unit"
- android:padding="7dip" >
- <ImageView
- android:id="@+id/iv_user_left"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="0.1"
- android:clickable="true"
- android:src="@drawable/booklist_menu_normal" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="0.8"
- android:gravity="center_horizontal"
- android:text="Fragment"
- android:textColor="#000"
- android:textSize="17dp" />
- <ImageView
- android:id="@+id/iv_right"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:layout_weight="0.1"
- android:clickable="true"
- android:src="@drawable/back_normal" />
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:text="用户管理"
- android:textColor="#000000"
- android:textSize="24sp" />
- </LinearLayout>
⑨、其他不重要的代码如下:
DetailsActivity.java:
- package net.loonggg.fragment;
- import android.app.Activity;
- import android.os.Bundle;
- public class DetailsActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.details);
- }
- }
其对应的布局文件代码如下:details.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#fff"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:scaleType="fitXY"
- android:src="@drawable/p1" />
- </LinearLayout>
还有就是string.xml的代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">碎片化侧边栏</string>
- <string name="action_settings">Settings</string>
- <string name="title1">这种导弹的平均航速为5倍音速</string>
- <string name="title2">这种导弹的平均航速为5倍音速</string>
- <string name="title3">这种导弹的平均航速为5倍音速</string>
- <string name="title4">这种导弹的平均航速为5倍音速</string>
- <string name="title5">这种导弹的平均航速为5倍音速</string>
- <string name="title6">这种导弹的平均航速为5倍音速</string>
- <string name="title7">这种导弹的平均航速为5倍音速</string>
- <string name="test">美国《空军》杂志、西班牙《世界报》等媒体报道,美军计划在今年内多次试射高超音速导弹,从而将其作为威慑中俄的强力武器。有外媒指出,解放军已开发出可拦截高速导弹的防控系统,而且有潜力进一步开发拦截高超音速导弹的防空武器。
- 美
国《空军》杂志报道,美军将在2013年继续试射高超音速巡航导弹,这种导弹的平均航速为5倍音速,最高瞬时航速达到7倍音速。对于目前全球所有的防卫系
统来说,它都是无法防御的导弹。美国《防务新闻》更是声称,美军的“高超音速攻击时代”即将来临,目前各国军队尚无能力对这种高速攻击目标进行有效拦截,
任何导弹防御系统在它面前都形同虚设。 - 西班牙《世界报》报道,从2013年3月到12月,美军可能进行3次重要的实验,如果一切顺利,美军可能于2014年尝试装备少量的高超音速导弹,这种导弹有望在未来5年内形成战斗力。
- 报道认为,美军高超音速导弹所形成的威胁以及对世界格局的影响难以估量。美国可能在一些“敏感区域”部署这种导弹,例如东亚地区,而朝鲜和伊朗的核设施也可能成为它的攻击目标。报道强调,美军高超音速导弹的主要作用是对其他军事大国实施威慑,主要是中国和俄罗斯。
- 据
俄罗斯《军工信使》周刊指出,解放军在防范超音速导弹攻击方面“已经追赶上来”,中国某国防工业公开发的代号为LD-2000的陆基防空系统,已经被证实
具备实战能力。这种防空系统可为高价值目标,包括指挥所、弹道导弹发射装置等提供保护。LD-2000防空系统发射的导弹拦截高速飞行的导弹和飞机,尤其
擅长对付雷达反射面积较小的隐身巡航导弹及高速巡航导弹。报道揣测,它可以准确拦截飞行速度超过两倍音速的超音速导弹。通过改进其控制软件,该系统还可用
来拦截高速火箭弹和迫击炮弹。 - 报道称,LD-2000系统可加装在军用卡车底盘上,从而实现机动部署,在需要它的地方构建防范超音速导弹的密集导弹阵。这种导弹集群的火力可与美军著名的“密集阵”(专门防御来袭空中目标的防空导弹集群)媲美。
- 报道称,LD-2000是一款针对出口市场的产品,解放军肯定同步开发了自用型号,而且其性能必然优于LD-2000,应该可以防范速度更快的来袭目标,例如航速达到三四倍音速的巡航导弹。报道认为,这表明解放军有能力开发用来对付美军高超音速导弹的防御系统。</string>
- </resources>
到这里就基本完成了。可能你现在没有看懂,我把源码放出来,供大家参考。源代码下载地址如下:
Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏的更多相关文章
- 【转】 Pro Android学习笔记(四一):Fragment(6):数据保留
目录(?)[-] 通过fragment参数实现数据保留 对TitleFragment进行修改 对DetailActivity进行修改 通过savedInstanceState进行数据保留 保留frag ...
- 【转】 Pro Android学习笔记(三九):Fragment(4):基础小例子-续
目录(?)[-] Step 3实现简介显示类DetailFragment 创建实例 编写所需的生命周期代码 Step 4实现showDetailint index如何管理fragment fragme ...
- 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续
目录(?)[-] Step 2实现Fragment指定调用类TitleFragment onInflate和onAttach onCreate和onCreateView onActivityCreat ...
- 【转】 Pro Android学习笔记(三七):Fragment(2):基础小例子
目录(?)[-] 小例子运行效果 Pre-step一点准备 Step 1Activity的布局 小例子运行效果 这是一个书名和书简介的例子.运行如下图.Activity由左右两个Fragment组成, ...
- Android学习笔记四十Preference使用
Preference直译为偏好,博友建议翻译为首选项.一些配置数据,一些我们上次点击选择的内容,我们希望在下次应用调起的时候依旧有效,无须用户再一次进行配置或选择.Android提供preferenc ...
- 【转】Pro Android学习笔记(三六):Fragment(1):基本概念
目录(?)[-] 为何引入Fragment 大小屏幕的适配 横屏竖屏切换 返回键 什么是Fragment 为何引入Fragment 我们之前的Activity都是都是全屏处理较为简单的单一事务功能,适 ...
- Android学习笔记四:activity的四种启动模式
Activity有四种启动模式:standard,singleTop,singleTask,singleInstance. 1.standard:标准启动模式 默认模式,这个模式下启动的Activit ...
- android 学习笔记四:控件
1.android:gravity 指定控件的基本位置,比如居中.居右等位置 Top:顶部 bottom:底部 left:居左 right:居右 center_vertical:垂直居中 center ...
- Android 学习笔记四:创建工具栏按钮
原文:http://blog.csdn.net/lihongxun945/article/details/48951199 前面我们已经可以在一个Activity中添加一些按钮之类的组件.由于手机的屏 ...
随机推荐
- vos设置可呼出手机或固话
问题: 默认公司只让呼出手机号码,但有的客户要求能打固话,怎么办? 落地网关——补充设置——落地前缀——落地被叫改写规则 在改写规则里添加固话号段即可 具体案例: 5201——1表示让520号段只能拨 ...
- 笨办法学Python(二十二)
习题 22: 到现在你学到了哪些东西? 这节以及下一节的习题中不会有任何代码,所以也不会有习题答案或者加分习题.其实这节习题可以说是一个巨型的加分习题.我将让你完成一个表格,让你回顾你到现在学到的所有 ...
- 使用ABAP和JavaScript代码生成PDF文件的几种方式
ABAP 方法1:使用ABAP + Adobe Lifecycle Enterprise Service 详细步骤参考我的博客Convert word document into PDF via Ad ...
- Vue--父组件传数据给子组件,子组件生命周期过程拿到数据的情况
需求: 在子组件渲染之前,我要修改数据的某个字段 结果是 组件在beforeUpdate,updated 的状态才能拿到父组件的数据 那么证明,我根本无法在beforeUpdate,updated两个 ...
- 编译安装 mysql 5.5,运行 cmake报错Curses library not found
是因为 curses库没有安装,执行下面的语句即可 yum -y install ncurses-devel 如果上述命令的结果是no package,则使用下面的命令安装 apt-get insta ...
- 字符串缓冲区StringBuffer类,正则表达式
StringBuffer类 StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容.它是一个容器,容器中可以装很多字符串. ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- angular学习之angular-phonecat项目的实现
---恢复内容开始--- AngularJS官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些Android手机,了解它们的详细信息,并进行搜索和排序操作. ...
- Linux下vim操作的一些使用技巧
以下均为个人在编程时对vim编辑器的一些心得,大神请指点,新手可以看过来 1.多文本编辑 vim -On/-on filename_1 … filename_n 如上所示,在要编辑的文件名前加上“-O ...
- linux shell 部分问题解决方法
1. 判断shell里判断字符串是否包含某个字符 a. 可以用正则式匹配符号 “=~” 举例:str="this is a string" 要想在判断str中是否含有 ...