美文网首页
Android ItemTouchHelper 实现账号历史记

Android ItemTouchHelper 实现账号历史记

作者: xq9527 | 来源:发表于2022-12-27 11:50 被阅读0次

    前言:

    各位同学大家好,有段时间没有给大家更新文章了,具体多久我也不记得了。因为最近在工作更新了一些新功能,用到了ItemTouchHelper 所以就记录一下

    效果图

    screenrecorder-2022-12-13-10-57-47-573[00_00_12--00_00_32].gif
    image.png
    image.png
    image.png

    具体实现 :

    布局文件

    <?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=".LoginActivity">
        <LinearLayout
            android:layout_width="360dp"
            android:layout_height="360dp"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:layout_gravity="center"
             android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/activity_background_white">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="56.89dp"
                android:background="@drawable/activity_head_background_gray">
                <TextView
                    android:id="@+id/login_account_headlinetext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/account_login"
                    android:textSize="25.2dp"
                    android:layout_centerInParent="true"
                    android:textColor="@color/activityTitle"
                    android:visibility="visible"
                    />
    
    
                <RelativeLayout
                    android:id="@+id/help_imageview_rl"
                    android:layout_width="88.89dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:visibility="visible"
                    >
                    <ImageView
                        android:id="@+id/help_imageview"
                        android:layout_width="62.67dp"
                        android:layout_height="16.89dp"
                        android:src="@drawable/help"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="20.44dp"
                        android:visibility="visible"
                        />
                </RelativeLayout>
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="122.58dp"
                android:layout_marginTop="7.64dp">
    
                <LinearLayout
                    android:layout_width="300dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_centerInParent="true">
                    <!---->
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="59.48dp"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        >
                        <ImageView
                            android:layout_width="23.11dp"
                            android:layout_height="23.56dp"
                            android:src="@drawable/user_image"
                            android:id="@+id/user_icon_image"
                            android:layout_centerVertical="true"
                            />
                        <RelativeLayout
                            android:id="@+id/select_loginaccount_rl"
                            android:layout_width="44.45dp"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true">
                            <ImageView
                                android:id="@+id/select_loginaccount_image"
                                android:layout_width="16.45dp"
                                android:layout_height="8.89dp"
                                android:layout_alignParentRight="true"
                                android:layout_marginRight="16.89dp"
                                android:layout_centerVertical="true"
                                android:src="@drawable/account_list"/>
                        </RelativeLayout>
                        <ImageView
                            android:id="@+id/account_login_detete"
                            android:layout_width="23.56dp"
                            android:layout_height="23.11dp"
                            android:layout_marginRight="4.45dp"
                            android:visibility="gone"
                            android:layout_centerVertical="true"
                            android:src="@drawable/delete_image"
                            android:layout_toLeftOf="@id/select_loginaccount_rl"
                            />
                        <EditText
                            android:id="@+id/login_account_ed"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_alignParentBottom="true"
                            android:layout_marginLeft="10dp"
                            android:layout_marginBottom="0dp"
                            android:layout_toLeftOf="@id/account_login_detete"
                            android:layout_toRightOf="@id/user_icon_image"
                            android:background="@null"
                            android:editable="true"
                            android:gravity="center_vertical"
                            android:hint="@string/account_input_tips"
                            android:imeOptions="actionNext"
                            android:textColor="@color/activityTitle"
                            android:textColorHint="@color/activityText"
                            android:textSize="20dp" />
                    </RelativeLayout>
                    <!--  android:layout_marginTop="12.45dp"-->
                    <TextView
                        android:id="@+id/login_account_textview"
                        android:layout_width="300dp"
                        android:layout_height="1.78dp"
                        android:background="@color/activityTextLine"
                        />
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="59.48dp"
                        >
    
    
                        <ImageView
                            android:layout_width="23.11dp"
                            android:layout_height="23.56dp"
                            android:src="@drawable/account_password"
                            android:id="@+id/user_password_image"
                            android:layout_centerVertical="true"
                            />
                        <ImageView
                            android:id="@+id/look_psw_image"
                            android:layout_width="33.33dp"
                            android:layout_height="18.22dp"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="7.56dp"
                            android:layout_centerVertical="true"
                            android:src="@drawable/account_list_eye"
                            />
                        <ImageView
                            android:id="@+id/account_loginpsw_detete"
                            android:layout_width="23.56dp"
                            android:layout_height="23.11dp"
                            android:layout_marginRight="8dp"
                            android:visibility="gone"
                            android:layout_centerVertical="true"
                            android:src="@drawable/delete_image"
                            android:layout_toLeftOf="@id/look_psw_image"
                            />
                        <EditText
                            android:id="@+id/account_login_psw"
                            android:layout_width="182.22dp"
                            android:layout_height="match_parent"
                            android:layout_centerVertical="true"
                            android:hint="@string/password_input_tips"
                            android:textColorHint="@color/activityText"
                            android:layout_marginLeft="35.56dp"
                            android:background="@null"
                            android:gravity="center_vertical"
                            android:imeOptions="actionNext"
                            android:textColor="@color/activityTitle"
                            android:textSize="20dp"
                            android:password="true"
                            />
                    </RelativeLayout>
                    <TextView
                        android:layout_width="300dp"
                        android:layout_height="1.78dp"
                        android:background="@color/activityTextLine"
                        />
    
                </LinearLayout>
    
            </RelativeLayout>
    
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="24.45dp"
                android:layout_marginTop="22.22dp"
                >
    
    
                <RelativeLayout
                    android:id="@+id/automaticlogin_rl"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
    
                    <ImageView
                        android:id="@+id/automaticlogin_image"
                        android:layout_width="15.11dp"
                        android:layout_height="15.11dp"
                        android:layout_gravity="center"
                        android:src="@drawable/accept_useragreement_true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="33.78dp"
                        />
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@id/automaticlogin_image"
                        android:layout_centerVertical="true"
                        android:text="自动登录"
                        android:textSize="18.67dp"
                        android:layout_marginLeft="8.89dp"
                        />
                </RelativeLayout>
                <LinearLayout
                    android:id="@+id/forget_password_rl"
                    android:layout_width="100.45dp"
                    android:layout_height="24.45dp"
                    android:orientation="vertical"
                    android:layout_marginRight="22.22dp"
                    android:layout_alignParentRight="true">
    
                    <TextView
                        android:id="@+id/forget_password_text"
                        android:layout_width="match_parent"
                        android:layout_height="22.22dp"
                        android:textColor="@color/activityYelloButton"
                        android:text="@string/forget_password"
                        android:layout_gravity="center"
                        android:textSize="18.67dp"
                        />
    
    
                </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_marginTop="12.45dp"
                android:layout_width="match_parent"
                android:layout_height="53.33dp">
                <Button
                    android:id="@+id/promptly_login"
                    android:layout_width="310.67dp"
                    android:layout_height="56.89dp"
                    android:textSize="24dp"
                    android:layout_marginLeft="6.22dp"
                    android:layout_centerInParent="true"
                    android:textColor="@color/activityButtonTextColor"
                    android:background="@drawable/blue_button_style"
                    android:text="@string/now_login"/>
            </RelativeLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="26.67dp"
                android:layout_marginTop="22.22dp"
                android:orientation="horizontal"
                android:visibility="gone"
                >
                <RelativeLayout
                    android:id="@+id/mobile_phone_rl"
                    android:layout_width="0px"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    >
                    <TextView
                        android:id="@+id/mobile_phone_text"
                        android:layout_width="84.44dp"
                        android:layout_height="match_parent"
                        android:textSize="20.44dp"
                        android:layout_alignParentLeft="true"
                        android:textColor="@color/activityTitle"
                        android:layout_marginLeft="33.33dp"
                        android:text="@string/message_register"/>
                </RelativeLayout>
                <RelativeLayout
                    android:id="@+id/register_rl"
                    android:layout_width="0px"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    >
                    <TextView
                        android:id="@+id/register_textview"
                        android:layout_width="84.44dp"
                        android:layout_height="match_parent"
                        android:textSize="20.44dp"
                        android:textColor="@color/activityTitle"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="33.33dp"
                        android:text="手机注册"
                        />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
    
    

    布局预览

    image.png

    我们要实现的功能就是要把登录后的数据缓存到一个list 列表里面 然后能够滑动 删除 还可以长按移动每一个子元素 。

    • 1 缓存数据 并用list列表展示出来

    首先缓存逻辑我们要用到 SharedPreferences 来缓存数据 当然你用sqlite 也可以 我后面会补一个这样的版本

    自定义PopupWindow
    package com.example.accountrecord;
    import java.util.List;
    import android.content.Context;
    import android.graphics.drawable.ColorDrawable;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.PopupWindow;
    import androidx.recyclerview.widget.ItemTouchHelper;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    import com.example.accountrecord.recycler.MessageitemtochCallback;
    import com.example.accountrecord.recycler.Myadapter;
    import com.example.accountrecord.recycler.OnItemClickCallback;
    import com.example.accountrecord.recycler.StartdragCallback;
    
    
    /**
     * 自定义PopupWindow  主要用来显示ListView
     *
     * @param
     * @param
     * @author Ansen
     * @create time 2015-11-3
     */
    public class SpinerPopWindow extends PopupWindow {
        private static final String TAG = "SpinerPopWindow";
        private LayoutInflater inflater;
        private List<SelectPhone> list;
        private Context context;
        private RemoveUserinfoListner removeUserinfoListner;  //删除item 数据响应回调
        private RecyclerView recyclerView;
        private Myadapter myadapter;
        ItemTouchHelper itemTouchHelper;  
        private OnItemClickCallback onItemClickCallback;   //item 点击回调
    
    
        public SpinerPopWindow(Context context, List<SelectPhone> list,
                               OnItemClickCallback onItemClickCallback, RemoveUserinfoListner removeUserinfoListner) {
            super(context);
            inflater = LayoutInflater.from(context);
            this.list = list;
            this.context = context;
            this.removeUserinfoListner = removeUserinfoListner;
            this.onItemClickCallback=onItemClickCallback;
            init();
        }
        private void init() {
            View view = inflater.inflate(ResourceUtil.getLayoutId(context, "select_phonedialog"), null);
            setContentView(view);
            setFocusable(true);
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            ColorDrawable dw = new ColorDrawable(0x00);
            setBackgroundDrawable(dw);
            recyclerView=view.findViewById(R.id.select_listview);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            myadapter=new Myadapter(list, context,removeUserinfoListner,onItemClickCallback,new StartdragCallback() {
                @Override
                public void startdrag(RecyclerView.ViewHolder viewHolder) {
                    itemTouchHelper.startDrag(viewHolder);
                }
            });
            recyclerView.setAdapter(myadapter);
            ItemTouchHelper.Callback callback=new MessageitemtochCallback(myadapter);
            itemTouchHelper=new ItemTouchHelper(callback);
            itemTouchHelper.attachToRecyclerView(recyclerView);
            if (list != null) {
                recyclerView.setAdapter(myadapter);
            }
        }
    }
    

    自定义popupwindow 布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
        <RelativeLayout
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:background="@color/activityTextWhite"
            android:layout_centerHorizontal="true"
            >
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/select_listview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </androidx.recyclerview.widget.RecyclerView>
        </RelativeLayout>
    </RelativeLayout>
    
    image.png

    我们这边的想法比较明确就是 我们在点击登录输入框右边图片三角形 我们讲popupwindow 弹出来 然后展示一个列表 显示出我们缓存在本地的账号 信息 那么我们这边用 RecyclerView 来显示这个列表.但是如果我们要处理拖住item 滑动item 等到操作 我们就需要实现 ItemTouchHelper .callback 这个接口

    package com.example.accountrecord.recycler;
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.ItemTouchHelper;
    import androidx.recyclerview.widget.RecyclerView;
    
    
    public class MessageitemtochCallback extends ItemTouchHelper.Callback {
        private  ItemTouchAdapterCallback itemTouchAdapterCallback;
        public MessageitemtochCallback(ItemTouchAdapterCallback itemTouchAdapterCallback) {
            this.itemTouchAdapterCallback = itemTouchAdapterCallback;
        }
    
    
        /**
         * @param recyclerView
         * @param viewHolder
         * @return
         * 判断是否需要监听哪个动作 侧滑 拖拽
         */
        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
           int  dragFlags=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
           int  swipeFlags=ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            return makeMovementFlags(dragFlags,swipeFlags);
        }
    
        /**
         * @param recyclerView
         * @return
         * 拖拽的时候回调这个方法
         *
         */
    
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder srcholder,
                              @NonNull RecyclerView.ViewHolder targethodler) {
    
            /**
             * 刷新数据
             * 刷新界面(刷新adapter)
             *
             */
           itemTouchAdapterCallback.onitemmove(srcholder.getAdapterPosition(),targethodler.getAdapterPosition());
            return false;
        }
        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
         itemTouchAdapterCallback.onitemSwiped(viewHolder.getAdapterPosition());
        }
        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }
    }
    

    我们在 getMovementFlags 这个回调方法里面设置 需要监听那个动作 侧滑 拖拽 然后在 onMove 方法里面处理刷新数据的操作

        /**
         * @param recyclerView
         * @return
         * 拖拽的时候回调这个方法
         *
         */
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder srcholder,
                              @NonNull RecyclerView.ViewHolder targethodler) {
            /**
             * 刷新数据
             * 刷新界面(刷新adapter)
             *
             */
       itemTouchAdapterCallback.onitemmove(srcholder.getAdapterPosition(),targethodler.getAdapterPosition());
            return false;
        }
    

    然后是我们的适配器处理

    适配器

    package com.example.accountrecord.recycler;
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    import com.example.accountrecord.DeleteaccountDialog;
    import com.example.accountrecord.Deleteaccountlistener;
    import com.example.accountrecord.R;
    import com.example.accountrecord.RemoveUserinfoListner;
    import com.example.accountrecord.ResourceUtil;
    import com.example.accountrecord.SelectPhone;
    import com.example.accountrecord.SharedPreferencesUtils;
    import java.util.Collections;
    import java.util.List;
    
    
    public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHodler> implements ItemTouchAdapterCallback{
       private List<SelectPhone> data;
       private StartdragCallback satrtdragCallback;
       private Context context;
       private RemoveUserinfoListner removeUserinfoListner;
       private OnItemClickCallback onItemClickListener;
    
    
        public Myadapter(List<SelectPhone> data, Context context, RemoveUserinfoListner removeUserinfoListner,
                         OnItemClickCallback onItemClickListener,StartdragCallback satrtdragCallback ) {
            this.data = data;
            this.satrtdragCallback = satrtdragCallback;
            this.context=context;
            this.removeUserinfoListner=removeUserinfoListner;
            this.onItemClickListener=onItemClickListener;
        }
    
    
    
        @NonNull
        @Override
        public Myadapter.MyViewHodler onCreateViewHolder(@NonNull ViewGroup parent, int i) {
           View  view=LayoutInflater.from(parent.getContext()).inflate(R.layout.selectphone_item,parent,false);
            return new MyViewHodler(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull final Myadapter.MyViewHodler myViewHodler, @SuppressLint("RecyclerView") final int position) {
            final SelectPhone selectPhone = (SelectPhone) data.get(position);
            myViewHodler.phonetextview.setText(selectPhone.getName());
              myViewHodler.account_rl.setOnTouchListener(new View.OnTouchListener() {
                 @Override
                 public boolean onTouch(View v, MotionEvent event) {
                    satrtdragCallback.startdrag(myViewHodler);
                     return false;
                 }
             });
    
            myViewHodler.deleterl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // data.remove(position);
                    DeleteaccountDialog deleteaccountDialog = new DeleteaccountDialog(context, selectPhone.getName(), new Deleteaccountlistener() {
                        @Override
                        public void deleteaccountsuccess() {
                            notifyDataSetChanged();
                            removeUserinfoListner.removeuserinfosuccess(position,
                                    (List<SelectPhone>)data);
                        }
                    });
                    deleteaccountDialog.show();
    
                }
            });
    
    
            myViewHodler.account_rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    onItemClickListener.onClick(  myViewHodler.account_rl, position);
                }
            });
    
        }
        @Override
        public int getItemCount() {
            return data.size();
        }
    
        public class  MyViewHodler extends RecyclerView.ViewHolder{
            TextView phonetextview;
            ImageView imageview;
            RelativeLayout deleterl;
            RelativeLayout account_rl;
    
            public MyViewHodler(@NonNull View itemView) {
                super(itemView);
                phonetextview = itemView.findViewById(ResourceUtil.getId(context, "account_textview"));//account_textview   //account_image
                imageview = itemView.findViewById(ResourceUtil.getId(context, "account_image"));
                deleterl = itemView.findViewById(ResourceUtil.getId(context, "delete_account_rl"));
                account_rl=itemView.findViewById(ResourceUtil.getId(context,"account_rl"));
            }
        }
    
        @Override
        public boolean onitemmove(int fromposition, int toposition) {
              Collections.swap(data ,fromposition,toposition);
              SharedPreferencesUtils.putSelectBean(context,data,"qqmessage");
               notifyItemMoved(fromposition,toposition);
               return false;
        }
        @Override
        public void onitemSwiped(int position) {
            data.remove(position);
            notifyItemRemoved(position);
        }
    }
    

    我们的适配器实现了 ItemTouchAdapterCallback 这个接口然后onitemmove 方法和 onitemSwiped 方法,然后分别 处理侧滑监听和拖拽监听的

     @Override
        public boolean onitemmove(int fromposition, int toposition) {
                Collections.swap(data ,fromposition,toposition);
                SharedPreferencesUtils.putSelectBean(context,data,"qqmessage");
                notifyItemMoved(fromposition,toposition);
                return false;
          }
        @Override
        public void onitemSwiped(int position) {
            data.remove(position);
            notifyItemRemoved(position);
        }
    

    LoginActivity 实现

    package com.example.accountrecord;
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Context;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.PopupWindow;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.example.accountrecord.recycler.OnItemClickCallback;
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
        private static final String TAG = "LoginActivity";
        private EditText account_ed ,password_ed;
        private String  account, password;
        private  Boolean flag=false;
        private Button  loginbtn;
        List<SelectPhone> getdata=null;
        private Context mContext=LoginActivity.this;
        private TextView  textView;
        private RelativeLayout select_loginaccount_rl;
        private String  cacheaccount ,cachepsw;
        private SpinerPopWindow mSpinerPopWindow;
        List<SelectPhone> data=new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            initview();
    
        }
    
        private void initview() {
            account_ed=findViewById(R.id.login_account_ed);
            password_ed=findViewById(R.id.account_login_psw);
            loginbtn=findViewById(R.id.promptly_login);
            loginbtn.setOnClickListener(this);
            textView=findViewById(R.id.login_account_textview);
            select_loginaccount_rl=findViewById(R.id.select_loginaccount_rl);
            select_loginaccount_rl.setOnClickListener(this);
            getdata = SharedPreferencesUtils.getSelectBean(mContext, "selectphone");
            if (getdata != null && getdata.size() > 0) {
                SelectPhone phone = getdata.get(0);
                cacheaccount = phone.getName();
                cachepsw=phone.getPassword();
    
                if (!TextUtils.isEmpty(cacheaccount)) {
                    account_ed.setText(cacheaccount);
                    password_ed.setText(cachepsw);
                } else {
                    account_ed.setText(null);
                    password_ed.setText(null);
                }
            }
    
            mSpinerPopWindow = new SpinerPopWindow(LoginActivity.this,
                    getdata, onItemClickListener, removeUserinfoListner);
            mSpinerPopWindow.setOnDismissListener(dismissListener);
        }
    
        //删除用户缓存信息
        private RemoveUserinfoListner removeUserinfoListner = new RemoveUserinfoListner() {
            @Override
            public void removeuserinfosuccess(int position, List<SelectPhone> data) {
                if (data != null && data.size() > 0) {
                    data.remove(position);
                    SharedPreferencesUtils.putSelectBean(mContext, data, "selectphone");
                    flag = false;
                    List<SelectPhone> getdata = SharedPreferencesUtils.
                            getSelectBean(LoginActivity.this, "selectphone");
                    if (getdata != null && getdata.size() > 0) {
                        SelectPhone selectPhone = getdata.get(0);
                        account_ed.setText(selectPhone.getName());
                        password_ed.setText(selectPhone.getPassword());
                    } else {
                        account_ed.setText(null);
                        password_ed.setText(null);
    
                    }
                } else {
                    Toast.makeText(mContext,"缓存数据为空",Toast.LENGTH_LONG).show();
                }
            }
    
        };
    
        /**
         * 监听popupwindow取消
         */
        private PopupWindow.OnDismissListener dismissListener = new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                flag = false;
    
            }
        };
    
        /**
         * popupwindow显示的ListView的item点击事件
         */
    
        private OnItemClickCallback onItemClickListener=new OnItemClickCallback() {
            @Override
            public void onClick(View parent, int position) {
                mSpinerPopWindow.dismiss();
                flag = false;
                SelectPhone selectPhone = getdata.get(position);
                String getusername = selectPhone.getName();
                String psw = selectPhone.getPassword();
                account_ed.setText(getusername);
                password_ed.setText(psw);
            }
        };
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.promptly_login:
                    login();
                    break;
                case R.id.select_loginaccount_rl:
                    if (flag) {
                        flag = false;
                        if (mSpinerPopWindow != null) {
                            mSpinerPopWindow.dismiss();
                        } else {
    
                        }
                    } else {
                        flag = true;
                        if (mSpinerPopWindow != null) {
                            mSpinerPopWindow.showAsDropDown(textView);
                        } else {
                        }
    
                    }
                    break;
              default:
                  break;
            }
        }
    
        private  void  login(){
            account=account_ed.getText().toString();
            password=password_ed.getText().toString();
            if(TextUtils.isEmpty(account)||TextUtils.isEmpty(password)){
                Toast.makeText(mContext,"账号或者密码输入不能为空",Toast.LENGTH_LONG).show();
            }
            SelectPhone selectPhone=new SelectPhone();
            selectPhone.setName(account);
            selectPhone.setPassword(password);
            List<SelectPhone> getdata = SharedPreferencesUtils.getSelectBean(LoginActivity.this, "selectphone");
            if (getdata != null && getdata.size() > 0) {
                getdata.add(0,selectPhone);
                SharedPreferencesUtils.putSelectBean(LoginActivity.this, getdata, "selectphone");
            } else {
                data.add(selectPhone);
                SharedPreferencesUtils.putSelectBean(LoginActivity.this, data, "selectphone");
            }
            Toast.makeText(mContext,"登录成功数据缓存成功",Toast.LENGTH_LONG).show();
            finish();
    
        }
    }
    

    我们在 LoginActivity 界面的逻辑就比较简单了 我们只需要缓存数据以及在点击登录button的时候 缓存数据到本地即可.

    最后总结:

    这个demo 主要是对recyclerview 里面的 ItemTouchHelper.Callback 类里面拖拽方法和侧滑时间方法的处理 ,然后配合recyclerview 来实现这样的一个本地数据缓存的列表的效果 。 最后希望我都文章能帮助各位同学工作和学习 。如果觉得文章还不错希望能给我一个star 和转发

    项目地址:

    码云 https://gitee.com/qiuyu123/item-touch-helperdemo

    相关文章

      网友评论

          本文标题:Android ItemTouchHelper 实现账号历史记

          本文链接:https://www.haomeiwen.com/subject/ozywfdtx.html