Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaAndroid View Evolution – PlaceHolderView

Android View Evolution – PlaceHolderView

Those days are gone when ListView, GridView, RecyclerView, and Pesky Adapters ruled the arena. Say Hello to PlaceHolderView ???? Adding listeners to each RecyclerView and implementing dynamic content is a tedious operation. Let’s not even talk about animation; if we have to perform it for nested Recyclers, it’ll be a disaster. What if we could replace ListView, GridView, and RecyclerView with ease and simplicity by using a very basic interface with additional capability and no adapter implementation?

What is the Purpose of the PlaceHolderView Class?

PlaceHolderView automates the process of configuring View adapters by removing all of the boilerplate code. The incredible thing is that there is no need for an adaptor. The code becomes incredibly modular, and the ability to add annotations to any view or method gives it a lot of flexibility. There are no unsightly codes like findViewById or onClickListener.

  • With just one line of code, you can create amazing animations for the view objects.
  • Memory management is a breeze.

How to Introduce this Great View in The project?

Well, the answer is pretty simple. PlaceHolderView is a view that allows you to see what’s going on. Some of the views in the PlaceHolderView library are based on RecyclerView, while the others are created from scratch. To produce the binding classes, all of the annotations are processed during the build process. There are two different variants to choose from.

  1. Uses Java reflection in the older version (branch 1.x).
  2. Annotation Processing is now supported in the new version (branch 2.x).

To add this library to your Android Project simply put up this line in your Gradle (project) and then hit ‘Sync’

Kotlin




implementation 'com.mindorks.android:placeholderview:1.0.3'
implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'com.mindorks.android:placeholderview:1.0.3'
kapt 'com.mindorks.android:placeholderview-compiler:1.0.3'


And then magically the PlaceHolderView is added to your current Android Project

Step #1: In the XML layout, create a PlaceHolderView.After that you need to tweak your XML files and add or replace the current view, to do so, just follow the below code snippet

XML




<com.mindorks.placeholderview.PlaceHolderView
android:id="@+id/geeksForGeeksView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>


Now Step #2: Create the view of the items which you need to inflate in your PlaceHolder View

XML




<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:padding="10dp">
    
  <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp"
    app:cardElevation="10dp">
        <ImageView
             android:id="@+id/geekyImage"
             android:layout_width="match_parent"
             android:layout_height="200dp"
             android:scaleType="centerCrop"
            android:src="@android:color/holo_orange_dark"/>
  </android.support.v7.widget.CardView>
    
</LinearLayout>


Step #3: Now in the MainActivity or Adapter Activity Java file just bind the methods so that they can be identified during the runtime

Java




@Animate(Animation.ENTER_RIGHT_DESC)
@NonReusable
@Layout(R.layout.gallery_item_big)
public class GeeksImage {
    @View(R.id.imageView) private ImageView imageView;
    private String mUrl;
    private Context mContext;
    private PlaceHolderView mPlaceHolderView;
    public ImageTypeBig(Context context,
                        PlaceHolderView placeHolderView,
                        String url)
    {
        mContext = context;
        mPlaceHolderView = placeHolderView;
        mUrl = url;
    }
    @Resolve private void onResolved()
    {
        Glide.with(mContext).load(mUrl).into(imageView);
    }
    @LongClick(R.id.imageView) private void onLongClick()
    {
  
        mPlaceHolderView.removeView(this);
    }
}


Step #4. For this last step just inflate the views to the placeholder view and you are good to go 

Java




PlaceHolderView mGalleryView = (PlaceHolderView)findViewById(R.id.galleryView);
.setHasFixedSize(true
.setItemViewCacheSize(15
.setLayoutManager(new GridLayoutManager(this, 3));
mGalleryView.addView(new GeekImage(this.getApplicationContext(), mGalleryView, url1));
.addView(new GeekImage(this.getApplicationContext(), mGalleryView, url2));
.addView(new GeekImage(this.getApplicationContext(), mGalleryView, url3));
.addView(new GeekImage(this.getApplicationContext(), mGalleryView, url4));


That’s it!

You just mastered the art of using PlaceHolderView and you will never look back to the older view options, and will now seem amazingly inferior. 

GeekTip: PlaceHolderView will recycle viewItems and attempt to refill the data of the current viewItem with similar/same viewtype viewItems. As a result, whenever the viewItem is associated to the window, the method annotated with @Resolve will be invoked. That is, if you don’t explicitly populate the viewItem in a method annotated with @Resolve, that viewItem may display the history of a previously used viewItem.

Rather than using a method marked with @Resolve, try to instantiate any class in the function Object() { [native code] }. 

Make the list empty initially if the itemView contains PlaceHolderView/ListViews and the item is being added using a method marked with @Resolve. This is necessary because if the recycled view contains PlaceHolderView/ListViews of other itemViews, duplicate viewitems may be inserted. Before adding views to a method marked with @Resolve, use removeAllViews() for PlaceHolderView.

RELATED ARTICLES

Most Popular

Recent Comments