Pengenalan Android Advanced View Card View

Android Cardview (Sumber Gambar Google Images)

Ini adalah widget baru untuk Android, yang bisa digunakan untuk menampilkan semacam kartu layout di android.

Dalam tutorial ini kita akan menggunakan RecyclerView untuk menampilkan daftar Card Views.

buat layout baru dengan nama “card_view_row” (card_view_row.xml).

<?xml version="1.0" encoding="utf-8"?&gt;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
 
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp"&gt;
 
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"&gt;
 
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textStyle="bold"/&gt;
 
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"/&gt;
        </RelativeLayout&gt;
    </android.support.v7.widget.CardView&gt;

</LinearLayout&gt;

Sebelum memulai, jangan lupa sertakan dependensi ini di aplikasi Anda secara bertahap. Masukan dalam Build.gradle kalian.

dependencies {
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support:cardview-v7:27.0.1'
    compile 'com.android.support:recyclerview-v7:27.0.1'
}

Langkah Selanjutnya: lihatlah layout activity, yang menunjukkan RecyclerView :

Cukup tambahkan RecyclerView di Layout Activity CardView Anda. buat layout bernama “activity_card_view” (activity_card_view.xml).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".CardViewActivity"&gt;

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/&gt;

</RelativeLayout&gt;

Dalam contoh CardView Android ini, kita akan menggunakan objek ini untuk memasok data ke adaptor :

Buat class baru yang disebut : “DataObject” (DataObject.java)

public class DataObject {
    private String mText1;
    private String mText2;
 
    DataObject (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }
 
    public String getmText1() {
        return mText1;
    }
 
    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }
 
    public String getmText2() {
        return mText2;
    }
 
    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}

Sekarang di Activity CardView Anda, gunakan kartu Anda, buat class java baru bernama “CardViewActivity” (CardViewActivity.java).

public class CardViewActivity extends AppCompatActivity {
 
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "CardViewActivity";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view);
 
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);
 
    }
 
 
    @Override
    protected void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new 	MyRecyclerViewAdapter
                .MyClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Log.i(LOG_TAG, " Clicked on Item " + position);
            }     });   }
 
    private ArrayList<DataObject&gt; getDataSet() {
        ArrayList results = new ArrayList<DataObject&gt;();
        for (int index = 0; index < 20; index++) {
            DataObject obj = new DataObject("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }
        return results;    }}

Buat sebuah Adapter:

Dengan membuat class MyRecyclerViewAdapter baru (MyRecyclerViewAdapter.java).

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder&gt; {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<DataObject&gt; mDataset;
    private static MyClickListener myClickListener;
 
    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;
 
        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }
 
        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }
 
    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }
 
    public MyRecyclerViewAdapter(ArrayList<DataObject&gt; myDataset) {
        mDataset = myDataset;
    }

  @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_row, parent, false);
 
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }
 
    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }
 
    public void addItem(DataObject dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }
 
    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }
 
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
 
    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

Sumber : Master Android (App)

Bagikan Melalui :

Post Author: Poncoe

Seorang Anak payah yang bercita-cita menjadi penulis dan membuat terkesan cewek yang ia sukai. Founder of LuckyTrue Development

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *