首先要依赖 compile 'com.android.support:design:25.0.0'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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.wuxianedu.aatoolbarto.MainActivity" xmlns:app="http://schemas.android.com/apk/res-auto" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!--app:layout_scrollFlags="scroll|enterAlways 向上滑动ToolBar随时出现--> <!--app:layout_scrollFlags="scroll|exitUntilCollapsed" 向上滑动时保留ToolBar的初始大小--> <!-- app:layout_scrollFlags="scroll|enterAlwaysCollapsed" 向上滑动的时候ToolBar隐藏,向下滑动顶部的时候ToolBar出现--> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/coll_zhedie" android:fitsSystemWindows="true" android:layout_width="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary"> <ImageView android:layout_width="match_parent" android:layout_height="120dp" android:src="@mipmap/titts" app:layout_collapseParallaxMultiplier="0.8" android:scaleType="centerCrop" app:layout_collapseMode="parallax" android:fitsSystemWindows="true" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_id" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_collapseMode="pin"> <RelativeLayout android:layout_marginLeft="60dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="搜索" android:layout_marginLeft="45dp" android:layout_width="wrap_content" app:layout_collapseMode="pin" android:layout_height="wrap_content" android:visibility="gone"/> <com.wuxianedu.aatoolbarto.ClearEditText android:gravity="center" android:hint="搜索商品和商家" android:layout_width="300dp" app:layout_collapseMode="pin" android:layout_marginTop="4dp" android:drawableRight="@drawable/lxrdelete" android:textColor="#000000" android:background="@drawable/lxr_sousuo" android:text="输入商家和商品" android:layout_height="40dp" android:visibility="visible"/> <ImageView android:id="@+id/iv_sousuo" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginTop="12dp" app:layout_collapseParallaxMultiplier="1" android:src="@drawable/search_icon" android:layout_marginLeft="5dp"/> </RelativeLayout> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_height="match_parent"> <ScrollView android:layout_width="wrap_content" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="500dp"> <GridView android:id="@+id/gv_id" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:numColumns="4" android:layout_height="137dp" android:visibility="visible"/> <com.wuxianedu.aatoolbarto.NoScrollListView android:id="@+id/lv_id" android:layout_width="wrap_content" android:layout_height="500dp"/> </LinearLayout> </ScrollView> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
item_adapter.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <ImageView android:id="@+id/image" android:src="@mipmap/icon_linkedin" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" /> 9 </LinearLayout>
activity_mainsss.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" > 6 7 <ImageView 8 android:id="@+id/head" android:src="@mipmap/ic_launcher" 9 android:layout_width="100dp" android:layout_marginLeft="5dp" 10 android:layout_height="100dp" android:scaleType="centerCrop" 11 android:paddingLeft="10dp" /> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="vertical" > 17 18 <TextView 19 android:id="@+id/name" 20 android:text="wowowow" android:layout_marginTop="25dp" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:textSize="20dp" 24 android:textColor="#f0f" 25 android:paddingLeft="10dp"/> 26 27 <TextView 28 android:id="@+id/pass" 29 android:text="介绍" android:layout_marginTop="10dp" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:textSize="20dp" 33 android:textColor="#f0f" 34 android:paddingLeft="10dp"/> 35 36 </LinearLayout> 37 38 </LinearLayout>
布局文件都贴出来了,下面贴出代码
Mainactivity.java
1 package com.wuxianedu.aatoolbarto; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.GridView; 6 import android.widget.ListView; 7 import android.widget.SimpleAdapter; 8 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 public class MainActivity extends AppCompatActivity { 15 private String[] name = { "剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意" 16 ,"剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意","剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意"}; 17 private String[] names = { "剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意" 18 ,"剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意","剑萧舞蝶", "张三", "hello", "诗情画意", "剑萧舞蝶", "张三", "hello", "诗情画意"}; 19 private int[] imageids = { R.mipmap.s1,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7, 20 R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7,R.mipmap.s1,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7, 21 R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5 22 ,R.mipmap.s1,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7, 23 R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s1,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5,R.mipmap.s6,R.mipmap.s7, 24 R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5}; 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 GridView gridView = (GridView) findViewById(R.id.gv_id); 31 32 ListView listView = (ListView) findViewById(R.id.lv_id); 33 List<Map<String, Object>> listems = new ArrayList<>(); 34 List<Map<String, Object>> ssssss = new ArrayList<>(); 35 for (int i = 0; i < name.length; i++) { 36 Map<String, Object> listem = new HashMap<String, Object>(); 37 listem.put("head", imageids[i]); 38 listem.put("name", name[i]); 39 listem.put("pass", names[i]); 40 listems.add(listem); 41 } 42 43 for (int i = 0; i < name.length; i++) { 44 Map<String, Object> listemss = new HashMap<String, Object>(); 45 listemss.put("heads", R.mipmap.icon_linkedin); 46 ssssss.add(listemss); 47 } 48 SimpleAdapter simplead = new SimpleAdapter(this, listems, 49 R.layout.activity_mainsss, new String[] { "name", "head","pass"}, 50 new int[] {R.id.name,R.id.head}); 51 listView.setAdapter(simplead); 52 SimpleAdapter simpleads = new SimpleAdapter(this, ssssss, 53 R.layout.item_adapter, new String[] {"heads"}, 54 new int[] {R.id.image}); 55 gridView.setAdapter(simpleads); 56 } 57 }
自定义ListView.java
1 package com.wuxianedu.aatoolbarto; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.widget.ListView; 6 7 /** 8 * Created by Administrator on 2016/11/23. 9 */ 10 public class NoScrollListView extends ListView { 11 public NoScrollListView(Context context) { 12 super(context); 13 } 14 public NoScrollListView(Context context, AttributeSet attrs) { 15 super(context, attrs); 16 } 17 18 public NoScrollListView(Context context, AttributeSet attrs, int defStyle) { 19 super(context, attrs, defStyle); 20 } 21 22 @Override 23 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 24 int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 25 super.onMeasure(widthMeasureSpec, expandSpec); 26 } 27 }
ClearEditText.java
1 package com.wuxianedu.aatoolbarto; 2 3 4 import android.content.Context; 5 import android.graphics.drawable.Drawable; 6 import android.text.TextUtils; 7 import android.util.AttributeSet; 8 import android.view.MotionEvent; 9 import android.widget.EditText; 10 11 /** 12 * 带删除图标的EditText 13 * @author Administrator 14 * 15 */ 16 public class ClearEditText extends EditText{ 17 18 private Drawable rightDeleteIcon; 19 public ClearEditText(Context paramContext, AttributeSet paramAttributeSet) { 20 super(paramContext, paramAttributeSet); 21 rightDeleteIcon = getCompoundDrawables()[2]; 22 int size = CoreUtil.dipToPixel(getContext(),18); 23 //设置drawable的 图片的大小 24 rightDeleteIcon.setBounds(0, 0,size,size); 25 } 26 27 @Override 28 public boolean dispatchTouchEvent(MotionEvent event) { 29 if ((rightDeleteIcon != null) && (event.getAction() == MotionEvent.ACTION_DOWN)) { 30 float f1 = event.getX(); 31 float f2 = getWidth() - 80; 32 L.e("f1="+f1+",f2="+f2); 33 if (f1 > f2) 34 setText(null); 35 clearFocus(); 36 } 37 return super.dispatchTouchEvent(event); 38 } 39 40 @Override 41 public boolean onPreDraw() { 42 int width = CoreUtil.dipToPixel(getContext(),100); 43 if (TextUtils.isEmpty(getText()) || getWidth() < width) { 44 setCompoundDrawables(null, null, null, null); 45 } else { 46 setCompoundDrawables(null, null, rightDeleteIcon, null); 47 } 48 return true; 49 } 50 51 }
CoreUtil.java
1 package com.wuxianedu.aatoolbarto; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.regex.Matcher; 6 import java.util.regex.Pattern; 7 8 import android.app.Activity; 9 import android.app.ProgressDialog; 10 import android.content.Context; 11 import android.support.v4.app.FragmentActivity; 12 import android.util.DisplayMetrics; 13 import android.view.WindowManager; 14 15 public class CoreUtil { 16 //加载对话框 17 private static ProgressDialog pd; 18 private static float sDensity = 0; 19 static List<FragmentActivity> list = new ArrayList<FragmentActivity>(); 20 21 /** 22 * 把所有打开的activity加进集合 23 * @param fActivity 24 */ 25 public static void addToActivityList(FragmentActivity fActivity){ 26 if(!list.contains(fActivity)){ 27 list.add(fActivity); 28 } 29 } 30 31 /** 32 * 关闭activity集合 33 */ 34 public static void finishActivityList(){ 35 L.e("activity集合大小-----》》"+list.size()); 36 for (int i = 0; i < list.size(); i++) { 37 L.e("被关闭fragmentActivity-----》》"+list.get(i)); 38 list.get(i).finish(); 39 } 40 list.clear(); 41 } 42 43 /** 44 * 关闭activity 并杀死进程。 45 */ 46 public static void exitApp(){ 47 for (FragmentActivity activity : list) { 48 activity.finish(); 49 L.e("exitApp:-----》》"+activity); 50 } 51 list.clear(); 52 L.e("android.os.Process.killProcess(android.os.Process.myPid());"); 53 android.os.Process.killProcess(android.os.Process.myPid()); 54 } 55 56 57 /** 58 * 开启加载提示对话款 59 */ 60 public static void showProDialog(Context context) { 61 if(pd == null ){ 62 pd = new ProgressDialog(context); 63 pd.setMessage("加载中,请稍后..."); 64 pd.setCancelable(false); 65 } 66 if(pd.isShowing()){ 67 // pd.dismiss(); 68 return; 69 } 70 pd.show(); 71 } 72 /** 73 * 关闭加载提示对话款 74 */ 75 public static void dismissProDialog() { 76 if(pd != null ){ 77 pd.dismiss(); 78 } 79 } 80 81 /** 82 * 将sp值转换为px值,保证文字大小不变 83 * @param context 84 * @param spValue 85 * @return 86 */ 87 public static int spToPixel(Context context, float spValue) { 88 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 89 return (int) (spValue * fontScale + 0.5f); 90 } 91 92 /** 93 * dp转换为像素 94 * @param context 95 * @param nDip 96 * @return 97 */ 98 public static int dipToPixel(Context context, int nDip) { 99 if (sDensity == 0) { 100 final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 101 DisplayMetrics dm = new DisplayMetrics(); 102 wm.getDefaultDisplay().getMetrics(dm); 103 sDensity = dm.density; 104 } 105 return (int) (sDensity * nDip); 106 } 107 108 /** 109 * 获取屏幕 宽 高 110 * @param activity 111 * @return 数组 0 为宽度 1 为高度 112 */ 113 public static int[] getDisplay(Activity activity){ 114 int[] display = new int[2]; 115 DisplayMetrics dm = new DisplayMetrics(); 116 activity.getWindowManager().getDefaultDisplay().getMetrics(dm); 117 display[0] = dm.widthPixels; 118 display[1] = dm.heightPixels; 119 return display; 120 } 121 122 /** 123 * 判断是否为中文 124 * @param str 125 * @return 126 */ 127 public static boolean isChinese(String str){ 128 Pattern p=Pattern.compile("^[u4e00-u9fa5]*$"); 129 Matcher m=p.matcher(str); 130 if(m.matches()){ 131 return true; 132 }else{ 133 return false; 134 } 135 } 136 /** 137 * 判断是否为字母 138 * @param 139 * @return 140 */ 141 public static boolean isEnglish(String fstrData){ 142 char c = fstrData.charAt(0); 143 if(((c>='a'&&c<='z') || (c>='A'&&c<='Z'))){ 144 return true; 145 }else{ 146 return false; 147 } 148 } 149 150 }