android實(shí)現(xiàn)RecyclerView列表單選功能
本文實(shí)例為大家分享了android實(shí)現(xiàn)RecyclerView列表單選功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)思維
1.首先在一行的xml布局中添加一個(gè)選中效果的icon圖片,未選中的情況下INVISIBLE或者GONE 都可以,推薦使用INVISIBLE它會(huì)占用布局位置但是不顯示,這樣可以避免布局中其他控件因?yàn)楣催x布局的消失而輕微變動(dòng)位置
2.將適配器類中的onCreateViewHolder方法重寫添加按鍵監(jiān)聽,onBindViewHolder方法中重寫添加判斷點(diǎn)擊的位置(具體原理請(qǐng)查看下面貼的代碼)
PS:
RecyclerView的實(shí)現(xiàn)全部方法就不貼出來了,本人的博客中有RecyclerView各種實(shí)現(xiàn)效果文章
1.首先在一行的xml布局中添加一個(gè)選中效果的icon圖片,未選中的情況下INVISIBLE或者GONE 都可以,推薦使用INVISIBLE它會(huì)占用布局位置但是不顯示,這樣可以避免布局中其他控件因?yàn)楣催x布局的消失而輕微變動(dòng)位置
<?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="wrap_content">
<LinearLayout
android:id="@+id/DeviceList_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<ImageView
android:id="@+id/DeviceList_ImageView"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:src="@mipmap/preview_image_boy"
android:layout_gravity="center"/>
<TextView
android:id="@+id/DeviceList_NameText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="名稱"
android:textSize="@dimen/BigTextSize"
android:textColor="@color/colorBlue"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:maxLines="1"/>
<ImageView
android:id="@+id/DeviceList_Select"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_select"
android:visibility="invisible"
android:layout_marginLeft="10dp"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/colorBlue"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
</LinearLayout>
</LinearLayout>
效果圖:

2.將適配器類中的onCreateViewHolder方法重寫添加按鍵監(jiān)聽,onBindViewHolder方法中重寫添加判斷點(diǎn)擊的位置(具體原理請(qǐng)查看下面貼的代碼)
package com.example.lenovo.mydemoapp.myDeviceList;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lenovo.mydemoapp.R;
import java.util.List;
/**
* Created by lenovo on 2018/5/18.
*/
public class DeivceListAdapter extends RecyclerView.Adapter<DeivceListAdapter.ViewHolder> {
private List<DeivceListData> mList;
private int mposition = -1;
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mListImage,mListSelect;
TextView mListNameText;
LinearLayout mListLayout;
public ViewHolder(View itemView) {
super(itemView);
mListImage = (ImageView)itemView.findViewById(R.id.DeviceList_ImageView);
mListNameText = (TextView)itemView.findViewById(R.id.DeviceList_NameText);
mListLayout = (LinearLayout)itemView.findViewById(R.id.DeviceList_Layout);
mListSelect = (ImageView)itemView.findViewById(R.id.DeviceList_Select);
}
}
public DeivceListAdapter (List<DeivceListData> list){
this.mList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_list_row_layout,parent,false);
final ViewHolder holder = new ViewHolder(view);
/*
添加選中的打勾顯示
*/
holder.mListLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//將點(diǎn)擊的位置傳出去
mposition = holder.getAdapterPosition();
//在點(diǎn)擊監(jiān)聽里最好寫入setVisibility(View.VISIBLE);這樣可以避免效果會(huì)閃
holder.mListSelect.setVisibility(View.VISIBLE);
//刷新界面 notify 通知Data 數(shù)據(jù)set設(shè)置Changed變化
//在這里運(yùn)行notifyDataSetChanged 會(huì)導(dǎo)致下面的onBindViewHolder 重新加載一遍
notifyDataSetChanged();
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
DeivceListData deivceListData = mList.get(position);
holder.mListImage.setImageResource(deivceListData.getmDeviceListDataImage());
holder.mListNameText.setText(deivceListData.getmDeviceListDataNameText());
/*
onBindViewHolder 方法可能是在class里for添加了其他視圖
引入mposition與當(dāng)前的position判斷,判斷在點(diǎn)擊的位置上顯示打勾圖片,在其他位置上不顯示打勾
*/
if (position == mposition) {
holder.mListSelect.setVisibility(View.VISIBLE);
} else {
holder.mListSelect.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return mList.size();
}
}
實(shí)現(xiàn)效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問題
- Android RecyclerView實(shí)現(xiàn)滑動(dòng)刪除
- Android列表RecyclerView排列布局
- Android Studio使用recyclerview實(shí)現(xiàn)展開和折疊功能(在之前的微信頁面基礎(chǔ)之上)
- Android recyclerView橫條指示器實(shí)現(xiàn)淘寶菜單模塊
- Android RecyclerView實(shí)現(xiàn)懸浮吸頂、分隔線、到底提示效果
- Android無限循環(huán)RecyclerView的完美實(shí)現(xiàn)方案
- Android中解決RecyclerView各種點(diǎn)擊事件的方法
- Android RecyclerView網(wǎng)格布局示例解析
相關(guān)文章
Flutter開發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Flutter開發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)顏色漸變動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)顏色漸變動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
解決Android MediaRecorder錄制視頻過短問題
本文主要介紹Android MediaRecorder,在使用MediaRecorder時(shí)經(jīng)常會(huì)遇到視頻錄制太短問題,這里提供解決問題的實(shí)例代碼以供大家參考2016-07-07
一個(gè)吸頂Item的簡(jiǎn)單實(shí)現(xiàn)方法分享
這篇文章主要給大家介紹了一個(gè)吸頂Item的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
android仿微信表情雨下落效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于android仿微信表情雨下落效果的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android中實(shí)現(xiàn)圖文并茂的按鈕實(shí)例代碼
這篇文章主要介紹了Android中實(shí)現(xiàn)圖文并茂的按鈕實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友可以參考下2017-04-04
Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局
這篇文章主要介紹了Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局,文中講解了創(chuàng)建可復(fù)用布局的例子以及include標(biāo)簽和merge標(biāo)簽使用例子,需要的朋友可以參考下2014-06-06
Android五種隱藏狀態(tài)欄和標(biāo)題欄的方法
這篇文章主要介紹了Android五種隱藏狀態(tài)欄和標(biāo)題欄的方法的相關(guān)資料,需要的朋友可以參考下2017-05-05

