2016-09-01 95 views
0

我創建了一個包含gridview的新佈局。當應用程序運行時,gridview正在獲得預期的內容。如何刷新其他項目時點擊一個特定項目的gridview android

gridview的每個單元格都有一個正常的特定模板。當點擊一個單元格時,一個新的模板被加載到這個單元格中。 With the help of @wanglugao, every thing is working fine so far

現在我想刷新正常模板的未點擊的項目,當我點擊一個項目。只有被點擊的項目需要包含新模板。但是,當我點擊一個項目,即使我點擊了其他項目,它仍然與新模板。

圖像鏈接說明事先我Activity.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(); 
     } 
    }); 

感謝我的current status

這是我的BaseAdapter,

public class KategoriAdapter extends BaseAdapter{ 

private Context mContext; 
private String[] categoryValues; 
private Bitmap[] pictures; 
private String mTag = "NORMAL_TEMPLATE"; 


//indicate that position using 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); 
      mTag = (String) convertView.getTag(); 
     } 


    } else { 
     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; 
      default: 
       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; 
} 

}

及相關部分。

回答

0

問題是您使用mTag。你使用這個,只有當你的轉換視圖不是空的時候才能確定使用哪個模板,但是你只在你膨脹一個新的轉換視圖時才設置它。

mTag不應該是一個實例字段。它應該是您(通常)在您的getView()方法中設置的本地。所以,像這樣,

Tag tag = mNewTemplatePos==possition ? NEW : NORMAL; 
switch (tag) { 
    // inflate base on the tag 
} 

此外,您沒有正確使用您的轉換視圖;你在某些情況下扔掉它。如果你的網格中有兩個非常不同的單元格,你應該使用視圖類型(Google它)或有一個佈局,並通過隱藏或顯示部分來改變它。

+0

謝謝@Jeffrey Blattman。您的評論幫助我解決了我的問題。 –

+0

那些誰需要我的代碼的最終版本可以看看[鏈接](http://stackoverflow.com/questions/39238402/how-to-load-a-new-template-on-a-selected-grid-視圖項功能於機器人) –

相關問題