Java 如何在android中的选定网格视图项上加载新模板

Java 如何在android中的选定网格视图项上加载新模板,java,android,templates,gridview,Java,Android,Templates,Gridview,我是android的新手。我想加载一个新模板,其中包含网格视图对象的选定项上的两个按钮。 那有可能吗 我向我的项目中添加了一个gridview,并使用基本适配器将一个模板加载到gridview的每个项目中。但我想要的是,当我单击gridview的一个项目时,我想将一个新模板(布局)加载到所选项目 问题已解决,以下是编辑后的代码 底座适配器 public class KategoriAdapter extends BaseAdapter{ private Context mContext; pr

我是android的新手。我想加载一个新模板,其中包含网格视图对象的选定项上的两个按钮。 那有可能吗

我向我的项目中添加了一个gridview,并使用基本适配器将一个模板加载到gridview的每个项目中。但我想要的是,当我单击gridview的一个项目时,我想将一个新模板(布局)加载到所选项目

问题已解决,以下是编辑后的代码

底座适配器

public class KategoriAdapter extends BaseAdapter{

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



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

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);

    int posId = mNewTemplatePos;

    if (convertView == null){
        if (mNewTemplatePos ==possition){
            convertView = getNewTemplate(inflater,possition);
        }else {
            convertView = getNormalTemplate(inflater,possition);
        }
    }else {
        if (posId==possition){
            convertView = getNewTemplate(inflater,possition);
        }else{
            convertView = getNormalTemplate(inflater,possition);
        }

    }
    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();
        }
    });

}
。。。。。
最终KategoriAdapter适配器=新的KategoriAdapter(getApplicationContext(),mKategoriler,kategoriResimleri);
grid=(GridView)findViewById(R.id.gv_kategoriler);
设置适配器(适配器);
grid.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
适配器。使用新模板(位置);
Toast.makeText(getApplicationContext(),mKategoriler[position].toString(),Toast.LENGTH_SHORT).show();
}
});
}

我已经重写了你的KategoriAdapter类:

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;
}

}
公共类KategoriAdapter扩展BaseAdapter{
私有上下文;
私有最终字符串[]类别值;
私人最终位图[]图片;
//指示列表中的位置都使用新模板
私有列表mNewTemplatePos;
公共图像视图类别结构;
//指示这是正常的模板视图
私有最终字符串NORMAL\u TEMPLATE=“NORMAL\u TEMPLATE”;
//指示这是新的模板视图
私有最终字符串NEW\u TEMPLATE=“NEW\u TEMPLATE”;
公共KategoriAdapter(上下文上下文、字符串[]类别值、位图[]图片){
this.mContext=上下文;
this.categoryValues=categoryValues;
这个。图片=图片;
this.mNewTemplatePos=新的ArrayList();
}
//将新模板应用于positon
公共空间使用新模板(int pos){
mNewTemplatePos.add(pos);
//不要列出数据已更改,列表将刷新ui本身。
notifyDataSetChanged();
}
@凌驾
public int getCount(){
返回categoryvalue.length;
}
@凌驾
公共对象getItem(int position){
返回null;
}
@凌驾
公共长getItemId(int position){
返回0;
}
@凌驾
公共视图getView(int position、视图convertView、视图组父级){
final LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
if(convertView==null){
如果(mNewTemplatePos.contains(position)){
convertView=getNewTemplate(充气机,位置);
//使用标记指示模板的类型
setTag(新的_模板);
}否则{
convertView=getNormalTemplate(充气机,位置);
setTag(普通模板);
}
}否则{
开关((字符串)convertView.getTag()){
案例标准模板:
//convertView是正常的模板视图,但您需要在Position中使用新的模板视图
如果(mNewTemplatePos.contains(position))
convertView=getNewTemplate(充气机,位置);
打破
案例新模板:
//convertView是新的模板视图,但您需要Position中的普通模板视图
如果(!mNewTemplatePos.包含(position))
convertView=getNormalTemplate(充气机,位置);
打破
}
}
返回视图;
}
私有视图getNormalTemplate(布局更平坦充气器,int position){
视图网格=充气机。充气(R.layout.kategoriler\u列表项,空);
TextView cName=(TextView)grid.findViewById(R.id.grid\u item\u ad);
categoryPictures=(ImageView)grid.findViewById(R.id.grid\u item\u resim);
cName.setText(类别值[position]);
setImageBitmap(图片[position]);
返回网格;
}
私有视图getNewTemplate(布局更平坦的充气机,内部存储){
//TODO:31/08/16在此处为新模板视图布局充气
返回NewTemplateView;
}
}
您应该确定当前contentView是否是getView()中的正确模板类型,因为当它不为null时,contentView可能是列表中的新模板之一。使用标记指示模板类型很方便

何时使用新模板(位置)?
只需应用使用new template()所需的位置,并在onItemClick()方法中使用它

grid.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
使用新模板(位置);
}
});

我已经重写了你的KategoriAdapter类:

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;
}

}
公共类KategoriAdapter扩展BaseAdapter{
私有上下文;
私有最终字符串[]类别值;
私人最终位图[]图片;
//指示列表中的位置都使用新模板
私有列表mNewTemplatePos;
公共图像视图类别结构;
//指示这是正常的模板视图
私有最终字符串NORMAL\u TEMPLATE=“NORMAL\u TEMPLATE”;
//指示这是新的模板视图
私有最终字符串NEW\u TEMPLATE=“NEW\u TEMPLATE”;
公共KategoriAdapter(上下文上下文、字符串[]类别值、位图[]图片){
this.mContext=上下文;
this.categoryValues=categoryValues;
这个。图片=图片;
this.mNewTemplatePos=新的ArrayList();
}
//将新模板应用于positon
公共空间使用新模板(int pos){
mNewTemplatePos.add(pos);
//不要列出数据已更改,列表将刷新ui本身。
notifyDataSetChanged();
}
@凌驾
public int getCount(){
返回categoryvalue.length;
}
@凌驾
公共对象getItem(int position){
返回null;
}
@凌驾
公共长线