2016-05-09 11 views
-1

グリッドアイテムのテキストの色を選択し、1つのアイテムのみを選択する必要があります。これは既に行われていますが、グリッドビューにアイテムが多数ある場合、アイテムを選択して、グリッド内のランダムな項目を選択し、複数の項目を選択することもできます。 私は多くの回答を試みましたが、単一の解決策は見つかりませんでした。 誰でも何か考えがありますか?Gridviewがスクロールするとそのビューが変更される

私はリンクの下で与えられた答えを試みたが、それは

When the GridView scrolls, it changes its view's activated status

私の問題を解決していなかった私は、アダプタgetViewメソッドメソッドとグリッドビュー

のonItemSelected()メソッドで自分のコードを共有します
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long arg3) { 

       view.setSelected(true); 
       TextView tv = (TextView) view;     // Get the current selected view as a TextView 
       tv.setTextColor(Color.parseColor("#50d1e4")); // Set the current selected item text color 
       TextView previousSelectedView = (TextView) gridview.getChildAt(previousPosition); // Get the last selected View from GridView 

       // If there is a previous selected view exists 
       if (previousPosition != -1 && previousPosition!=position) { 
        previousSelectedView.setSelected(false);      // Set the last selected View to deselect 
        previousSelectedView.setTextColor(Color.parseColor("#162750")); // Set the last selected View text color as deselected item 
       } 
       previousPosition = position; 

     } 
    }); 

ここでは、previousPositionの初期値= -1(int型)

アダプタの

GetViewメソッドが

public static class ViewHolder 
{ 
    public TextView txt_time_slot; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder view; 
    final Context context = parent.getContext(); 
    if (inflater == null) 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     view = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.grid_item, null); 
     view.txt_time_slot = (TextView) convertView.findViewById(R.id.txt_item_time_slot); 
     convertView.setTag(view); 
     convertView.setId(0); 
    } else { 
     view = (ViewHolder) convertView.getTag(); 
    } 

     if (listValues.get(position) != null) 
     { 
      view.txt_time_slot.setText(listValues.get(position)); 

     } 

    return convertView; 
} 

以下に示す。ここでgrid_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/txt_item_time_slot" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="10:20 AM" 
     android:gravity="center_horizontal" 
     android:padding="3dp" 
     android:textColor="#162750" 
     android:textStyle="bold" 
     android:textSize="30sp"/> 
+0

は、実際にそれらは、それらが "選択" されている項目を再利用しているランダムなアイテム(あなたがするsetSelectedと呼ばれる)ではありません。あなたのアダプタで選択/選択解除状態を処理する必要があります。 – danypata

+0

ありがとう@danypata私は今しようとする –

答えて

0

previousPosition変数を追加する必要はありませんです。 モデルに1つのパラメータを追加します。任意のモデルのリストがあり、パラメータisSelected(ブール値)を1つ追加します。ユーザーがそれをクリックすると、これをtrueに設定します。

アダプタのgetViewメソッドでは、itemのisSelected値をチェックします。リストから取得できます(listValue.get(position).isSelected())。戻り値に基づいて、背景を変更することができます。また、選択された価値を得るのにも役立ちます。 EX用

 Product product = mProcucts.get(position); 
    itemViewHolder.textView.setText(product.getProductName()); 
    if(product.isSelected()){ itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_checked)); 
    }else{ 
     itemViewHolder.textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_button_unchecked)); 
    } 
+0

@ Shivani .....それは私のために働いていない –

関連する問題