今天自習寫ExpandListView的作業,昨天沒寫博客就是去寫作業去了。
今天來說昨天內容吧!
其實ExpandListView和ListView的用法大同小異。
首先就是創建一個自己的適配器(現在好像只會寫自定義的適配器)
創建的時候繼承BaseExpandListView,然后實現里面的方法。
這里一個個方法重寫下,出來getView的兩個方法難寫點,其他的還都簡單。
一個個解釋哈,我們剛開始實現接口的抽象方法后,創建自己需要的字符串,
就拿手機QQ的聯系人界面來說
group的作用就是給分組命名的字符串,每個都有一個字符串,所以用字符串數組
child就是你分組的人,以此類推是不是就是二維字符串數組。
至於geyan我這邊想寫那個個性簽名的那些話傳過來
getGroupCount:返回分組的個數
getChildrenCount:分組里面的二級菜單個數。(二級菜單就是聯系人,比如特別關心里面的張三)
getGroup:就是獲得這個組當前的下標的值
getChild:就是獲得這個二級菜單當前的下標的值
getGroupId:給每個分組分配id=下標
getChildId:你懂的
hasStableIds:這個是看數據是否穩定,就是是否有重復id,大概這個意思,我一般是true,穩定總 是好的,當然視情況而定吧
getGroupView:這個懂吧,先得在layout里寫好布局,然后實例化連接它。然后就給它連接控件進行操作。
getChildView:這個就是給二級菜單用的布局,也得寫好布局xml,千萬不要搞混了,或者傳個控件給實例化。
isChildSelectable:這個是說二級菜單可不可選。
繼續,我們寫好適配器就是連接ExpandListView。
自己寫的適配器new個對象,三個參數就是一個字符串數組做分組,兩個字符串二維數組做二級菜單的內容。然后就是監聽,第一個是二級菜單的監聽,第二個是分組的監聽。
還是比較簡單的,然后自己注意寫好三個布局,應該不會難看(本活動的布局,分組的布局,二級菜單的布局)
我做出的效果就是如圖:
做了一些修飾,其實我把這個融入到我昨天寫的界面里去了。
會不會奇怪為啥有那個三角形不打開是右邊,打開卻在下面。這里說下選擇器。
在drawable里面創建一個select.xml。然后在里面寫:
這里的作用就是看你這個是不是展開的,如果不是就右三角形,這兩個三角形是在阿里巴巴矢量圖標庫里下載過來的。
怎么用呢?,展開是expand,我們學的也是ExpandListView,肯定是在布局中找到控件並搞地址。
可以看到groupIndicator,這個就是它的指針圖標,選擇器就相當於監聽。
l
補充個內容,就是圖片的裁剪,變圓形,這個得自己寫個Bitmap,
package com.example.listview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MyImage extends androidx.appcompat.widget.AppCompatImageView {
private Paint paint = new Paint();
public MyImage(Context context) {
super(context);
}
public MyImage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//將頭像按比例縮放
private Bitmap scaleBitmap(Bitmap bitmap){
int width = getWidth();
//一定要強轉成float 不然有可能因為精度不夠 出現 scale為0 的錯誤
float scale = (float)width/(float)bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
//將原始圖像裁剪成正方形
private Bitmap dealRawBitmap(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//獲取寬度
int minWidth = width > height ? height:width ;
//計算正方形的范圍
int leftTopX = (width - minWidth)/2;
int leftTopY = (height - minWidth)/2;
//裁剪成正方形
Bitmap newBitmap = Bitmap.createBitmap(bitmap,leftTopX,leftTopY,minWidth,minWidth,null,false);
return scaleBitmap(newBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (null != drawable) {
Bitmap rawBitmap =((BitmapDrawable)drawable).getBitmap();
//處理Bitmap 轉成正方形
Bitmap newBitmap = dealRawBitmap(rawBitmap);
//將newBitmap 轉換成圓形
Bitmap circleBitmap = toRoundCorner(newBitmap, 14);
final Rect rect = new Rect(0, 0, circleBitmap.getWidth(), circleBitmap.getHeight());
paint.reset();
//繪制到畫布上
canvas.drawBitmap(circleBitmap, rect, rect, paint);
} else {
super.onDraw(canvas);
}
}
private Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
//指定為 ARGB_4444 可以減小圖片大小
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Rect rect = new Rect(0, 0,bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
int x = bitmap.getWidth();
canvas.drawCircle(x / 2, x / 2, x / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
這個就是自己創個class,寫進去就行
然后自己用圖片的時候就<MyImage 等提示找到那個路徑和自己差不多的,
<com.example.listview.MyImage
android:id="@+id/image_p"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
android:src="@drawable/pika1"
/>
用法和ImageView一樣。
再提一下自己改標題,每次是不是看着那個標題總是自己的項目名稱,是不是看着不爽。
我們先寫一個自己想要的標題,本牛崽這邊是title_right
再到你的活動相對應的布局里去調用這句話
<include layout="@layout/title_right"></include>
你會發現有兩個標題在那排着,
這個時候得到活動里去隱藏默認的標題。
在onCreate方法里,
ActionBar actionbar = getSupportActionBar();
if (actionbar != null) {
actionbar.hide();
}
本牛崽百度搞到的,
就這些了。。。好像還比較簡單吧,得自己多打,現在有的兄弟姐妹前面的代碼沒打,每天聽理論,現在都不知道下手了。
一定得多敲,腦子記憶會刷新,手指記憶會記很久。