Android知识点记录-滚动控件(RecylerView)

一、案例介绍

1、模拟聊天列表并实现删除聊天单条记录功能。

@1:关键技术:

        RecyclerView的应用、

        自定义SlidingButtonView类、

        RecyclerView.ViewHolder中的getLayoutPosition()方法、

        RecyclerView.Adapter类中的notifyItemRemoved()方法、

        SlidingButtonView类中的changeScrollX()方法。

@2:步骤代码:

        #1:在主活动布局界面上添加RecyclerView控件,并调整相应参数属性。

 <!--  列表控件  -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:overScrollMode="never"/>

        #2:在主活动类文件中,初始化RecyclerView控件,并为其设置适配器。

public class SlideDelteActivity extends AppCompatActivity {
    private RecyclerView lRecyclerView;    //列表控件
    private MyAdapter lMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_delte);
        initView();
        setAdapter();
    }
    //初始化控件
    private void initView(){
        lRecyclerView = findViewById(R.id.rv);

    }
    //设置适配器方法
    private void setAdapter(){
       
    }
}

       #3:自定义子项内容布局。主要有图标、名称两个属性。

<?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="80dp"
    android:layout_marginBottom="1dp"
    android:background="@color/white">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/rv_layout_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">
            <ImageView
                android:id="@+id/rv_item_img"
                android:background="@mipmap/ic_launcher"
                android:layout_width="50dp"
                android:layout_height="50dp"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_toRightOf="@id/rv_item_img"
                android:orientation="vertical"
                android:gravity="center_vertical">
                <TextView
                    android:id="@+id/rv_item_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:textColor="@color/black"
                    android:textSize="15sp"
                    android:text="赵..."/>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

        #4:自定义工具类。获取屏幕宽度。

public class Utils {
    //获取屏幕宽度
    public static int getScreenWidth(Context context){
        //获取窗口管理服务
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //屏幕参数对象
        DisplayMetrics outMetric = new DisplayMetrics();
        //获取屏幕参数
        wm.getDefaultDisplay().getMetrics(outMetric);
        //返回屏幕宽度
        return outMetric.widthPixels;
    }
}

        #5:自定义适配器MyAdapter类。在其中实现图标、名称的适配。


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    //图标数组
    private int[] icon = {
            R.drawable.wallet,R.drawable.wallet,
            R.drawable.wallet,R.drawable.wallet,
            R.drawable.wallet,R.drawable.wallet,
            R.drawable.wallet,R.drawable.wallet,
            R.drawable.wallet,R.drawable.wallet,
            R.drawable.wallet,R.drawable.wallet,
    };
    //名字数组
    private int[] name = {
            R.string.delete_text_name,R.string.delete_text_name,
            R.string.delete_text_name,R.string.delete_text_name,
            R.string.delete_text_name,R.string.delete_text_name,
            R.string.delete_text_name,R.string.delete_text_name,
            R.string.delete_text_name,R.string.delete_text_name,
            R.string.delete_text_name,R.string.delete_text_name,
    };
    private Context lContext;    //定义上下文
    //图标集合
    private List<Integer> listIcon = new ArrayList<Integer>();
    //名字集合
    private List<Integer> listName = new ArrayList<Integer>();

    public MyAdapter(Context context){
        lContext = context;
        //设置菜单行数与行内图标、名称
        for (int i=0; i<12;i++){
            listIcon.add(icon[i]);
            listName.add(name[i]);
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView name;  //名字
        public ImageView img;  //图标
        public ViewGroup layout_content;   //图标与信息布局
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.rv_item_name);
            img = itemView.findViewById(R.id.rv_item_img);
            layout_content = itemView.findViewById(R.id.rv_layout_content);
        }
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取列表中每行的布局文件
        View view  = LayoutInflater.from(lContext).inflate(R.layout.rv_item_layout,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    //设置列表菜单中行内所显示的内容
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.img.setBackgroundResource(listIcon.get(position));   //设置图标
        holder.name.setText(listName.get(position));  //设置名称
        //设置内容布局的宽度为屏幕的宽度
        holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(lContext);
    }

    //返回数据集合中行的总数
    @Override
    public int getItemCount() {
        return listIcon.size();
    }
}

        #6:在主活动界面中的设置适配器方法中实现设置适配器。

