android GridView多選效果的實(shí)例代碼
具體代碼如下:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
android:verticalSpacing="3dp"
android:horizontalSpacing="3dp"
android:numColumns="3"
></GridView>
</LinearLayout>
item.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:orientation="vertical" >
<ImageView
android:scaleType="fitXY"
android:padding="3dp"
android:layout_height="70dp"
android:layout_width="70dp"
android:id="@+id/image_item"
/>
</LinearLayout>
mainActivity.java
package com.imageview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private Adpter adpter;
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int [] image={R.drawable.dog1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5,R.drawable.dog2};
adpter=new Adpter(image, this);
gridView=(GridView) findViewById(R.id.grid);
gridView.setAdapter(adpter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
adpter.chiceState(position);
}
});
}
}
Adpter.java
package com.imageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class Adpter extends BaseAdapter {
private int[] image;
private boolean isChice[];
private Context context;
public Adpter(int[] im, Context context) {
this.image = im;
Log.i("hck", im.length+"lenght");
isChice=new boolean[im.length];
for (int i = 0; i < im.length; i++) {
isChice[i]=false;
}
this.context = context;
}
@Override
public int getCount() {
return image.length;
}
@Override
public Object getItem(int arg0) {
return image[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View view = arg1;
GetView getView=null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item, null);
getView = new GetView();
getView.imageView=(ImageView) view.findViewById(R.id.image_item);
view.setTag(getView);
} else {
getView = (GetView) view.getTag();
}
getView.imageView.setImageDrawable(getView(arg0));
return view;
}
static class GetView {
ImageView imageView;
}
//主要就是下面的代碼了
private LayerDrawable getView(int post) {
Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(image[post])).getBitmap();
Bitmap bitmap2=null;
LayerDrawable la=null;
if (isChice[post]== true){
bitmap2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.editable_mode_checked_tag);
}
if (bitmap2!=null) {
Drawable[] array = new Drawable[2];
array[0] = new BitmapDrawable(bitmap);
array[1] = new BitmapDrawable(bitmap2);
la= new LayerDrawable(array);
la.setLayerInset(0, 0, 0, 0, 0); //第幾張圖離各邊的間距
la.setLayerInset(1, 0, 65, 65, 0);
}
else {
Drawable[] array = new Drawable[1];
array[0] = new BitmapDrawable(bitmap);
la= new LayerDrawable(array);
la.setLayerInset(0, 0, 0, 0, 0);
}
return la; // 返回疊加后的圖
}
public void chiceState(int post)
{
isChice[post]=isChice[post]==true?false:true;
this.notifyDataSetChanged();
}
}
- android ItemTouchHelper實(shí)現(xiàn)可拖拽和側(cè)滑的列表的示例代碼
- Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
- Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
- Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
- Android的ListView多選刪除操作實(shí)現(xiàn)代碼
- Android中創(chuàng)建對(duì)話框(確定取消對(duì)話框、單選對(duì)話框、多選對(duì)話框)實(shí)例代碼
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android自定義控件實(shí)現(xiàn)可多選課程日歷CalendarView
- Android實(shí)現(xiàn)可拖拽列表和多選功能
相關(guān)文章
android Launcher AppWidget添加步驟介紹
大家好,本篇文章主要講的是android Launcher AppWidget添加步驟介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能(示例代碼)
在一些商場(chǎng)購(gòu)物時(shí),不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實(shí)現(xiàn)消費(fèi)簽名,非常方便,下面小編給大家分享Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能,需要的朋友參考下吧2017-12-12Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果
這篇文章主要介紹了Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android 實(shí)現(xiàn)長(zhǎng)按彈出PopupMenu 菜單欄
這篇文章主要介紹了Android 實(shí)現(xiàn)長(zhǎng)按彈出PopupMenu 菜單欄,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語(yǔ)言中類與屬性及構(gòu)造函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Android開發(fā)Compose集成高德地圖實(shí)例
這篇文章主要為大家介紹了Android開發(fā)Compose里使用高德地圖實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android實(shí)現(xiàn)向本地寫入一個(gè)XML文件和解析XML文件
這篇文章主要介紹了Android實(shí)現(xiàn)向本地寫入一個(gè)XML文件和解析XML文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android通過(guò)代碼控制ListView上下滾動(dòng)的方法
今天小編就為大家分享一篇關(guān)于Android通過(guò)代碼控制ListView上下滾動(dòng)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12