Cardview Android

Asked

Viewed 1,525 times

0

I am participating in the creation of a project, and I need to create a list containing several CardViews, as the number of cards it’s not exact, I can’t just do XML.

Does anyone have a hint as to which way to go java? When the card is clicked it will open another activity.

  • 2

    Welcome to Sopt. What have you tried? Take advantage and read [Ask] and [tour] to know how the site works.

  • 1

    Welcome to Stack Overflow, I suggest you read: http://answall.com/help/dont-ask and http://answall.com/help/mcve for a better chance of getting your question answered. About the question itself, could you put the part of the referred code so we can help? The contents of these cards are being taken from where?

  • See the links: http://goo.gl/JzYAFf and http://goo.gl/M0iZI1. both show how to create elements of listview dynamically.

  • Try using a for, then you generate the cardviews and generate an id with its respective number...

1 answer

3

Create an Activity that has a recyclerView:

<LinearLayout
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
   <android.support.v7.widget.RecyclerView
      android:id="@+id/myRecycler"
      android:scrollbars="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
       />
</LinearLayout>

In the onCreate of your Activity put:

        RecyclerView myRecycler = findViewById(R.id.myRecycler);
        rvDisciplina.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        SeuAdapter seuAdapter = new SeuAdapter(this,mList);
        myRecycler.setAdapter(adapterInfo);

Create an Adapter to control views:

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

private List<Aluno> mList;
private LayoutInflater mLayoutInflater;
private Context mContext;


public SeuAdapter(Context c, List<Aluno> l){
    mContext = c;
    mList = l;
    mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = mLayoutInflater.inflate(R.layout.item_layout, viewGroup, false);
    MyViewHolder mvh =  new MyViewHolder(view);
    return mvh;
}

@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {
    myViewHolder.txtNome.setText(mList.get(position).getNome);
    myViewHolder.txtSobrenome.setText(mList.get(position).getSobrenome);
    myViewHolder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mContext.startActivity(mContext,seuClasse.class);
        }
    });
}

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

public void addlisItem(Aluno a,int position){
    mList.add(a);
    notifyItemInserted(position);

}
public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView txtNome;
    public TextView txtSobrenome;
    public View view;
    public MyViewHolder(View itemView) {
        super(itemView);
        txtNome = (TextView)itemView.findViewById(R.id.txtNome);
        txtSobrenome = (TextView)itemView.findViewById(R.id.txtSobrenome);
        view = itemView;
    }
}

}

Create an xml for your items. Example:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/margin_left_right"
    android:layout_marginRight="@dimen/margin_left_right"
    android:layout_marginTop="@dimen/margin_top"
    android:layout_marginBottom="@dimen/margin_bottom"

    app:cardElevation="4dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="false"
    app:cardMaxElevation="4dp"
    app:cardCornerRadius="3dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtNomeT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:textColor="@color/colorSecond"
            android:singleLine="true"
            android:layout_marginLeft="16dp"
            android:text="Nome"
            android:layout_gravity="left|center_vertical" />
        <TextView
            android:id="@+id/txtSobrenome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:singleLine="true"
            android:layout_marginLeft="16dp"
            android:text="Nome"
            android:layout_gravity="left|center_vertical" />
    </LinearLayout>
</android.support.v7.widget.CardView>

Don’t forget the compiles.

compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'

If it wasn’t clear to you, I recommend this video lesson recyclerView

  • Good morning friend, because there is this method addlisItem If the Adapter already passes that list mList?

  • If you want to add more item to list. For example: You already have items 1 and 2 in this list and want to add item 3.

  • I managed to change, but not passing the model by the constructor, because in my bag my data comes from an Api, so I added a model List, and then addAll but using the synchronized

  • When you have the data just call Seuadapter youAdapter = new Seuadapter(this,mList); myRecycler.setAdapter(adapterInfo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.