I've simple card.xml
layout in which one image is presented, it's size is 300px x 300px.
For displaying cards I've made Recycler View and RecyclerAdapter, and initialized all stuff to works in form of Grid Layout.
My problem is: views are displayed but vertical distance between two rows of cards is to much, I want to get it closer.
Here's how it looks like:
For next row I must scroll:
Is there any to fix it?
card layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground"
android:clickable="true"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/java"></ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
Adapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card,parent,false);
return (ViewHolder) new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 4;
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView){
super(itemView);}
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(this,2));
RecyclerAdapter adapter = new RecyclerAdapter();
recyclerView.setAdapter(adapter);
}
}
PS: this's all laboratory project, so things like dynamically initialized cards content have been skipped.
It seems to me that your problem is in the layout of the CardView
.
Try to replace android:layout_height="match_parent"
in android:layout_height="wrap_content"
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground"
android:clickable="true"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/java"></ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>