Skip to content Skip to sidebar Skip to footer

Gridview Expand Code

I am making an app in which I want to set images onto gridview and on itemclick I want to show the description of an image via textview. I have already done the setting of images o

Solution 1:

You can try the following approach for your reference : I am using text view instead of ImageView. But the concept remain same. It is displayed something like this. enter image description here

Const.java : Class to keep constants.

publicclassConst {
publicstaticfinalint DES_VIEW=0;
publicstaticfinalint FOOD_VIEW=1;
publicstaticfinalint FAKE_VIEW=2;
publicstaticfinalint COLUMN_NUMBER = 2;
}

RowData.java : An Interface which will be used by the adapter class to get view type of each element.

publicinterfaceRowData {
intgetViewType();
}

FoodItem.java : Just a plain pojo class which implements RowData ( The parent item FoodItem item 0 is populated )

publicclassFoodItemimplementsRowData{
public int getViewType() {
    return viewType;
}

publicvoidsetViewType(int viewType) {
    this.viewType = viewType;
}

private int viewType;
publicbooleanisExpanded() {
    return isExpanded;
}

publicvoidsetExpanded(boolean expanded) {
    isExpanded = expanded;
}

privateboolean isExpanded;

publicStringgetFoodName() {
    return foodName;
}

publicvoidsetFoodName(String foodName) {
    this.foodName = foodName;
}

privateString foodName;

publicStringgetDescription() {
    return description == null ? "DEFAULT" : description;
}

publicvoidsetDescription(String description) {
    this.description = description;
}

privateString description;

}

FakeData.java : Pojo class to populate empty views.

publicclassFakeDataimplementsRowData {
@OverridepublicintgetViewType() {
    return Const.FAKE_VIEW;
}
}

FoodAdapter.java : Adapter class.

publicclassFoodAdapterextendsRecyclerView.Adapter<RecyclerView.ViewHolder> {

publicvoidsetFoodItems(List<RowData> foodItems) {
    this.foodItems = foodItems;
   // addBlankViews();
}



private List<RowData> foodItems;

@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;

    switch (viewType) {
        case Const.FOOD_VIEW:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
            returnnewVH(view);
        case Const.DES_VIEW:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.des, parent, false);
            returnnewDesView(view);

        case Const.FAKE_VIEW:
            Log.e("CALLED", " ---> " + viewType);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
            returnnewFakeView(view);
            default:
                returnnull;
    }
}

@OverridepublicvoidonBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    intview= getItemViewType(position);

    switch (view) {
        case Const.FAKE_VIEW: {
            FakeViewfakeView= (FakeView) holder;
            Log.e("CALLED", position + " " + view);
        }
        break;

        case Const.DES_VIEW: {
            DesViewdesView= (DesView) holder;
            desView.setData((FoodItem) getFoodItem(position));
        }
        break;
        case Const.FOOD_VIEW: {
            VHfoodView= (VH) holder;
            foodView.setData((FoodItem) getFoodItem(position));

        }
        break;

    }

}

RowData getFoodItem(int position) {
    if (foodItems != null) {
        return foodItems.get(position);
    }
    returnnull;
}

@OverridepublicintgetItemViewType(int position) {
    RowDatafoodItem= getFoodItem(position);
    if (foodItem != null) {
        return foodItem.getViewType();

    }
    return -1;
}

@OverridepublicintgetItemCount() {
    if (foodItems == null) {
        return0;
    } else {
        return foodItems.size();
    }

}


classVHextendsRecyclerView.ViewHolder {
    TextView foodItemName;


    publicVH(View itemView) {
        super(itemView);
        foodItemName = itemView.findViewById(R.id.food_item);
        foodItemName.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                FoodAdapter.this.onClick(getAdapterPosition());

            }
        });
    }


    voidsetData(FoodItem data) {
        foodItemName.setText(data.getFoodName());
    }
}

classDesViewextendsRecyclerView.ViewHolder {
    TextView foodDesView;


    publicDesView(View itemView) {
        super(itemView);
        foodDesView = itemView.findViewById(R.id.des_item);
        foodDesView.setTextColor(Color.BLUE);
    }


    voidsetData(FoodItem data) {
        foodDesView.setText(data.getDescription());
    }
}

classFakeViewextendsRecyclerView.ViewHolder {

    publicFakeView(View itemView) {
        super(itemView);

    }
}

privateintcurrentExpandedIndex= -1;

privatevoidonClick(int position) {
 //  calculation to show and hide the child view (with magenta background ).if (currentExpandedIndex != -1) {
        foodItems.remove(currentExpandedIndex);
        currentExpandedIndex = -1;
        notifyDataSetChanged();
        return;
    }
    intstartIndex= position % Const.COLUMN_NUMBER;
    if (startIndex == 0) {
        startIndex = position;
    } else {
        startIndex = position - startIndex;
    }
    startIndex = startIndex + Const.COLUMN_NUMBER;

    if (currentExpandedIndex == -1) {
        FoodItemdes=newFoodItem();
        des.setViewType(Const.DES_VIEW);
        FoodItemfoodItem= (FoodItem) getFoodItem(position);
        des.setDescription(foodItem.getDescription());
        foodItems.add(startIndex, des);
        currentExpandedIndex = startIndex;
    }
    notifyDataSetChanged();
}
}

MainActivity.java : Activity class.

publicclassMainActivityextendsAppCompatActivity {
private FoodAdapter foodAdapter;


@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerViewrecyclerView= findViewById(R.id.reycleView);
    ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

    foodAdapter = newFoodAdapter();
    foodAdapter.setFoodItems(getFoodItems());
    GridLayoutManagermLayoutManager=newGridLayoutManager(this,Const.COLUMN_NUMBER);
    mLayoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
        @OverridepublicintgetSpanSize(int position) {
            switch (foodAdapter.getItemViewType(position)) {
                case Const.DES_VIEW:
                    return Const.COLUMN_NUMBER;
                default:
                    return1;
            }


        }
    });
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(foodAdapter);


}

private List<RowData> foodItems;

private List<RowData> getFoodItems() {
    foodItems = newArrayList<>();
    for (inti=0; i < 18; i++) {
        FoodItemfoodItem=newFoodItem();
        foodItem.setFoodName(" FoodItem item " + i);
        foodItem.setExpanded(false);
        foodItem.setDescription("Fake des of " + i);
        foodItem.setViewType(Const.FOOD_VIEW);
        foodItems.add(foodItem);
    }
    // calculation to add empty view.// these view will be added when, there is shortage of parent views to   fill row completely  intsize= foodItems.size();
    intblankView= size % Const.COLUMN_NUMBER;
    if (blankView != 0) {
        blankView = Const.COLUMN_NUMBER - blankView;
        for (inti=0; i < blankView; i++) {
            foodItems.add(newFakeData());
        }
    }

    return foodItems;

}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"tools:context="check.service.com.genericrecylerview.MainActivity"><android.support.v7.widget.RecyclerViewandroid:id="@+id/reycleView"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

des.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:background="@color/colorAccent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/des_item"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:orientation="vertical"android:layout_height="wrap_content"><TextViewandroid:id="@+id/food_item"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

Post a Comment for "Gridview Expand Code"