基于Android平台开发,备忘录记事本,列表实现(四)

相关视频教程在某站上面(🔍浩宇软件开发)

1. 涉及到的技术点

  1. 列表控件RecyclerView的使用
  2. Adapter的使用
  3. RecyclerView的点击监听事件

2. 代码实现过程

  1. 编写fragment_home.xml布局
<?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"
    android:orientation="vertical"
    tools:context=".fragment.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="备忘录记事本" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:listitem="@layout/note_list_item" />


        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/ll_empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/img_empty" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="空空如也~~" />
        </androidx.appcompat.widget.LinearLayoutCompat>


    </RelativeLayout>


    <androidx.cardview.widget.CardView
        android:id="@+id/btn_create_note"
        android:layout_width="58dp"
        android:layout_height="58dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="60dp"
        android:backgroundTint="@color/my_light_primary"
        app:cardCornerRadius="29dp">


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/baseline_border_color_24" />


    </androidx.cardview.widget.CardView>

</RelativeLayout>
  1. HomeFragment.java页面
public class HomeFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private LinearLayoutCompat ll_empty;

    private NoteListAdapter mListAdapter;

    private String[] item1 = new String[]{"置顶", "编辑", "删除"};
    private String[] item2 = new String[]{"取消置顶", "编辑", "删除"};

    private int currentIndex = 0;


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        recyclerView = rootView.findViewById(R.id.recyclerView);
        ll_empty = rootView.findViewById(R.id.ll_empty);

        //初始化适配器
        mListAdapter = new NoteListAdapter();

        //设置适配器
        recyclerView.setAdapter(mListAdapter);

        //recyclerView点击事件
        mListAdapter.setOnItemClickListener(new NoteListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, NoteInfo noteInfo) {
                Intent intent = new Intent(getActivity(), NoteDetailsActivity.class);
                intent.putExtra("noteInfo", noteInfo);
                startActivity(intent);
            }

            @Override
            public void onMore(int position, NoteInfo noteInfo) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("操作")
                        .setNegativeButton("取消", null)
                        .setSingleChoiceItems(noteInfo.getNote_is_top() == 0 ? item1 : item2, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                currentIndex = which;
                            }
                        })
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (currentIndex == 0) {
                                    //三元运算符
                                    NoteDbHelper.getInstance(getActivity()).updateNoteTop(noteInfo.getNote_id(), noteInfo.getNote_is_top() == 0 ? 1 : 0);
                                    //刷新
                                    initData();

                                } else if (currentIndex == 1) {
                                    Intent intent = new Intent(getActivity(), CreateNoteActivity.class);
                                    intent.putExtra("noteInfo", noteInfo);
                                    startActivityForResult(intent, 1000);


                                } else {
                                    int row = NoteDbHelper.getInstance(getActivity()).deleteNote(noteInfo.getNote_id());
                                    if (row > 0) {
                                        Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                                        //刷新
                                        initData();
                                    } else {
                                        Toast.makeText(getActivity(), "删除失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        })
                        .show();

                currentIndex = 0;
            }

        });

    }

    @Override
    protected void setListener() {
        rootView.findViewById(R.id.btn_create_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //跳转到创建笔记界面
                // startActivity(new Intent(getActivity(), CreateNoteActivity.class));
                Intent intent = new Intent(getActivity(), CreateNoteActivity.class);
                startActivityForResult(intent, 1000);
            }
        });
    }

    @Override
    protected void initData() {
        UserInfo userInfo = UserInfo.getUserInfo();
        List<NoteInfo> noteInfos = NoteDbHelper.getInstance(getActivity()).queryNoteListData(userInfo.getUsername());
        if (mListAdapter != null) {
            mListAdapter.setNoteInfoList(noteInfos);
        }
        if (noteInfos.size() == 0) {
            ll_empty.setVisibility(View.VISIBLE);
        } else {
            ll_empty.setVisibility(View.GONE);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 1000) {
            initData();
        }
    }
}

3. 运行效果图

请添加图片描述

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值