Search code examples
androidonclicklistener

How to Set OnClickListener to Card in PlaceholderView?


I am using PlaceHolderView library to show an Infinite List of contents like the Facebook Android app. I was using InfinitePlaceHolderView for it. It looks like this:

And here is how it looks like output

Now If the story is large then a user can click on the card view and it will take him to a new activity where there will be a detail of that story. For this, I need to add a Listener. But I could not find any documentation at all, how to implement it. The documentation shows that all those cards are dynamically created and not any of those cards have any id so that I can put a listener.

How to implement it?


Solution

  • So, what I did is given below:

    In my feed.xml file which contain the above view, I set an id for the whole view. Then I used that id and used it ItemView.java from here:

    feed.xml

    <?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:id="@+id/home_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_100"
        android:orientation="vertical"
        android:weightSum="4"
        tools:context="com.example.shamsad.tutionhub.activities.FeedActivity">
    
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:id="@+id/tbHome"
            android:background="@color/colorPrimary"
            app:titleTextColor="@color/grey_100">
        </android.support.v7.widget.Toolbar>
    
        <com.mindorks.placeholderview.InfinitePlaceHolderView
            android:id="@+id/loadMoreView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    
    </LinearLayout>
    

    ItemView.java

    @Click(R.id.rootView)
        public void onCardClick() {
            Intent intent = new Intent(mContext, DetailActivity.class);
            intent.putExtra("title", mInfo.getTitle());
            intent.putExtra("caption", mInfo.getCaption());
            intent.putExtra("salary", mInfo.getSalary());
            intent.putExtra("routine", mInfo.getRoutine());
            intent.putExtra("time", mInfo.getTime());
            Log.d("sajid", "onClick");
            mContext.startActivity(intent);
        }
    

    So, in this onCardClick() method, the clickable id is set up by this code @Click(R.id.rootView). And that's all. It basically sets the whole card-view as a clickable thing.

    And also thanks to the writer of this great library Janisher Ali. I contacted him and he helped a lot, and it worked.

    This is a great library. To me much better than RecyclerView. You can try it out :)