2016-09-01 9 views
0

グリッドビューを含む新しいレイアウトを作成しました。アプリケーションが実行されると、gridviewは予想されるコンテンツを取得しています。androidのgridviewの特定のアイテムをクリックすると他のアイテムをリフレッシュする方法

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の使用です。これを使用して、変換ビューがnullでないときに使用するテンプレートを決定しますが、新しい変換ビューを展開するときにのみ設定します。

mTagはインスタンスフィールドであってはなりません。あなたが(常に)getView()メソッドで設定したローカルでなければなりません。このように、

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

また、変換ビューを適切に使用していません。あなたはそれをいくつかのケースで捨てる。グリッドに2つの非常に異なるセルがある場合は、ビュータイプ(Googleのもの)を使用するか、レイアウトを1つ作成して、セクションを非表示または表示して変更する必要があります。

+0

ありがとう、@ジェフリーBlattman。あなたのご意見は私の問題を解決するのに役立ちました。 –

+0

私のコードの究極のバージョンを必要とする人は[リンク]を見ることができます(http://stackoverflow.com/questions/39238402/how-to-load-a-new-template-on-a-selected-grid-ビューアイテムインアンドロイド) –

関連する問題