今天是冲刺的第二天吧,主要做了笔记展示界面的简要设计,效率比较低。
遇到的困难:采用的是recyclerview+卡片式布局,自己光想着展示笔记的详细信息,而没有考虑到安卓屏幕的限制,导致一篇笔记占据屏幕空间过大,而且界面也不好看。
解决思路:只展示笔记的简要信息和标题,具体内容放在另一个界面,通过点击标题进行笔记的查看。
明天计划完成的任务:页面实现下拉刷新
附上编写的代码:
分享大厅主界面(采用recyclerview):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="作者" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学科" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="时间" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年级" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="搜索" android:textSize="16sp"> </Button> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal|center_vertical" > </androidx.recyclerview.widget.RecyclerView> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </LinearLayout>
卡片式布局(recyclerview中每个item对应的布局):
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:id="@+id/cv_item" card_view:cardCornerRadius="8dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="10dp" card_view:srcCompat="@drawable/user_logo" > </ImageView> <TextView android:id="@+id/writer_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="嘻哈二怪" android:layout_marginLeft="10dp" android:layout_marginTop="23dp" android:textSize="16sp" android:layout_toRightOf="@+id/icon" > </TextView> <ImageView android:id="@+id/collect" android:layout_width="30dp" android:layout_height="30dp" android:layout_margin="5dp" card_view:srcCompat="@drawable/shoucang" android:layout_alignParentRight="true" > </ImageView> <TextView android:id="@+id/course" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="高数" android:textSize="17sp" android:layout_marginTop="23dp" android:layout_marginRight="40dp" android:layout_toLeftOf="@+id/collect" android:layout_alignBottom="@+id/writer_name" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="大一上高数第一次整理笔记" android:textSize="18sp" android:layout_below="@+id/icon" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2020-04-17" android:textSize="16sp" android:layout_marginTop="20dp" android:layout_alignRight="@+id/collect" android:layout_marginRight="15dp" android:layout_marginBottom="10dp" android:layout_alignParentRight="true" android:layout_below="@+id/icon" /> </RelativeLayout> </androidx.cardview.widget.CardView>
适配器
package com.itheima.cloudnotes.adapter; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.itheima.cloudnotes.Activity.Detail; import com.itheima.cloudnotes.R; import com.itheima.cloudnotes.enity.Note; import java.util.List; public class NoteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<Note> mNoteList; private Context mContext; @NonNull @Override //onCreateViewHolder负责承载每个子项的布局。它有两个参数,其中一个是 int viewType; public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { mContext=parent.getContext(); View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false); return new RecyclerViewHolder(view); } //onBindViewHolder负责将每个子项holder绑定数据,俩参数分别是RecyclerView.ViewHolder holder, int position; @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder; final Note note=mNoteList.get(position); recyclerViewHolder.icon.setImageResource(note.getIcon()); recyclerViewHolder.tv_writer_name.setText(note.getWriter_name()); recyclerViewHolder.tv_course.setText(note.getCourse()); recyclerViewHolder.collect.setImageResource(note.getCollect()); recyclerViewHolder.tv_title.setText(note.getTitle()); recyclerViewHolder.tv_date.setText(note.getDate()); recyclerViewHolder.tv_title.setOnClickListener(new View.OnClickListener( ) { @Override public void onClick(View v) { Intent intent=new Intent(mContext,Detail.class); intent.putExtra("writer_name",note.getWriter_name()); mContext.startActivity(intent); } }); recyclerViewHolder.collect.setOnClickListener(new View.OnClickListener( ) { @Override public void onClick(View v) { } }); } @Override public int getItemCount() { return mNoteList.size(); } public NoteAdapter(List<Note> mNoteList) { this.mNoteList = mNoteList; } private class RecyclerViewHolder extends RecyclerView.ViewHolder{ View NoteView; ImageView icon; TextView tv_writer_name; TextView tv_course; ImageView collect; TextView tv_title; TextView tv_date; public RecyclerViewHolder(@NonNull View itemView) { super(itemView); NoteView=itemView; icon=itemView.findViewById(R.id.icon); tv_writer_name=itemView.findViewById(R.id.writer_name); tv_course=itemView.findViewById(R.id.course); collect=itemView.findViewById(R.id.collect); tv_title=itemView.findViewById(R.id.title); tv_date=itemView.findViewById(R.id.date); } } }
详细信息页面(很粗糙)
活动加载布局
package com.itheima.cloudnotes; import android.annotation.SuppressLint; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.itheima.cloudnotes.listener.EndlessRecyclerOnScrollListener; import com.itheima.cloudnotes.adapter.NoteAdapter; import com.itheima.cloudnotes.enity.Note; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private List<Note> noteList=new ArrayList<>(); private SwipeRefreshLayout srl; private RecyclerView recyclerView; private NoteAdapter noteAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init() { initNotes(); recyclerView=findViewById(R.id.recyclerView); LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); noteAdapter=new NoteAdapter(noteList); recyclerView.setAdapter(noteAdapter); } public void initNotes() { for(int i=0;i<5;i++) { Note note=new Note(R.drawable.user_logo,"胖乎乎瘦高高","高数",R.drawable.shoucang,"常用定积分公式","2020-04-18"); noteList.add(note); } }