android 帮助页
每个程序都需要帮助介绍页面
主代码为:
package com.weidingqiang.custommooc.activitys; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import com.weidingqiang.custommooc.R; import java.util.ArrayList; import java.util.List; /** * 引导页 */ public class GuideActivity extends AppCompatActivity implements View.OnClickListener{ private static final String TAG = GuideActivity.class.getSimpleName(); private ViewPager viewPager; private Button guideButton; private int[] imgIds; private List<ImageView> imageViews ; //是否显示按钮 private boolean isshow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide); //设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); initData(); initUI(); } /** * 绑定UI */ private void initUI(){ viewPager = (ViewPager) this.findViewById(R.id.guide_viewPager); guideButton = (Button) this.findViewById(R.id.guide_button); guideButton.setOnClickListener(this); initImageData(); viewPager.setAdapter(pagerAdapter); viewPager.addOnPageChangeListener(onPageChangeListener); } /** * 初始化数据 */ private void initData(){ imgIds = new int[] { R.mipmap.guide_image1,R.mipmap.guide_image2, R.mipmap.guide_image3,R.mipmap.guide_image4,R.mipmap.guide_image5}; imageViews = new ArrayList<ImageView>(); } private void initImageData() { for (int imgId : imgIds) { ImageView imageView = new ImageView(getApplicationContext()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setImageResource(imgId); imageViews.add(imageView); } } @Override public void onClick(View v) { finish(); } /** * PagerAdapter 监听器 */ private PagerAdapter pagerAdapter = new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageViews.get(position)); return imageViews.get(position); } @Override public int getCount() { return imageViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imageViews.get(position)); } }; /** * OnPageChangeListener 监听器 */ private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if(position == imageViews.size()-1){ isshow = true; }else { isshow = false; } } @Override public void onPageScrollStateChanged(int state) { if(isshow && state==ViewPager.SCROLL_STATE_IDLE) { guideButton.setVisibility(View.VISIBLE); }else { guideButton.setVisibility(View.INVISIBLE); } } }; }
布局文件为:
<?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" > <android.support.v4.view.ViewPager android:id="@+id/guide_viewPager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <Button android:id="@+id/guide_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/guide_but_bg" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_marginBottom="60dp" android:visibility="invisible" /> </RelativeLayout>
按钮状态文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@mipmap/guide_start_click_pressed"/> <item android:drawable="@mipmap/guide_start_click_pormal"/> </selector>
增加状态按钮 point
主代码:
package com.weidingqiang.custommooc.activitys; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import com.weidingqiang.custommooc.R; import com.weidingqiang.custommooc.utils.DisplayUtil; import com.weidingqiang.custommooc.views.widgets.GuidePointGroup; import java.util.ArrayList; import java.util.List; /** * 引导页 */ public class GuideActivity extends AppCompatActivity implements View.OnClickListener{ private static final String TAG = GuideActivity.class.getSimpleName(); private ViewPager viewPager; private Button guideButton; private int[] imgIds; private List<ImageView> imageViews ; //是否显示按钮 private boolean isshow; private RelativeLayout baseLayout; private GuidePointGroup guidePointGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide); //设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); initData(); initUI(); } /** * 绑定UI */ private void initUI(){ baseLayout = (RelativeLayout) this.findViewById(R.id.baseLayout); viewPager = (ViewPager) this.findViewById(R.id.guide_viewPager); guideButton = (Button) this.findViewById(R.id.guide_button); guideButton.setOnClickListener(this); initImageData(); initGuidePointGroup(); viewPager.setAdapter(pagerAdapter); viewPager.addOnPageChangeListener(onPageChangeListener); } /** * 初始化数据 */ private void initData(){ imgIds = new int[] { R.mipmap.guide_image1,R.mipmap.guide_image2, R.mipmap.guide_image3,R.mipmap.guide_image4,R.mipmap.guide_image5}; imageViews = new ArrayList<ImageView>(); } private void initImageData() { for (int imgId : imgIds) { ImageView imageView = new ImageView(getApplicationContext()); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setImageResource(imgId); imageViews.add(imageView); } } private void initGuidePointGroup(){ guidePointGroup = new GuidePointGroup(this,imgIds.length); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layoutParams.bottomMargin = DisplayUtil.dip2px(getBaseContext(),90); baseLayout.addView(guidePointGroup, layoutParams); } @Override public void onClick(View v) { finish(); } /** * PagerAdapter 监听器 */ private PagerAdapter pagerAdapter = new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageViews.get(position)); return imageViews.get(position); } @Override public int getCount() { return imageViews.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imageViews.get(position)); } }; /** * OnPageChangeListener 监听器 */ private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if(position == imageViews.size()-1){ isshow = true; }else { isshow = false; } guidePointGroup.changeSelectView(position); } @Override public void onPageScrollStateChanged(int state) { if(isshow && state==ViewPager.SCROLL_STATE_IDLE) { guideButton.setVisibility(View.VISIBLE); }else { guideButton.setVisibility(View.INVISIBLE); } } }; }
布局文件:
<?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" android:id="@+id/baseLayout" > <android.support.v4.view.ViewPager android:id="@+id/guide_viewPager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <Button android:id="@+id/guide_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/guide_but_bg" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:visibility="invisible" /> </RelativeLayout>
pointview 代码
package com.weidingqiang.custommooc.views.widgets; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.weidingqiang.custommooc.R; import com.weidingqiang.custommooc.utils.DisplayUtil; import java.util.ArrayList; import java.util.List; /** * Created by weidingqiang on 15/11/25. */ public class GuidePointGroup extends LinearLayout { private int pointnum; private List<ImageView> viewList; public GuidePointGroup(Context context,int num) { super(context); pointnum = num; initView(); } private void initView(){ viewList = new ArrayList<ImageView>(); for (int i = 0;i<pointnum;i++){ LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); if(i!=0){ layoutParams.leftMargin = DisplayUtil.dip2px(getContext(),6); } ImageView view = new ImageView(getContext()); view.setBackgroundResource(R.drawable.guide_view_bg); addView(view, layoutParams); viewList.add(view); } changeSelectView(0); } public void changeSelectView(int current){ for (int i = 0;i<pointnum;i++) { ImageView view = viewList.get(i); view.setSelected(i == current); } } }
point_bg
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@mipmap/guide_view_check_icon"/> <item android:drawable="@mipmap/guide_view_uncheck_icon"/> </selector>
人的一生能做好一件事就阿弥陀佛了