
Many apps need to display user-interface elements based on large data sets, or data that frequently changes. For example, a music app might need to display information about thousands of albums, but only a dozen of those albums might be on-screen at a time. If the app created UI widgets for each of those albums, the app would end up using a lot of memory and storage, potentially making the app slow and crash-prone. On the other hand, if the app created UI widgets each time a new album scrolled onto the screen and destroyed the widgets when it scrolled off, that would also cause the app to run slowly, since creating UI objects is a resource-intensive operation for more details refer here
will see now one sample example
First, you can below dependency in gradle
1 |
implementation 'com.android.support:design:27.0.2' |
activity_main.xml
Add recycler view to xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.admin.recycler.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recycle" app:layoutManager="android.support.v7.widget.LinearLayoutManager" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package com.example.admin.recycler; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.widget.ListView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { JSONObject j1=null; RecyclerView recyclerView; ArrayList<HashMap<String,String>> remind_list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView=findViewById(R.id.recycle); JSONObject jsonObject=new JSONObject(); try { jsonObject.put("action","get_reminder"); jsonObject.put("mobile","9533928284"); } catch (JSONException e) { e.printStackTrace(); } GetReminder getReminder=new GetReminder(); getReminder.execute(jsonObject.toString()); } private class GetReminder extends AsyncTask<String,String,String>{ String url = "http://androindian.com/apps/reminder/api.php"; @Override protected String doInBackground(String... params) { j1 = JsonFunction.getJsonFromUrlparam(url, params[0]); Log.i("json", "" + j1); return String.valueOf(j1); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); try { JSONObject jsonObject1 = new JSONObject(s); JSONArray jsonArray = jsonObject1.getJSONArray("data"); for(int i=0;i<jsonArray.length();i++) { JSONObject jsonObject2 = jsonArray.getJSONObject(i); HashMap<String,String> remindlist=new HashMap<>(); remindlist.put("title",jsonObject2.getString("title")); remindlist.put("date",jsonObject2.getString("date")); remindlist.put("time",jsonObject2.getString("time")); remindlist.put("place",jsonObject2.getString("place")); remindlist.put("alert",jsonObject2.getString("alert")); remindlist.put("reminder_cat",jsonObject2.getString("reminder_cat")); remind_list.add(remindlist); } recyclerView.setAdapter(new RecyclerAdapter(MainActivity.this,remind_list)); } catch (Exception e) { e.printStackTrace(); } } } } |
RecyclerAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.example.admin.recycler; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; /** * Created by Admin on 12/26/2017. */ public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> { ArrayList<HashMap<String,String>> remind_list=new ArrayList<>(); Context context; public RecyclerAdapter(MainActivity mainActivity, ArrayList<HashMap<String, String>> remind_list) { context=mainActivity; this.remind_list=remind_list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.custom,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int po) { Log.e("pod", remind_list.get(po).toString()); holder.t1.setText(remind_list.get(po).get("title")); holder.t2.setText(remind_list.get(po).get("date")); holder.t3.setText(remind_list.get(po).get("time")); holder.t4.setText(remind_list.get(po).get("place")); holder.t5.setText(remind_list.get(po).get("alert")); holder.t6.setText(remind_list.get(po).get("reminder_cat")); } @Override public int getItemCount() { return remind_list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { TextView t1,t2,t3,t4,t5,t6; public MyViewHolder(View view) { super(view); t1=view.findViewById(R.id.textView); t2=view.findViewById(R.id.textView2); t3=view.findViewById(R.id.textView3); t4=view.findViewById(R.id.textView4); t5=view.findViewById(R.id.textView5); t6=view.findViewById(R.id.textView6); } } } |
custom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?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="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> |
Output: