the android code runs in andrpid studio is like this MainActivity.jav mainactivity.java import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private List itemList; private BoxAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); itemList = new ArrayList<>(); for (int i = 0; i < 36; i++) { itemList.add(new Object()); } adapter = new BoxAdapter(itemList); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new GridLayoutManager(this, 4)); } } ...................................... BoxAdapter.java import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class BoxAdapter extends RecyclerView.Adapter { private List itemList; public BoxAdapter(List itemList) { this.itemList = itemList; } @NonNull @Override public BoxViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_box, parent, false); return new BoxViewHolder(view); } @Override public void onBindViewHolder(@NonNull BoxViewHolder holder, int position) { // Do nothing } @Override public int getItemCount() { return itemList.size(); } static class BoxViewHolder extends RecyclerView.ViewHolder { public BoxViewHolder(@NonNull View itemView) { super(itemView); } } } ................................ activity_main.xml ......................................... and it runs like picture 1 how can i change the code ,to make it shows 80*80 square like this
the android code runs in andrpid studio is like this
MainActivity.jav
mainactivity.java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Object> itemList;
private BoxAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
itemList = new ArrayList<>();
for (int i = 0; i < 36; i++) {
itemList.add(new Object());
}
adapter = new BoxAdapter(itemList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
}
}
......................................
BoxAdapter.java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class BoxAdapter extends RecyclerView.Adapter<BoxAdapter.BoxViewHolder> {
private List<Object> itemList;
public BoxAdapter(List<Object> itemList) {
this.itemList = itemList;
}
@NonNull
@Override
public BoxViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_box, parent, false);
return new BoxViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull BoxViewHolder holder, int position) {
// Do nothing
}
@Override
public int getItemCount() {
return itemList.size();
}
static class BoxViewHolder extends RecyclerView.ViewHolder {
public BoxViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
................................
activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="4" />
</androidx.constraintlayout.widget.ConstraintLayout>
.........................................
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/purple" />
and it runs like picture 1
how can i change the code ,to make it shows 80*80 square like this
-
Step by step
Solved in 5 steps