2017-01-12 15 views
1

私はスピナーを持っており、何もせずにユーザーがスピナーを終了したときに何かを表示/非表示したい。例えば、ユーザがスピナの外側の領域に接触したときである。 p.s.コンテナレイアウト(私の場合はLinearLayout)のonTouchEventも呼び出されません。ユーザーが項目getViewメソッド関数が呼び出されていないを選択せず​​に終了しますので、性別は(スピナーオブジェクトそのものである)ときユーザーがスピナーを終了したときに呼び出されるイベント

public SpinnerHintAdapter(Activity context, int resourceId, int textViewId, List<SpinnerItem> list, Spinner parent){ 

    super(context,resourceId,textViewId, list); 
    flater = context.getLayoutInflater(); 
    this.items = list; 
    this.gender = parent; 

} 

@Override 
public int getCount() { 
    return items.size(); 
} 

@Nullable 
@Override 
public SpinnerItem getItem(int position) { 
    return items.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    SpinnerItem spinnerItem = getItem(position); 
    View rowView = flater.inflate(R.layout.gender_item ,null,true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.main_text); 
    txtTitle.setText(spinnerItem.getName()); 
    txtTitle.setTextColor(txtTitle.getResources().getColor(R.color.color_white)); 
    Log.i(Tags.byEmail, "VVVVVVVVVVVVv");; 
    gender.setVisibility(View.VISIBLE); 
    return rowView; 
} 


@Override 
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    firstTime = false; 
    SpinnerItem rowItem = getItem(position); 
    View rowView = flater.inflate(R.layout.gender_drop_down_item ,null,true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.drop_down_text); 
    txtTitle.setText(rowItem.getName()); 
    if(!isEnabled(position)){ 
     txtTitle.setBackground(txtTitle.getResources().getDrawable(R.drawable.normal_rounded_text_field)); 
     txtTitle.setTextColor(Color.parseColor("#777777")); 
    } 
    parent.setBackground(parent.getResources().getDrawable(R.drawable.normal_rounded_text_field)); 
    txtTitle.setEnabled(isEnabled(position)); 
    gender.setVisibility(View.INVISIBLE); 
    Log.i(Tags.byEmail, "DDDDDDDDDDDDDDDDDD"); 
    return rowView; 
} 

@Override 
public boolean isEnabled(int position) { 
    if(position == 0) 
     return false; 
    return super.isEnabled(position); 
} 

}

ではありません。ここで は、カスタムspineerのための私の実装です可視。 OnItemSelectedを試してみましたが、OnNothingSelectedも呼び出されませんでした。 イベントOnTouchイベントは呼び出されません。

次のイベントが場合ユーザー出口呼ばれない: 1- 2- OnFocusChanged 3- OnNothingSelected OnItemSlected。 OnTouch

+0

'AdapterView.OnItemSelectedListener.onNothingSelected'? – Selvin

+0

提供されたコードが役立ちます。 – W4R10CK

+0

これを助けるかもしれないhttp://stackoverflow.com/a/1714426/3790150 – saeed

答えて

1

を使用しますか? Spinner

public class CustomSpinner extends Spinner { 

    ... Constructors ... 

    @Override 
    public void onWindowFocusChanged(boolean hasWindowFocus) { 
     super.onWindowFocusChanged(hasWindowFocus); 
     Log.d(CustomSpinner.class.getSimpleName(), "onWindowFocusChanged: " + hasWindowFocus); 
     if (hasWindowFocus) { 
      // User click out of window 
     } else { 
      // User click in spinner window 
     } 
    } 
} 
+0

これは私の最初の試みでした。誰かが項目を選択したときにのみ呼び出されます。何かを選択する前にユーザーが存在しています –

+0

@alnourahmed「何かを選択する前にユーザーが存在します」とはどういう意味ですか?捕まえる? –

+0

ユーザーがスピナーをクリックしてドロップダウンメニューが表示されてもユーザーがドロップダウンの外側をクリックしたときにonNothingSelectedが呼び出されると思います –

1

、4-ノー、そのイベントが魔法のようにあなたのために働くだろうと思うonFocusChangeListener

// Alternatively, set it in the XML 
mSpinner.setFocusable(true); 
mSpinner.setFocusableInTouchMode(true); 
mSpinner.setOnFocusChangeListener(new OnFocusChangeListener() { 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     // ... do your stuff 
     // Don't forget to call the super method. 
     super.onFocusChange(v,hasFocus); 
    } 

}); 
+0

それはまた、呼び出されていないだけでなく、それはスピナーを最初から選択することができません、私はクリックしなければならなかったそれは2回ドロップダウンリストを見るために –

+0

あなたはsuper.onFocusChangeを呼び出しましたか? – Jasper

+0

OnFocusChangeListenerはインターフェイスです。あなたはこれを電話していますか? –

0

次の2つのメソッドを実装しAdapterView.OnItemSelectedListener設定します。 onItemSelectedおよびonNothingSelected。このよう

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // This is called when the user selects an item in the Spinner 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
     // This is called when the user closes the spinner selecting nothing 
    } 
}); 
関連する問題