相关视频教程在某站上面(🔍浩宇软件开发)
1. 涉及到的技术点
- 列表控件RecyclerView的使用
- Adapter的使用
- RecyclerView的点击监听事件
2. 代码实现过程
- 编写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>
- 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();
}
}
}