CardView Android [closed]

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 is not exact, I can not only do in XML .

Does anyone have a clue which way to go right on java ? When card is clicked it will open another activity .

    
asked by anonymous 03.01.2016 / 16:03

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 place:

        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>

Do not forget the compiles.

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

If it was not quite clear to you, I recommend this video lesson recyclerView

    
04.01.2016 / 04:45