Step 1: 首先上效果圖
我們要實(shí)現(xiàn)的效果是在一個(gè)Recyclerview的網(wǎng)格布局中,長(zhǎng)按出現(xiàn)checkbox以及底部按鈕。可以記錄下我們選中的條目并顯示它的位置。你可以在這里進(jìn)行你想要的操作。
Step 2: 功能實(shí)現(xiàn)
- 每個(gè)item的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/imgv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
fresco:backgroundImage="@mipmap/imgv_fitness_gril"
fresco:placeholderImage="@mipmap/imgv_fitness_gril"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundedCornerRadius="50dp" />
<CheckBox
android:id="@+id/cb_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/imgv_item"
android:layout_alignRight="@id/imgv_item"
android:clickable="false" />
</RelativeLayout>
每個(gè)item大概就是這個(gè)樣子:
從圖中我們可以看出,它是一個(gè)有邊框且右下角有一個(gè)Checkbox的布局。一般把android:clickable
設(shè)置為false
,然后通過響應(yīng)item的點(diǎn)擊事件設(shè)置holder.mCbItem.setChecked(boolean)
來控制我們checkbox的選中與否。得益于Frasco的強(qiáng)大功能,可以在xml文件中輕松設(shè)置各個(gè)角的圓角。
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundedCornerRadius="50dp"
這就是一個(gè)左上和右下是50dp圓角的形狀,(在截圖中沒有體現(xiàn)出來,但是程序運(yùn)行起來是可以看到圓角的)。當(dāng)然你可以把Checkbox放在其它任何你想要的位置。
- 列表頁的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.txs.multiplepicselect.MainActivity">
<TextView
android:background="#6CC4B8"
android:textColor="#fff"
android:gravity="center"
android:id="@+id/title"
android:text="圖片長(zhǎng)按多選"
android:layout_width="match_parent"
android:layout_height="40dp" />
<android.support.v7.widget.RecyclerView
android:layout_below="@id/title"
android:id="@+id/rcv"
android:layout_above="@id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_margin="5dp"
android:background="@drawable/selector_activity_login"
android:visibility="gone"
android:textColor="#fff"
android:text="選中的數(shù)據(jù)"
android:id="@+id/btn"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="40dp" />
</RelativeLayout>
整個(gè)布局也是很簡(jiǎn)單,頂部是一個(gè)title(僅僅是為了好看而已),上部是一個(gè)Recyclerview實(shí)現(xiàn)網(wǎng)格布局,下方是一個(gè)模仿發(fā)送功能的按鈕,長(zhǎng)按出現(xiàn),再次長(zhǎng)按則隱藏掉。順便提一下,如果你是在項(xiàng)目中初次使用Recyclerview的話,在引入Recyclerview的時(shí)候,千萬注意Recyclerview的版本要和你的support:appcompat-v7
版本相同,即像這樣:
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
里面的26.1.0
要相同的哦。
- 下面是我們Recyclerview的適配器:
/**
* @author txs
* @date 2018/01/14
*/
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.MyViewHolder> {
private Context context;
/**
* 控制是否顯示Checkbox
*/
private boolean showCheckBox;
/**
* 屏幕寬度 我們要?jiǎng)討B(tài)設(shè)置每個(gè)item大小為屏幕寬度的1/3
*/
private int screenWidth;
/**
* 設(shè)置每個(gè)item 的params(大小)
*/
private GridLayoutManager.LayoutParams params;
/**
* frasco 使用
*/
private Uri uri;
public RecAdapter(Context context, List<String> list, int screenWidth) {
this.context = context;
this.list = list;
this.screenWidth = screenWidth;
//frasco 使用
uri = Uri.parse("res:///" + R.mipmap.imgv_fitness_gril);
}
public boolean isShowCheckBox() {
return showCheckBox;
}
public void setShowCheckBox(boolean showCheckBox) {
this.showCheckBox = showCheckBox;
}
/**
* 這就是適配器要傳過來的數(shù)據(jù)集合了
*/
private List<String> list = new ArrayList<>();
/**
* 防止Checkbox錯(cuò)亂 做setTag getTag操作
*/
private SparseBooleanArray mCheckStates = new SparseBooleanArray();
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_pic, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
//防止復(fù)用導(dǎo)致的checkbox顯示錯(cuò)亂
holder.mCbItem.setTag(position);
//設(shè)置item寬高為屏幕寬度的1/3
params = (GridLayoutManager.LayoutParams) holder.mRlItem.getLayoutParams();
params.width = screenWidth / 3;
params.height = screenWidth / 3;
//判斷當(dāng)前checkbox的狀態(tài)
if (showCheckBox) {
holder.mCbItem.setVisibility(View.VISIBLE);
//防止顯示錯(cuò)亂
holder.mCbItem.setChecked(mCheckStates.get(position, false));
} else {
holder.mCbItem.setVisibility(View.GONE);
//取消掉Checkbox后不再保存當(dāng)前選擇的狀態(tài)
holder.mCbItem.setChecked(false);
mCheckStates.clear();
}
//點(diǎn)擊監(jiān)聽
holder.mRlItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (showCheckBox) {
holder.mCbItem.setChecked(!holder.mCbItem.isChecked());
}
onItemClickListener.onClick(view, position);
}
});
//長(zhǎng)按監(jiān)聽
holder.mRlItem.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return onItemClickListener.onLongClick(view, position);
}
});
//對(duì)checkbox的監(jiān)聽 保存選擇狀態(tài) 防止checkbox顯示錯(cuò)亂
holder.mCbItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int pos = (int) compoundButton.getTag();
if (b) {
mCheckStates.put(pos, true);
} else {
mCheckStates.delete(pos);
}
}
});
//frasco 使用
holder.mImgvItem.setImageURI(uri);
// Glide.with(context).load(R.mipmap.imgv_fitness_gril).into(holder.mImgvItem);
}
@Override
public int getItemCount() {
//暫時(shí)做60個(gè)模擬數(shù)據(jù)
return 60;
}
/**
* 自己寫接口,實(shí)現(xiàn)點(diǎn)擊和長(zhǎng)按監(jiān)聽
*/
public interface onItemClickListener {
void onClick(View view, int pos);
boolean onLongClick(View view, int pos);
}
private onItemClickListener onItemClickListener;
public void setOnItemClickListener(RecAdapter.onItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private RelativeLayout mRlItem;
private SimpleDraweeView mImgvItem;
private CheckBox mCbItem;
public MyViewHolder(View itemView) {
super(itemView);
mRlItem = (RelativeLayout) itemView.findViewById(R.id.rl_item);
mImgvItem = (SimpleDraweeView) itemView.findViewById(R.id.imgv_item);
mCbItem = (CheckBox) itemView.findViewById(R.id.cb_item);
}
}
}
整個(gè)適配器的大致流程如下:
首先,在創(chuàng)建適配器時(shí)傳進(jìn)來screenWidth
,即屏幕寬度。隨后我們可以在onBindViewHolder
方法中設(shè)置Item寬高都為屏幕的1/3。增強(qiáng)布局的美觀性。
隨后,最應(yīng)該解決的就是Recyclerviewl復(fù)用的問題。類似于ListView的satTag,防止Checkbox顯示錯(cuò)亂。所以利用private SparseBooleanArray mCheckStates = new SparseBooleanArray();
,通過mCheckStates
中儲(chǔ)存的boolean
狀態(tài)設(shè)置當(dāng)前Checkbox的選中狀態(tài)。
最后,Recyclerview沒有點(diǎn)擊監(jiān)聽對(duì)我們平時(shí)開發(fā)來說確實(shí)有些坑,但是也給了我們高度的自由去自行定制點(diǎn)擊監(jiān)聽。我們可以自己寫個(gè)接口實(shí)現(xiàn)我們的點(diǎn)擊和長(zhǎng)按效果(注意長(zhǎng)按監(jiān)聽onLongClick
返回值是boolean
)。
- MainActivity
public class MainActivity extends AppCompatActivity {
/**
* 網(wǎng)格布局的 Recyclerview
*/
private RecyclerView mRcv;
/**
* 顯示所保存數(shù)據(jù)的按鈕
*/
private Button mBtn;
/**
* recyclerview 的適配器
*/
private RecAdapter adapter;
/**
* 實(shí)際開發(fā)中用來保存聯(lián)網(wǎng)獲取的圖片數(shù)據(jù)
*/
private List<String> list;
/**
* 是否顯示checkbox
*/
private boolean isShowCheck;
/**
* 記錄選中的checkbox
*/
private List<String> checkList;
/**
* 屏幕寬度
*/
private int screenWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//每行3個(gè)的Recyclerview網(wǎng)格布局
mRcv.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
refreshUI();
initListener();
}
/**
* 適配器
*/
private void refreshUI() {
if (adapter == null) {
adapter = new RecAdapter(this, list, screenWidth);
mRcv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
/**
* 點(diǎn)擊監(jiān)聽
*/
private void initListener() {
//button的點(diǎn)擊
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, checkList.toString(), Toast.LENGTH_SHORT).show();
}
});
//adapter中定義的監(jiān)聽事件 可以根據(jù)isShowCheck判斷當(dāng)前狀態(tài),設(shè)置點(diǎn)擊Item之后是查看大圖(未實(shí)現(xiàn) 跳到下一個(gè)Activity即可)還是選中checkbox*/
adapter.setOnItemClickListener(new RecAdapter.onItemClickListener() {
@Override
public void onClick(View view, int pos) {
if (checkList.contains(String.valueOf(pos))) {
checkList.remove(String.valueOf(pos));
} else {
checkList.add(String.valueOf(pos));
}
}
@Override
public boolean onLongClick(View view, int pos) {
if (isShowCheck) {
mBtn.setVisibility(View.GONE);
adapter.setShowCheckBox(false);
refreshUI();
checkList.clear();
} else {
adapter.setShowCheckBox(true);
refreshUI();
mBtn.setVisibility(View.VISIBLE);
}
isShowCheck = !isShowCheck;
return false;
}
});
}
private void initData() {
list = new ArrayList<>();
checkList = new ArrayList<>();
list.add("1");
//屏幕寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
}
private void initView() {
mRcv = (RecyclerView) findViewById(R.id.rcv);
mBtn = (Button) findViewById(R.id.btn);
}
}
這里最應(yīng)該講的是這個(gè)checkList,通過它來保存我們點(diǎn)擊過的Item信息,如果點(diǎn)擊過則再次點(diǎn)擊時(shí)remove
掉此信息,否則add
進(jìn)來。
寫的不好,還請(qǐng)多多指教。
github項(xiàng)目地址:https://github.com/tangxuesong6/multiplepicselect/tree/master。