Hamza Polat Hamza Polat - 3 months ago 12
Android Question

How to load a new template on a selected grid view item in android

I'm new at android. and I want to load a new template which contains two button on a selected item of grid view object.
Is that possible.

I added a gridview to my project and by using base adapter a template was loaded to each item of gridview. But what I want is that when I clicked an item of gridview, I want to load a new template (layout) to the selected item.

Base Adapter

public class KategoriAdapter extends BaseAdapter{

private Context mContext;
private String[] categoryValues;
private Bitmap[] pictures;
private String mTag ;

//indicate that positon for new template
private int mNewTemplatePos = -1;

//indicate that this is normal template view
private final String NORMAL_TEMPLATE = "NORMAL_TEMPLATE";

//indicate that this is new template view
private final String NEW_TEMPLATE = "NEW_TEMPLATE";

public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
}

//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos =pos;
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}

@Override
public int getCount() {
return categoryValues.length;
}


@Override
public Object getItem(int possition) {
return null;
}

@Override
public long getItemId(int possition) {
return 0;
}

@Override
public View getView(int possition, View convertView, ViewGroup parent) {

final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

if (mNewTemplatePos==possition) {
convertView = getNewTemplate(inflater, possition);
//use tag to indicate the type of the template
convertView.setTag(NEW_TEMPLATE);
} else {
convertView = getNormalTemplate(inflater, possition);
convertView.setTag(NORMAL_TEMPLATE);
}
} else {
mTag = (String) convertView.getTag();
switch (mTag) {
case NORMAL_TEMPLATE:
//convertView is the normal template view but you need a new template view in possition
if (mNewTemplatePos==possition)
convertView = getNewTemplate(inflater, possition);

break;
case NEW_TEMPLATE:
//convertView is the new template view but you need a normal template view in possition
if (mNewTemplatePos!=possition)
convertView = getNormalTemplate(inflater, possition);
break;
}
}
return convertView;
}


private View getNormalTemplate(LayoutInflater inflater, int possition) {

final View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
ImageView categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;

}

private View getNewTemplate(LayoutInflater inflater, int possition) {

final View grid = inflater.inflate(R.layout.kategori_secenek_template, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
cName.setText(categoryValues[possition]);
Button btn_nesne_tani = (Button) grid.findViewById(R.id.btn_nesneleri_taniyalim);
Button btn_cumle_kur = (Button) grid.findViewById(R.id.btn_cumle_kuralim);


btn_nesne_tani.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"nesne",Toast.LENGTH_SHORT).show();
}
});

btn_cumle_kur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"cümle",Toast.LENGTH_SHORT).show();
}
});

return grid;
}


}

KategoriActivity.java

.....
final KategoriAdapter adapter = new KategoriAdapter(getApplicationContext(), mKategoriler, kategoriResimleri);
grid=(GridView)findViewById(R.id.gv_kategoriler);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.useNewTemplate(position);
Toast.makeText(getApplicationContext(), mKategoriler[position].toString(),Toast.LENGTH_SHORT).show();
}
});

}

Answer

I have rewrite your KategoriAdapter class:

public class KategoriAdapter extends BaseAdapter {

private Context mContext;
private final String[] categoryValues;
private final Bitmap[] pictures;

//indicate that positon in list are all use new template
private List<Integer> mNewTemplatePos;
public ImageView categoryPictures;

//indicate that this  is normal template view
private final String NORMAL_TEMPLATE = "NORMAL_TEMPLATE";

//indicate that this  is new template view
private final String NEW_TEMPLATE = "NEW_TEMPLATE";

public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
    this.mContext = context;
    this.categoryValues = categoryValues;
    this.pictures = pictures;
    this.mNewTemplatePos = new ArrayList<>();
}

//apply new template to positon
public void useNewTemplate(int pos) {
    mNewTemplatePos.add(pos);
    //notiy list that data has changed and the list will refresh ui itself.
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return categoryValues.length;
}


@Override
public Object getItem(int possition) {
    return null;
}

@Override
public long getItemId(int possition) {
    return 0;
}

@Override
public View getView(int possition, View convertView, ViewGroup parent) {
    final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        if (mNewTemplatePos.contains(possition)) {
            convertView = getNewTemplate(inflater, possition);
            //use tag to indicate the type of the template
            convertView.setTag(NEW_TEMPLATE);
        } else {
            convertView = getNormalTemplate(inflater, possition);
            convertView.setTag(NORMAL_TEMPLATE);
        }
    } else {
        switch ((String) convertView.getTag()) {
            case NORMAL_TEMPLATE:
                //convertView is the normal template view but you need a new template view in possition
                if (mNewTemplatePos.contains(possition))
                    convertView = getNewTemplate(inflater, possition);
                break;
            case NEW_TEMPLATE:
                //convertView is the new template view but you need a normal template view in possition
                if (!mNewTemplatePos.contains(possition))
                    convertView = getNormalTemplate(inflater, possition);
                break;
        }
    }

    return convertView;
}


private View getNormalTemplate(LayoutInflater inflater, int possition) {
    View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
    TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
    categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
    cName.setText(categoryValues[possition]);
    categoryPictures.setImageBitmap(pictures[possition]);
    return grid;
}

private View getNewTemplate(LayoutInflater inflater, int possition) {
    // TODO: 31/08/16 inflate you new template view layout here 
    return youNewTemplateView;
}

}

You should determine wether if current contentView is the right template type in getView() because contentView may be one of the new template in your list when it is not null.It is convenient to use tag to indicate the template type.

When to use useNewTemplate(position)?
Just apply the position that you need to use new template to useNewTemplate() and use it in your onItemClick() method.

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                useNewTemplate(position);
            }
        });
Comments