Android高级控件——ViewPager、GridView、popwindow、SlideMenu(中)
android:screenOrientation="locked"锁屏
android:screenOrientation="landscape"横屏锁定
<!--android:theme="@android:style/Theme.NoTitleBar.Fullscreen" Activity 直接extends Activity-->
//隐藏标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_zyf_text);
//隐藏ActionBar
getSupportActionBar().hide();
ViewPager滚动 + 滑动3页监听
ViewPager
横向滑动的分页组件
应用十分广泛的组件之一,如实现不同频道的左右滑动显示
同ListView一样属于适配器控件,ViewPager专属适配器PagerAdapter
<pre name="code" class="html" style="font-size: 16px;"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">package com.example.jreduch7292;</span>
import android.content.Intent;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import
android.view.Window;import android.view.WindowManager;import android.widget.ImageView;import java.util.ArrayList;import java.util.List;public class ZyfTextActivity extends AppCompatActivity { private ViewPager vp; private List<ImageView> list; @Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_zyf_text);
//隐藏ActionBar getSupportActionBar().hide(); vp=(ViewPager)findViewById(R.id.vp); vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override
public void onPageSelected(int position) { if(position==3){ Intent intent=new Intent(ZyfTextActivity.this,GridViewActivity.class); startActivity(intent); finish(); } } @Override public void onPageScrollStateChanged(int state) { } }); // (Context context,List<ListView>List)
list=new ArrayList<>(); ImageView img=new ImageView(this); img.setImageResource(R.mipmap.a); list.add(img); img=new ImageView(this); img.setImageResource(R.mipmap.b); list.add(img); img=new ImageView(this); img.setImageResource(R.mipmap.c); list.add(img);
img=new ImageView(this); list.add(img); vp.setAdapter(new MyViewPagerAdapter(list)); } public class MyViewPagerAdapter extends PagerAdapter { private List<ImageView> list; public MyViewPagerAdapter(List<ImageView>list){ this.list=list; } @Override public int
getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position);
} @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jreduch7292.ZyfTextActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:background="#b9cf0e" android:id="@+id/vp" > </android.support.v4.view.ViewPager> </RelativeLayout>
使用GridView制作分享界面
package com.example.jreduch7292; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ZyfPopActivity extends AppCompatActivity { private Button bt1; private Button bb; private GridView gv; private PopupWindow pw; private View popView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zyf_pop); bt1=(Button) findViewById(R.id.bt1); popView= getLayoutInflater().inflate(R.layout.activity_grid_view,null); gv=(GridView)popView.findViewById(R.id.gv) ; bb=(Button)popView.findViewById(R.id.bb); List list=new ArrayList(); Map map=new HashMap(); map.put("img",R.mipmap.a); map.put("name","字母A"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.b); map.put("name","字母B"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.c); map.put("name","字母C"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.d); map.put("name","字母D"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.e); map.put("name","字母E"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.f); map.put("name","字母F"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.g); map.put("name","字母G"); list.add(map); map=new HashMap(); map.put("img",R.mipmap.h); map.put("name","字母H"); list.add(map); SimpleAdapter sa=new SimpleAdapter(this,list,R.layout.grid_layout, new String[]{"img","name"}, new int[]{R.id.iv,R.id.tv}); gv.setAdapter(sa); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pw=getPopWindow(popView); //设置VIEW隐藏 bt1.setVisibility(View.GONE); } }); bb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pw.dismiss(); // WindowManager.LayoutParams ll=getWindow().getAttributes(); // ll.alpha=1f; // getWindow().setAttributes(ll); //设置VIEW可见 // bt1.setVisibility(View.VISIBLE); } }); } public PopupWindow getPopWindow(View view){ PopupWindow popupWindow=new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); popupWindow.setOutsideTouchable(false); popupWindow.setAnimationStyle(R.style.popStyle); //设置背景透明度 WindowManager.LayoutParams ll=getWindow().getAttributes(); ll.alpha=0.6f; getWindow().setAttributes(ll); //---------------------------------- popupWindow.setBackgroundDrawable(new ColorDrawable());//返回 popupWindow.showAtLocation(bt1, Gravity.BOTTOM,0,0); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams ll=getWindow().getAttributes(); ll.alpha=1f; getWindow().setAttributes(ll); bt1.setVisibility(View.VISIBLE); } }); return popupWindow; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jreduch7292.ZyfPopActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt1" android:text="popwindow分享" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1e50f" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.jreduch7292.GridViewActivity"> <GridView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gv" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:columnWidth="100dp" android:horizontalSpacing="5dp" android:verticalSpacing="5dp" android:cacheColorHint="#00000000" android:listSelector="#00000000" android:scrollbars="none" ></GridView> <Button android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@+id/gv" android:id="@+id/bb" android:text="取消分享" android:textSize="20dp" /> </RelativeLayout>
SlideMenu侧边栏
slidingMenu = new SlidingMenu(this); //new一个侧边栏
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT); slidingMenu.setMenu(menuView_L);
//将布局加载到侧边栏 slidingMenu.setSecondaryMenu(v1); slidingMenu.setBehindWidth(300);
//侧边栏出来的宽度 slidingMenu.setBehindOffset(380);
//剩余部分的宽度 slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//侧边栏执行开始
SlidingMenu
常用属性介绍
mSlidingMenu.setMode(SlidingMenu.LEFT);//设置左滑菜单
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置滑动的屏幕范围,该设置为全屏区域都可以滑动
mSlidingMenu.setShadowDrawable(R.drawable.shadow);//设置阴影图片
mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);//设置阴影图片的宽度
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu划出时主页面显示的剩余宽度
mSlidingMenu.setBehindWidth(400);//设置SlidingMenu菜单的宽度
mSlidingMenu.setFadeDegree(0.35f);//SlidingMenu滑动时的渐变程度
mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上
mSlidingMenu.setMenu(R.layout.menu_layout);//设置menu的布局文件
mSlidingMenu.toggle();//动态判断自动关闭或开启SlidingMenu
mSlidingMenu.showMenu();//显示SlidingMenu
mSlidingMenu.showContent();//显示内容
mSlidingMenu.setOnOpenListener(onOpenListener);//监听SlidingMenu打开
关于关闭menu有两个监听,简单的来说,对于menu close事件,一个是when,一个是after
mSlidingMenu.OnClosedListener(OnClosedListener);//监听SlidingMenu关闭时事件
mSlidingMenu.OnClosedListener(OnClosedListener);//监听SlidingMenu关闭后事件
mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);//设置左右都可以划出SlidingMenu菜单
mSlidingMenu.setSecondaryMenu(R.layout.frame_menu);//设置右侧菜单的布局文件
mSlidingMenu.setSecondaryShadowDrawable(R.drawable.shadowright);//右侧菜单的阴影图片
package com.example.jreduch7292; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; public class SlidingMenuActivity extends AppCompatActivity { private ImageView iv1; private ImageView iv2; private ImageView iv3; private LinearLayout ll1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // getSupportActionBar().hide(); setContentView(R.layout.activity_sliding_menu); SlidingMenu slidingMenu=new SlidingMenu(this);//new 一个侧边栏 // View v=getLayoutInflater().inflate(R.layout.activity_grid_view,null); View v=getLayoutInflater().inflate(R.layout.activity_duu,null); View v1=getLayoutInflater().inflate(R.layout.activity_duu,null); iv1=(ImageView)v1.findViewById(R.id.iv1); iv2=(ImageView)v1.findViewById(R.id.iv2); iv3=(ImageView)v1.findViewById(R.id.iv3); ll1=(LinearLayout)v1.findViewById(R.id.ll1); //设置屏幕滑动范围 slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //设置屏幕左侧划出布局 // slidingMenu.setMenu(v);//将布局加载到侧边栏 //设置划出模式 支持 左右都能划 ; slidingMenu.setMode(SlidingMenu.LEFT_RIGHT); slidingMenu.setMenu(v1); slidingMenu.setSecondaryMenu(v); // slidingMenu.setBehindWidth(300);//侧边栏出来的宽度 slidingMenu.setBehindOffset(380);//剩余部分的宽度 slidingMenu.setFadeDegree(0.35f); //附加到Activity // slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);//侧边栏执行开始 slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_WINDOW);//推bar iv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error图片1"); } }); iv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error图片2"); } }); iv3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error图片3"); } }); ll1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("====","error搜索"); } }); } }
<?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" android:background="#313131" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/iv1" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/zyfzyf" android:layout_weight="1" android:layout_marginTop="10dp" /> <ImageView android:id="@+id/iv2" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/zyfzyf" android:layout_weight="1" android:layout_marginTop="10dp" /> <ImageView android:id="@+id/iv3" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/zyfzyf" android:layout_weight="1" android:layout_marginTop="10dp" /> </LinearLayout> <TextView android:id="@+id/it1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="更多登录方式" android:textColor="#fff" android:gravity="center" android:textSize="18sp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" android:id="@+id/ll1" > <ImageView android:id="@+id/iv4" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center|left" android:src="@mipmap/zyfzyf" android:layout_marginLeft="30dp" /> <TextView android:id="@+id/tv5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="搜索" android:layout_gravity="center|left" android:layout_marginLeft="30dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:id="@+id/iv5" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center|left" android:src="@mipmap/zyfzyf" android:layout_marginLeft="30dp" /> <TextView android:id="@+id/tv6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="收藏" android:layout_gravity="center|left" android:layout_marginLeft="30dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:id="@+id/iv6" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center|left" android:src="@mipmap/zyfzyf" android:layout_marginLeft="30dp" /> <TextView android:id="@+id/tv7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="通知" android:layout_gravity="center|left" android:layout_marginLeft="30dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:id="@+id/iv7" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center|left" android:src="@mipmap/zyfzyf" android:layout_marginLeft="30dp" /> <TextView android:id="@+id/tv8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="活动" android:layout_gravity="center|left" android:layout_marginLeft="30dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" > <ImageView android:id="@+id/iv8" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center|left" android:src="@mipmap/zyfzyf" android:layout_marginLeft="30dp" /> <TextView android:id="@+id/tv9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="设置" android:layout_gravity="center|left" android:layout_marginLeft="30dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/iv10" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/zyfzyf" android:layout_weight="1" android:layout_marginTop="10dp" /> <ImageView android:id="@+id/iv11" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/zyfzyf" android:layout_weight="1" android:layout_marginTop="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv10" android:layout_width="0dp" android:layout_height="wrap_content" android:text="离线" android:textSize="16sp" android:textColor="#fff" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/tv11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="离线" android:textSize="16sp" android:textColor="#fff" android:layout_weight="1" android:gravity="center" /> </LinearLayout> </LinearLayout>
作者:冲天之峰 20160802