//设置适配器方法
    private void setAdapter(){
        lRecyclerView.setLayoutManager(new LinearLayoutManager(this));   //设置列表布局管理器
        lRecyclerView.setAdapter(lMyAdapter = new MyAdapter(this));   //设置适配器
        lRecyclerView.setItemAnimator(new DefaultItemAnimator());  //设置列表中子项的动画
    }

到这里,滚动列表功能基本实现。

        #7:在子项内容布局文件中添加删除按钮代码。

//在第一层RelativeLayout布局控件最上层添加。
 <TextView
            android:id="@+id/tv_delete"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/rv_layout_content"
            android:background="@drawable/btn_click_red_bg"
            android:gravity="center"
            android:text="删除"
            android:drawableTop="@drawable/delete"
            android:drawablePadding="-10dp"
            android:textColor="@color/white"/>
        

        #8:在自定义适配器文件中,实现删除按钮的初始化、删除按钮的单击事件、删除列表中信息。

//设置列表菜单中行内所显示的内容
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.img.setBackgroundResource(listIcon.get(position));   //设置图标
        holder.name.setText(listName.get(position));  //设置名称
        //设置内容布局的宽度为屏幕的宽度
        holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(lContext);
        //设置按钮单击事件
        holder.btn_rv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int n = holder.getLayoutPosition();  //获取要删除行的位置
                removeData(n);                       //删除列表中指定行
            }
        });
    }
//删除列表行中信息的方法
    public void removeData(int position){
        listIcon.remove(position);    //删除列表行内图标
        listName.remove(position);    //删除行中名字
        notifyItemRemoved(position);  //删除行
    }

           #9:自定义滑动删除按钮的view类文件。

public class SlidingButtonView extends HorizontalScrollView {
    private TextView lTextView_Delete;  //删除按钮
    private int lScrollWidth;           //横向滚动的范围
    private Boolean first = false;      //标记第一次进入获取删除按钮控件
    public SlidingButtonView(Context context) {
        super(context,null);
    }

    public SlidingButtonView(Context context, AttributeSet attrs) {
        super(context, attrs,0);
    }

    public SlidingButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //设置滚动模式
        this.setOverScrollMode(OVER_SCROLL_NEVER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //第一次进入时获取删除按钮控件
        lTextView_Delete = findViewById(R.id.tv_delete);
        first = true;    //修改标记
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //默认隐藏删除按钮
        if (changed){
            this.scrollTo(0,0);
            //获取水平滚动条可以滚动的范围,即右侧删除按钮的宽度
            lScrollWidth = lTextView_Delete.getWidth();
        }
    }

    //手势判断
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                changeScrollX();
        }
        return super.onTouchEvent(ev);
    }

    //根据滑动距离判断是否显示删除按钮
    private void changeScrollX() {
        //触摸滑动的距离大于删除按钮宽度的一半
        if (getScaleX() >= (lScrollWidth/2)){
            //显示删除按钮
            this.smoothScrollTo(lScrollWidth,0);
        }else {
            //隐藏删除按钮
            this.smoothScrollTo(0,0);
        }
    }
}

        #10:将子项内容布局文件的最外层布局由LinearLayout更换为自定义的SlidingButtonView。

<?xml version="1.0" encoding="utf-8"?>
<view.SlidingButtonView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_marginBottom="1dp"
    android:background="@color/white">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        ...
        ...
        ...
    </RelativeLayout>

</view.SlidingButtonView>

到这里,实现删除聊天列表信息记录的功能。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值