“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

A few days ago we have been copying according to the project has been online, I believe you have accumulated a lot of experience, but also encountered a lot of bugs, believe yourself, believe Baidu, learning is the process of crawling and rolling, especially a field you are not familiar with, come on!! Today we are still copying a project that is already online in the Google Store, and continue to improve our practical experience

Let’s see how it works

We can obviously see that this is a RecyclerView, and below that is a check-in button, so let’s start coding

code

Same old XML

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout 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 :id="@+id/layout" android:layout_width="match_parent" Android :layout_height="match_parent" Android :layout_marginTop="10dp" android:layout_marginTop="10dp" android:layout_marginTop="10dp Android :layout_marginRight="10dp" Tools :context=".TaskCenterActivity"> <TextView // Android :layout_width="wrap_content" Android :layout_height="wrap_content" android:layout_height="wrap_content" android:layout_height="wrap_content" Android :layout_margin="15dp" // Default text Android :text=" daily check in "// font color Android :textColor="@color/black" // Font style, bold Android :textStyle="bold" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> / / below the sign in days < androidx. Recyclerview. Widget. Recyclerview / / resource id android: id = "@ + id/rv / / width is full Android :layout_width="match_parent" // Layout_height ="wrap_content" // distance above 15dp Android :layout_marginTop="15dp" app:layout_constraintTop_toBottomOf="@id/tv_title" /> // Check in below <TextView Android :layout_width="match_parent" android:layout_width="match_parent" android:layout_width="match_parent Android :layout_height="wrap_content" // Distance left and right 15dp Android :layout_margin="15dp" // background color Android :background="@color/teal_200" // Gravity ="center" android:background="@color/teal_200" // Text center android:gravity="center" // Android :text=" check in "// Font style Android :textStyle="bold" // Constraints app:layout_constraintTop_toBottomOf="@id/rv" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

Activity

private ConstraintLayout mLayout; private TextView mTvTitle; private RecyclerView mRv; private TextView mTvCheck; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_task_center); initView(); } private void initView() {// Initialize controller mLayout = findViewById(r.id.layout); mTvTitle = findViewById(R.id.tv_title); mRv = findViewById(R.id.rv); mTvCheck = findViewById(R.id.tv_check); Mtvcheck. setBackground(MyShape. SetMyShape (this,50, ContextCompat.getColor(this,R.color.teal_200))); / / set the layout manager for recyclerview, linear layout managers, lateral, not reverse mRv. SetLayoutManager (new LinearLayoutManager (this, recyclerview. HORIZONTAL, false));  TaskAdapter TaskAdapter = new TaskAdapter(this); // Set the mrv. setAdapter(taskAdapter); }Copy the code

Adapter

// Adapter must inherit recyclerView. Adapter public class TaskAdapter extends recyclerView. Adapter< taskAdapter. ViewHolder> {private final Context context; Public TaskAdapter(Context Context) {this.context = Context; } @NonNull @NotNull @Override public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, {// Load layout View root = layoutinflater.from (context).inflate(r.layout.item_task, parent, false); // Pass to ViewHolder return new ViewHolder(root); } @override public void onBindViewHolder(@nonnull @notnull TaskAdapter.ViewHolder holder, Int position) {if (position > 0) {// The data is the current position+1 holder.tv_day.settext ("Day" + (position+1)); } // Set the first small module to a rounded corner + background, the original Angle number is 4, Beijing arbitrary holder.item_layout1.setbackground (MyShape. SetMyShape (context,4,)) ContextCompat.getColor(context,R.color.teal_700))); } @override public int getItemCount() {return getItemCount(); } public class ViewHolder extends RecyclerView.ViewHolder { private final TextView tv_day; private final ConstraintLayout item_layout1; public ViewHolder(@NonNull @NotNull View itemView) { super(itemView); tv_day = itemView.findViewById(R.id.item_tv_day); item_layout1 = itemView.findViewById(R.id.item_layout1); }}}Copy the code

So let’s see what happens

Wow ~ golden legend!!