Tuesday 22 September 2015

Cardview example

XML

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent"    android:padding="16dp"    >

    <android.support.v7.widget.CardView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/cv"        >

        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:padding="16dp"            >

            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/person_photo"                android:layout_alignParentLeft="true"                android:layout_alignParentTop="true"                android:layout_marginRight="16dp"                />

            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/person_name"                android:layout_toRightOf="@+id/person_photo"                android:layout_alignParentTop="true"                android:textSize="30sp"                />

            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:id="@+id/person_hobby"                android:layout_toRightOf="@+id/person_photo"                android:layout_below="@+id/person_name"                />

        </RelativeLayout>

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

</LinearLayout>


<android.support.v7.widget.RecyclerView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/my_list"    />


Inialization

ArrayList<DeliveryLog> mDeliveryLog;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

mRecyclerView = (RecyclerView) v.findViewById(R.id.cards_sms);

mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(getActivity());
((LinearLayoutManager) mLayoutManager).setOrientation(LinearLayoutManager.VERTICAL); 
mRecyclerView.setLayoutManager(mLayoutManager);

Adapter

public class CardViewDataAdapter extends      RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
   public ArrayList<DeliveryLog> mDataset;

   // Provide a suitable constructor (depends on the kind of dataset)   public CardViewDataAdapter(ArrayList<DeliveryLog> myDataset) {
      mDataset = myDataset;
   }

   // Create new views (invoked by the layout manager)   @Override
   public CardViewDataAdapter.ViewHolder onCreateViewHolder(
         ViewGroup parent, int viewType) {
      // create a new view      View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.smsreportcontent, parent, false);

      ViewHolder viewHolder = new ViewHolder(itemLayoutView);
      return viewHolder;
   }

   // Replace the contents of a view (invoked by the layout manager)   @Override
   public void onBindViewHolder(ViewHolder holder, final int position) {
   holder.tvMessge.setText(mDeliveryLog.get(position).messge);

   }

   // Return the size of your dataset (invoked by the layout manager)   @Override
   public int getItemCount() {
      return mDeliveryLog.size();
   }

   // inner class to hold a reference to each item of RecyclerView   public class ViewHolder extends RecyclerView.ViewHolder {

      protected TextView tvName,tvHobby;

      public ViewHolder(View view) {
         super(view);
         tvtime = (TextView) view.findViewById(R.id.tvtime);
         tvSent = (TextView) view.findViewById(R.id.tvsent);

      }
   }
}

How to set

mAdapter = new CardViewDataAdapter(mDeliveryLog);
mRecyclerView.setAdapter(mAdapter);

No comments:

Post a Comment