Yang saya posting disini yaitu source code Recyclerview sesuai judul. Meskipun recyclerview sudah lama muncul namun saya belum pernah mengiplementasikan pada aplikasi android buatan saya hingga bulan Februari 2017. Penggunaan recyclerview pada aplikasi saya didasari karena saya membutuhan media yang mampu menampilkan puluhan ribu baris bertipe string. Sebelumnya menggunakan TextView namun aplikasi selalu error berkali2. Sebenarnya untuk masalah ini listview bisa dijadikan bahan percobaan sbelum melangkah menggunakan recyclerview. Recyclerview hampir mirip dengan listview namun recyclerview lebih canggih terutama disisi performa.
Gif 1. Hasil Tutorial |
XML:
1.activity_main.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" > <!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </RelativeLayout>
2.rowlayout,xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_marginRight="6dip" android:contentDescription="TODO" android:src="@android:mipmap/sym_def_app_icon" /> <TextView android:id="@+id/secondLine" android:layout_width="fill_parent" android:layout_height="26dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_toRightOf="@id/icon" android:ellipsize="marquee" android:singleLine="true" android:text="Description" android:textSize="12sp" /> <TextView android:id="@+id/firstLine" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/secondLine" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_alignWithParentIfMissing="true" android:layout_toRightOf="@id/icon" android:gravity="center_vertical" android:text="Example application" android:textSize="16sp" /> </RelativeLayout>
Java:
1. MainActivity.javaimport android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) ArrayList<String> mDataset=new ArrayList<>(); for(int i=0;i<10;i++){ String a="test "+i; mDataset.add(a);} mAdapter = new MyAdapter(mDataset); mRecyclerView.setAdapter(mAdapter); } }
2. MyAdapter.java
import java.util.ArrayList; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private ArrayList<String> mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView txtHeader; public TextView txtFooter; public ViewHolder(View v) { super(v); txtHeader = (TextView) v.findViewById(R.id.firstLine); txtFooter = (TextView) v.findViewById(R.id.secondLine); } } public void add(int position, String item) { mDataset.add(position, item); notifyItemInserted(position); } public void remove(String item) { int position = mDataset.indexOf(item); mDataset.remove(position); notifyItemRemoved(position); } // Provide a suitable constructor (depends on the kind of dataset) public MyAdapter(ArrayList<String> myDataset) { mDataset = myDataset; } // Create new views (invoked by the layout manager) Context context; @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new vie context=parent.getContext(); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false); // set the view's size, margins, paddings and layout parameters ViewHolder vh = new ViewHolder(v); return vh; } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(final ViewHolder holder, final int position) { // - get element from your dataset at this position // - replace the contents of the view with that element final String name = mDataset.get(position); holder.txtHeader.setText(mDataset.get(position)); holder.txtHeader.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // remove(name); Toast toast= Toast.makeText(context, "Recycler "+position, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP| Gravity.CENTER_HORIZONTAL, 0, 0); toast.show(); } }); holder.txtFooter.setText("Footer: " + mDataset.get(position)); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.size(); } }
3. tambahkan compile 'com.android.support:recyclerview-v7:24.2.1'
pada build.grandle (Module:app), ubah versi 24.2.1 sesuai dengan versi appcompat mu
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:recyclerview-v7:24.2.1' compile 'com.android.support:appcompat-v7:24.2.1' }
Silahkan ber-experiment ria untuk mengenal Recyclerview lebih dalam..
Terimakasih...
wow, emejing gak ngerti ane bang
ReplyDeleteblower infrared