如何在android中的选定网格视图项目上加载新模板 [英] How to load a new template on a selected grid view item in android

查看:139
本文介绍了如何在android中的选定网格视图项目上加载新模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在我的项目中添加了一个gridview,并使用base adapter将一个模板加载到gridview的每个项目。但我想要的是,当我点击一个gridview项目时,我想加载一个新的模板(布局)到选定的项目。



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



Base Adapter

  public class KategoriAdapter extends BaseAdapter {

private Context mContext;
private String [] categoryValues;
私人位图[]图片;



//表示新模板的位置
private int mNewTemplatePos = -1;

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

//应用新模板来定位
public void useNewTemplate(int pos){
mNewTemplatePos = pos;
// notiy列出数据已更改,并且列表将刷新ui本身。
notifyDataSetChanged();
}

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


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

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

$ b @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);
}

}
返回convertView;


$ b $ private查看getNormalTemplate(LayoutInflater inflater,int possition){

final查看grid = inflater.inflate(R.layout.kategoriler_list_item,空值);
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]);
返回网格;


$ b $ private查看getNewTemplate(LayoutInflater inflater,int possition){

final查看grid = inflater.inflate(R.layout.kategori_secenek_template,空值);
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();
}
});

返回网格;
}

}

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(){
$ b $ @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类:

  public class KategoriAdapter extends BaseAdapter {

private Context mContext;
private final String [] categoryValues;
私人最终位图[]图片;

//表示列表中的位置全部使用新模板
private List< Integer> mNewTemplatePos;
public ImageView categoryPictures;

//表示这是正常的模板视图
private final String NORMAL_TEMPLATE =NORMAL_TEMPLATE;

//表示这是新的模板视图
private final String NEW_TEMPLATE =NEW_TEMPLATE;

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

//应用新模板来定位
public void useNewTemplate(int pos){
mNewTemplatePos.add(pos);
// notiy列出数据已更改,并且列表将刷新ui本身。
notifyDataSetChanged();
}

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


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

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

$ b @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);
//使用标签来指示模板的类型
convertView.setTag(NEW_TEMPLATE);
} else {
convertView = getNormalTemplate(inflater,possition);
convertView.setTag(NORMAL_TEMPLATE);

}其他{
convert((String)convertView.getTag()){
case NORMAL_TEMPLATE:
// convertView是普通模板视图,但您需要如果(mNewTemplatePos.contains(possition))
convertView = getNewTemplate(inflater,possition);
休息;
case NEW_TEMPLATE:
// convertView是新的模板视图,但你需要一个正常的模板视图,位于
if(!mNewTemplatePos.contains(possition))
convertView = getNormalTemplate(充气器,possition);
休息;
}
}

返回convertView;


$ b $ private查看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]);
返回网格;
}

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

}

如果当前contentView是getView()中正确的模板类型,因为contentView可能是列表中新模板中的一个,如果它不为null,则可以使用标记来指示模板类型。



何时使用useNewTemplate(position)?

只需将你需要使用新模板的位置应用于useNewTemplate(),并将它用于你的onItemClick()方法。

  grid.setOnItemClickListener(new AdapterView.OnItemClickListener(){
$ b $ @Override
public void onItemClick(AdapterView< <> parent,View view,int position,long id){
useNewTemplate(position);
}
});


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.

THE PROBLEM WAS SOLVED, followings are the edited codes

Base Adapter

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

}

解决方案

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

这篇关于如何在android中的选定网格视图项目上加载新模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