简体   繁体   中英

Android CardView padding error

---TLDR---

I'm following a tutorial where with the help of cardview and recyclerview i should be able to list text beneath each other like this : how it should kind of look like(the image isn't needed)

how it actually looks

---PROBLEM---

As the title states and as it can be seen on the picture, it seems like theres a huge padding between 2 cardview item, which obviously isn't something i would like to have. I want them to be close to eachother, as it can be seen on the other picture.

Please keep in mind that im an ultra-beginner programmer :D. Thanks for all answers in advance.

---CODE---

cards_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="cards main container">

        <android.support.v7.widget.CardView
            android:layout_width="300dp"
            android:layout_height="60dp"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            card_view:cardElevation="5dp"
            card_view:cardUseCompatPadding="true"
            card_view:cardBackgroundColor="#FFF"
            android:id="@+id/card_view"
            android:layout_gravity="center_horizontal">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="10dp"
                        android:text="Marketing Tip"
                        android:id="@+id/textviewName"
                        android:textColor="#000"/>

                </LinearLayout>

        </android.support.v7.widget.CardView>

</LinearLayout>

HomeActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private static RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private static RecyclerView recyclerView;
    private static ArrayList<DataModel> data;
    static View.OnClickListener myOnClickListener;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        setTitle("Home");


        myOnClickListener = new myOnClickListener(this);


        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        data = new ArrayList<DataModel>();
        for (int i = 0; i < MyData.nameArray.length; i++) {
            data.add(new DataModel(
                    MyData.nameArray[i],
                    MyData.id_[i]
            ));

            adapter = new CustomAdapter(data);
            recyclerView.setAdapter(adapter);

        }

    }

        private static class myOnClickListener implements View.OnClickListener {

            private final Context context;

            private myOnClickListener(Context context) {
                this.context = context;
            }

            @Override
            public void onClick(View v) {
                //TODO WRITE THE CODE TO START AN ACTIVITY ONCE THE CARDVIEW IS CLICKED
            }
        }

}

CustomAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{

    private ArrayList<DataModel> dataSet;

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView textviewName;


        public MyViewHolder(View itemView) {
            super(itemView);
            this.textviewName = (TextView)itemView.findViewById(R.id.textviewName);

        }
    }

    public CustomAdapter(ArrayList<DataModel> data){
        this.dataSet = data;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards_layout, parent, false);
        view.setOnClickListener(HomeActivity.myOnClickListener);

        MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

        TextView textviewName = holder.textviewName;

        textviewName.setText(dataSet.get(listPosition).getName());


    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }
}

DataModel.java

public class DataModel {
    String name;
    int id_;

    public DataModel(String name, int id_) {

        this.name = name;
        this.id_ = id_;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id_;
    }
}

MyData.java

public class MyData {

    static String[] nameArray = {"Buzi", "Kocsog", "Fostalicska", "CiganyTibi"};

    static Integer[] id_ = {0, 1, 2, 3};
}

Heyho,

Im also a beginner, but i guess its due to your height of linearlayout in the cards_layout.xml.

Try to use wrap_content instead of match_parent. Otherwise, u will inflate a card, matched size of your recyclerView.

Hopefully this helps you.

Thomas

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/card_task"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:contentPadding="16dp"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp"
    layout_height="wrap_content"
    layout_width="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/desc"
            android:padding="5dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Description" />

    </LinearLayout>
</android.support.v7.widget.CardView>

The above layout file worked for me. Hope it helps

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="cards main container">

        <android.support.v7.widget.CardView
            android:layout_width="300dp"
            android:layout_height="60dp"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            card_view:cardElevation="5dp"
            card_view:cardUseCompatPadding="true"
            card_view:cardBackgroundColor="#FFF"
            android:id="@+id/card_view"
            android:layout_gravity="center_horizontal">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="10dp"
                        android:text="Marketing Tip"
                        android:id="@+id/textviewName"
                        android:textColor="#000"/>

                </LinearLayout>

        </android.support.v7.widget.CardView>

</LinearLayout>

Use this as your cardview layout. You have basically used match_parent for the Linear Layout causing the view to fill up the entire screen. Changing it to wrap_content should do the job. Rest is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM