Instead of worrying about inflating a new View
for the one item to be shown differently, why not just define another layout
in the current XML?
Take for example, an actual piece of code from an app of mine which shows the user's Facebook newsfeed (I have shortened it and taken off the unwanted code from it). A user could have a Status Update / Photo or a Video on his / her newsfeed. So instead of creating multiple layout XMLs, I define them all in the one XML that I inflate in the adapter
.
XML Example:
<!-- STATUS | PHOTO -->
<LinearLayout
android:id="@+id/linlaStatusPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
.... OTHER ELEMENTS NEEDED
</LinearLayout>
<!-- VIDEO LAYOUT -->
<LinearLayout
android:id="@+id/linlaVideo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
.... OTHER ELEMENTS NEEDED
</LinearLayout>
Now in the adapter, I check for the condition for the type of feed and show the necessary layout while hiding the other.
if (feedType.equals("status") || feedType.equals("photo")) {
// SHOW THE NECESSARY LAYOUT CONTAINER
holder.linlaStatusPhoto.setVisibility(View.VISIBLE);
// HIDE THE OTHERS
holder.linlaVideo.setVisibility(View.GONE);
// I HAVE MORE CONDITIONS TO CHECK. HENCE THE else if().
} else if (feedType.equals("video")) {
// SHOW THE NECESSARY LAYOUT CONTAINER
holder.linlaVideo.setVisibility(View.VISIBLE);
// HIDE THE OTHERS
holder.linlaStatusPhoto.setVisibility(View.GONE);
}
You can adapt this code easily to fit in your scheme of things. You will need to use some check to determine which Widget
has to be shown. But that is true even if you were to inflate another View
. If it is always the 7th item in the List, you can add one more parameter to your front_menu_data[]
that will hold the item number and use that to trigger the above example.
UPDATE:
Move the if...else
code block from here:
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new Front_menuHolder();
holder.txtTitle1 = (TextView)row.findViewById(R.id.menu_item);
holder.txtTitle2 = (TextView)row.findViewById(R.id.item_description);
holder.header2 = (TextView)row.findViewById(R.id.tools_header);
row.setTag(holder);
// MOVE THIS BLOCK
if (position != 6) {
holder.txtTitle1.setVisibility(View.VISIBLE);
holder.txtTitle2.setVisibility(View.VISIBLE);
holder.header2.setVisibility(View.GONE);
} else if (position == 6) {
holder.header2.setVisibility(View.VISIBLE);
holder.txtTitle1.setVisibility(View.GONE);
holder.txtTitle2.setVisibility(View.GONE);
}
} else {
holder = (Front_menuHolder)row.getTag();
}
To:
Front_menu front_menu = data[position];
if (position != 6) {
holder.txtTitle1.setVisibility(View.VISIBLE);
holder.txtTitle2.setVisibility(View.VISIBLE);
holder.header2.setVisibility(View.GONE);
holder.txtTitle1.setText(front_menu.item);
holder.txtTitle2.setText(front_menu.desc);
} else if (position == 6) {
holder.header2.setVisibility(View.VISIBLE);
holder.txtTitle1.setVisibility(View.GONE);
holder.txtTitle2.setVisibility(View.GONE);
holder.txtTitle1.setText(front_menu.item);
holder.txtTitle2.setText(front_menu.desc);
}