<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Salih Akşit on Medium]]></title>
        <description><![CDATA[Stories by Salih Akşit on Medium]]></description>
        <link>https://medium.com/@salihaksit7?source=rss-cb58f98e771b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qNNNxkoNtBzeYvH2.</url>
            <title>Stories by Salih Akşit on Medium</title>
            <link>https://medium.com/@salihaksit7?source=rss-cb58f98e771b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:25:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@salihaksit7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Extended Version of Generic RecyclerView.Adapter]]></title>
            <link>https://medium.com/@salihaksit7/extended-version-of-generic-recyclerview-adapter-dee98d689f6f?source=rss-cb58f98e771b------2</link>
            <guid isPermaLink="false">https://medium.com/p/dee98d689f6f</guid>
            <category><![CDATA[search]]></category>
            <category><![CDATA[recyclerview]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Salih Akşit]]></dc:creator>
            <pubDate>Wed, 18 Jul 2018 20:40:13 GMT</pubDate>
            <atom:updated>2018-07-18T20:40:13.237Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://medium.com/@salihakit/generic-recyclerview-adapter-with-pre-determined-item-height-99af31952d9e">Hi everyone, this is extended version of https://medium.com/@salihakit/generic-recyclerview-adapter-with-pre-determined-item-height-99af31952d9e</a> . The new features are onItemClickListener and search filter.</p><p>To accomplish search implementation , I added new method into LayoutModel which is “String getFilterVariable();”.</p><pre>public interface  LayoutModel  {<br><br>    int getLayoutId();<br><br>    String getFilterVariable();<br>}</pre><p>Since all items must implement this interface, you can determine according to which variable you use in order to search.For instance, I determine to use “name”.</p><pre>public class DummyModel implements LayoutModel {<br>    public String name;<br>    public String surname;<br>    private int rowHeight;</pre><pre>    public DummyModel(String name) {<br>        this.name = name;<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public void setName(String name) {<br>        this.name = name;<br>    }<br>    public int getRowHeight() {<br>        if (Hawk.<em>contains</em>(ROWHEIGHTSMALL)){<br>            return Hawk.<em>get</em>(ROWHEIGHTSMALL);<br>         }<br>         return -1;<br>   }<br>    @Override<br>    public int getLayoutId() {<br>        return R.layout.<em>item_dummy</em>_model;<br>    }<br>    @Override<br>    public String getFilterVariable() {<br>        return name;<br>    }<br>}</pre><p>Now, what I missed on previous version is that I eventually need a kind of ItemClickListener. So I added it on our adapter constructors. My ItemClickListener :</p><pre>public interface ItemClickListener&lt;T&gt; {<br><br>    void onItemClick(T item);<br><br>}</pre><p>So last version of our adapter is this:</p><pre>public class BaseRecyclerAdapter&lt;T extends LayoutModel&gt; extends RecyclerView.Adapter&lt;BaseRecyclerAdapter.ViewHolder&gt; {<br><br>    ArrayList&lt;T&gt; itemList;<br>    ArrayList&lt;T&gt; originalList;<br>    private Context ctx; // in case you need it<br>    ItemClickListener&lt;T&gt; listener;<br><br>    public class ViewHolder extends RecyclerView.ViewHolder {<br>        private final ViewDataBinding binding;<br><br>        public ViewHolder(ViewDataBinding binding) {<br>            super(binding.getRoot());<br>            this.binding = binding;<br>        }<br><br>        public ViewDataBinding getBinding() {<br>            return binding;<br>        }<br>    }<br><br>    public BaseRecyclerAdapter() {<br>        super();<br>    }<br><br>    public BaseRecyclerAdapter(ItemClickListener&lt;T&gt; listener) {<br>        super();<br>        this.listener = listener;<br>    }<br><br>    public BaseRecyclerAdapter(ArrayList&lt;T&gt; list, ItemClickListener&lt;T&gt; listener) {<br>        super();<br>        this.itemList = list;<br>        this.originalList = new ArrayList&lt;&gt;();<br>        this.originalList.addAll(itemList);<br>        this.listener = listener;<br>    }<br><br><br>    @Override<br>    public int getItemViewType(int position) {<br>        return itemList.get(position).getLayoutId();<br>    }<br><br>    @NonNull<br>    @Override<br>    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {<br>        LayoutInflater layoutInflater =<br>                LayoutInflater.<em>from</em>(viewGroup.getContext());<br>        ViewDataBinding binding = DataBindingUtil.<em>inflate</em>(<br>                layoutInflater, viewType, viewGroup, false);<br>        ctx = viewGroup.getContext();<br>        return new ViewHolder(binding);<br>    }<br><br>    @Override<br>    public void onBindViewHolder(@NonNull BaseRecyclerAdapter.ViewHolder holder, int position) {<br>        if (getItem(position) != null) {<br>            T item = getItem(position);<br>            holder.getBinding().setVariable(<em>obj</em>, item);<br>            holder.getBinding().setVariable(<em>itemClickListener</em>, listener);<br>            holder.getBinding().executePendingBindings();<br>        }<br>    }<br><br><br>    @Override<br>    public int getItemCount() {<br>        return itemList.size();<br>    }<br><br>    public T getItem(int index) {<br>        return ((itemList != null &amp;&amp; index &lt; itemList.size()) ? itemList.get(index) : null);<br>    }<br><br>    public void setList(ArrayList&lt;T&gt; list) {<br>        itemList = list;<br>        this.originalList = new ArrayList&lt;&gt;();<br>        this.originalList.addAll(itemList);<br>        notifyDataSetChanged();<br>    }<br><br>    public ArrayList&lt;? extends T&gt; getList() {<br>        return itemList;<br>    }<br><br>    public void reset() {<br>        itemList.clear();<br>        notifyDataSetChanged();<br>    }<br><br>    public void filter(String charText) {<br>        charText = charText.toLowerCase(Locale.<em>getDefault</em>());<br>        itemList.clear();<br>        if (charText.length() == 0) {<br>            itemList.addAll(originalList); //default list<br>        } else {<br>            for (T object : originalList) {<br>                if (object.getFilterVariable().toLowerCase().trim().startsWith(charText)) {<br>                    itemList.add(object);<br>                }<br>            }<br>        }<br>        notifyDataSetChanged();<br>    }<br><br>    @BindingAdapter(&quot;android:layout_height&quot;)<br>    public static void setLayoutHeight(View view, Integer height) {<br>        try {<br>            view.getLayoutParams().height = height;<br>        } catch (Exception ignore)<br><br>        {<br>        }<br>    }<br><br>}</pre><p>First trick is you have to use same variable name ,“<em>itemClickListener</em>”, on each xml file.Other one is you can not use “&lt;” or “&gt;” on type section, so just replace it with “&amp;lt;” and “&amp;gt;” respectively.I added onClick method to main layout, but it is up to you (surely). So our xml became:</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;<br>    &lt;data &gt;<br>        &lt;variable<br>            name=&quot;obj&quot;<br>           type=&quot;.DummyModel&quot; /&gt;<br>&lt;variable<br>    name=&quot;itemClickListener&quot;<br>    type=&quot;.ItemClickListener&amp;lt;DummyModel&amp;gt;&quot; /&gt;</pre><pre>    &lt;/data&gt;<br>    &lt;android.support.constraint.ConstraintLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:onClick=&quot;@{() -&gt; itemClickListener.onItemClick(obj)}&quot;<br>        android:layout_height=&quot;@{obj.rowHeight, default=wrap_content}&quot;&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_top&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;horizontal&quot;<br>            app:layout_constraintGuide_percent=&quot;0.3&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_bottom&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;horizontal&quot;<br>            app:layout_constraintGuide_percent=&quot;0.7&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_start&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;vertical&quot;<br>            app:layout_constraintGuide_percent=&quot;0.0416666&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_arrow_end&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;vertical&quot;<br>            app:layout_constraintGuide_percent=&quot;0.93333333&quot;/&gt;<br><br><br><br>        &lt;android.support.v7.widget.AppCompatTextView<br>            android:id=&quot;@+id/tv_dummy_item&quot;<br>            android:layout_width=&quot;0dp&quot;<br>            android:layout_height=&quot;0dp&quot;<br>            app:autoSizeTextType=&quot;uniform&quot;<br>            app:autoSizeMinTextSize=&quot;1sp&quot;<br>            app:autoSizeStepGranularity=&quot;1sp&quot;<br>            android:text=&quot;@{obj.name}&quot;<br>            app:layout_constraintEnd_toEndOf=&quot;@id/dummy_item_end&quot;<br>            app:layout_constraintBottom_toBottomOf=&quot;@id/dummy_item_bottom&quot;<br>            app:layout_constraintStart_toStartOf=&quot;@id/dummy_item_start&quot;<br>            app:layout_constraintTop_toTopOf=&quot;@id/dummy_item_top&quot; /&gt;<br><br><br>&lt;/layout&gt;</pre><p>Our next goals may be:</p><ul><li>What do we do if we need more than one click on the same item ?</li><li>There should be options for search from starting or contains or customized…</li><li>….</li></ul><p>Please contact me if you have any suggestions/comments or …..(you can replace this dots with the reason to meet your needs)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dee98d689f6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic RecyclerView.Adapter with pre-determined item height]]></title>
            <link>https://medium.com/@salihaksit7/generic-recyclerview-adapter-with-pre-determined-item-height-99af31952d9e?source=rss-cb58f98e771b------2</link>
            <guid isPermaLink="false">https://medium.com/p/99af31952d9e</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-data-binding]]></category>
            <category><![CDATA[generic-recycler-view]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Salih Akşit]]></dc:creator>
            <pubDate>Mon, 16 Jul 2018 21:26:01 GMT</pubDate>
            <atom:updated>2018-07-16T21:26:01.912Z</atom:updated>
            <content:encoded><![CDATA[<p>If you use databinding, basically you only need one generic adapter for all RecyclerViews. Just copy the below adapter, after scrolling down.</p><p>This is LayoutModel which you can implement your models , since it determines which layout your adapter use for convenient model.</p><pre>public interface  LayoutModel  {<br><br>    int getLayoutId();<br>}</pre><p>This one is the dummy model which replace with “T” at adapter. If you notice there is a variable namely rowHeight which we can use to determine height of the recyclerview’s item if you want to use AutoTextSize and/or GuideLine. I used “Hawk” , the 3-rd party library inorder to get calculated height from start-activity.</p><pre>public class DummyModel implements LayoutModel {<br>    public String name;<br>    private int rowHeight;</pre><pre>public DummyModel(String name) {<br>        this.name = name;<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public void setName(String name) {<br>        this.name = name;<br>    }<br>    public int getRowHeight() {<br>        if (Hawk.<em>contains</em>(ROWHEIGHTSMALL)){<br>            return Hawk.<em>get</em>(ROWHEIGHTSMALL);<br>         }<br>         return -1;<br>   }<br>    @Override<br>    public int getLayoutId() {<br>        return R.layout.<em>item_dummy</em>_model;<br>    }<br><br>}</pre><p>The variable name should be “obj” since we have one adapter and we want to keep using for all of recyclerviews. If you want to use app:autoSizeTextType=”uniform” , AppCompatTextView is placed in bounded area. Otherwise it may behave odd.That’s why we use Guideline. But this is an item of recyclerview, so we do not (generally) use match_parent and we want it to be dynamic (which has always the same ratio on every screen size )</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;layout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;<br>    &lt;data &gt;<br>        &lt;variable<br>            name=&quot;obj&quot;<br>           type=&quot;.DummyModel&quot; /&gt;<br>    &lt;/data&gt;<br>    &lt;android.support.constraint.ConstraintLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;@{obj.rowHeight, default=wrap_content}&quot;&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_top&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;horizontal&quot;<br>            app:layout_constraintGuide_percent=&quot;0.3&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_bottom&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;horizontal&quot;<br>            app:layout_constraintGuide_percent=&quot;0.7&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_item_start&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;vertical&quot;<br>            app:layout_constraintGuide_percent=&quot;0.0416666&quot;/&gt;<br><br>        &lt;android.support.constraint.Guideline<br>            android:id=&quot;@+id/dummy_arrow_end&quot;<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;vertical&quot;<br>            app:layout_constraintGuide_percent=&quot;0.93333333&quot;/&gt;<br><br><br><br>        &lt;android.support.v7.widget.AppCompatTextView<br>            android:id=&quot;@+id/tv_dummy_item&quot;<br>            android:layout_width=&quot;0dp&quot;<br>            android:layout_height=&quot;0dp&quot;<br>            app:autoSizeTextType=&quot;uniform&quot;<br>            app:autoSizeMinTextSize=&quot;1sp&quot;<br>            app:autoSizeStepGranularity=&quot;1sp&quot;<br>            android:text=&quot;@{obj.name}&quot;<br>            app:layout_constraintEnd_toEndOf=&quot;@id/dummy_item_end&quot;<br>            app:layout_constraintBottom_toBottomOf=&quot;@id/dummy_item_bottom&quot;<br>            app:layout_constraintStart_toStartOf=&quot;@id/dummy_item_start&quot;<br>            app:layout_constraintTop_toTopOf=&quot;@id/dummy_item_top&quot; /&gt;<br><br><br>&lt;/layout&gt;</pre><p>You may change &lt;DummyModel&gt; with your model and recyclerview also.</p><pre>private BaseRecyclerAdapter&lt;DummyModel&gt; adapter = new BaseRecyclerAdapter&lt;&gt;();<br>private ArrayList&lt;DummyModel&gt; dummyModelList;</pre><pre>recyclerView.setAdapter(adapter);<br>adapter.setList(dummyModelList);</pre><p>The below code is in start-activity. 0.0694444f is my ratio which represents (50dp/designScreenHeight).You have to change this according to yours.</p><pre>DisplayMetrics displayMetrics = new DisplayMetrics();<br>getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);<br><br>Hawk.<em>put</em>(ROWHEIGHTSMALL,(int) (displayMetrics.heightPixels * (0.0694444f)));</pre><p>Finally the adapter:</p><pre>public class BaseRecyclerAdapter&lt;T extends LayoutModel&gt; extends RecyclerView.Adapter&lt;BaseRecyclerAdapter.ViewHolder&gt;<br>        {<br><br>    ArrayList&lt;? extends T&gt; itemList;<br>    private Context mContext;<br><br>    public class ViewHolder extends RecyclerView.ViewHolder {<br>        private final ViewDataBinding binding;<br><br>        public ViewHolder(ViewDataBinding binding) {<br>            super(binding.getRoot());<br>            this.binding = binding;<br>        }<br><br>        public ViewDataBinding getBinding() {<br>            return binding;<br>        }<br>    }<br><br>    public BaseRecyclerAdapter() {<br>        super();<br>    }<br><br>    public BaseRecyclerAdapter(ArrayList&lt;? extends T&gt; list) {<br>        super();<br>        this.itemList = list;<br>    }<br><br><br>    @Override<br>    public int getItemViewType(int position) {<br>        return itemList.get(position).getLayoutId();<br>    }<br><br>    @NonNull<br>    @Override<br>    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {<br>        LayoutInflater layoutInflater =<br>                LayoutInflater.<em>from</em>(viewGroup.getContext());<br>        ViewDataBinding binding = DataBindingUtil.<em>inflate</em>(<br>                layoutInflater, viewType, viewGroup, false);<br>        mContext = viewGroup.getContext();<br>        return new ViewHolder(binding);<br>    }<br><br>    @Override<br>    public void onBindViewHolder(@NonNull BaseRecyclerAdapter.ViewHolder holder, int position) {<br>        if (getItem(position) != null) {<br>            T item = getItem(position);<br>            holder.getBinding().setVariable(<em>obj</em>, item);</pre><pre>        }<br>    }<br><br><br>    @Override<br>    public int getItemCount() {<br>        return itemList.size();<br>    }<br><br>    public T getItem(int index) {<br>        return ((itemList != null &amp;&amp; index &lt; itemList.size()) ? itemList.get(index) : null);<br>    }<br><br>    public void setList(ArrayList&lt;? extends T&gt; list) {<br>        itemList = list;<br>        notifyDataSetChanged();<br>    }<br><br>    public ArrayList&lt;? extends T&gt; getList() {<br>        return itemList;<br>    }<br><br>    public void reset() {<br>        itemList.clear();<br>        notifyDataSetChanged();<br>    }<br><br>    @BindingAdapter(&quot;android:layout_height&quot;)<br>    public static void setLayoutHeight(View view, Integer height) {<br>        try {<br>           view.getLayoutParams().height = height;<br><br>        } catch (Exception ignore)<br><br>        {<br>        }<br>    }<br><br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99af31952d9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>