2016-06-12 2 views
3

getDropDownView()がオーバーライドされたカスタムアダプタを使用するSpinnerがあります。カスタムドロップダウンビューの各項目は、TextViewとボタンで構成されています。カスタムドロップダウンビューのSpinnerがonItemSelected()を起動しない

私のコードを実行すると、スピナーのドロップダウンアイテムは表示されますが、クリックすると何も表示されません。スピナーのドロップダウンは開いたままで、spinner.onItemSelected()はトリガーされません。

drop_down_item.xml

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/dropdown_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:singleLine="true" /> 
    <Button 
     android:id="@+id/dropdown_button" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Remove"/> 
</RelativeLayout> 

カスタムアダプターコード

public View getDropDownView(final int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.drop_down_item, parent, false); 

    TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text); 
    textView.setText(mValues.get(position));   
    Button buttonView = (Button) rowView.findViewById(R.id.dropdown_button)); 

    return rowView; 
} 

私はこのコードで私のスピナーとアダプタを作成します。

spinner = (Spinner) findViewById(R.id.my_spinner); 
MyAdapter adapter = new MyAdapter(getViewContext(), R.layout.spinner_item, values); 
adapter.setDropDownViewResource(R.layout.drop_down_item); 
spinner.setAdapter(adapter); 
... 
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     // Do something here - but this never runs 
    } 
}); 

だから私はしないでくださいなぜonItemなのか知っているSelected()は呼び出されなくなりましたか?

ドロップダウンTextViewにクリックリスナーを配置する必要があるのでしょうか?spinner.setSelection(pos)を使用してonItemSelected()をトリガーする必要がありますか?

答えて

1

解決策は、TextViewとButtonの両方のレイアウトにandroid:focusable = "false"を設定することです。

はまた、コードでこれを行うことができます。

textView.setFocusable(false); 
buttonView.setFocusable(false); 

は答えhereを発見しました。これはSpinnerの実装では、フォーカス可能な項目がビュー内に1つしかないためです。だから私はそのアイテムを選択できませんでした。

+0

getDropDownViewでOnClickListenerを定義していますか?動作していますか?私の場合、getDropDownViewにOnClickListenerを追加すると、クリックした後にスピナービューが閉じることはありません。 – Kenji

2

イベントは基本的に、SpinnerのドロップダウンビューのLinearLayoutをクリックしてコールバックを受け取るためにアクティビティが実装するインターフェイスです。

public class MyArrayAdapter extends BaseAdapter { 

String[] values; 
int CustomResource; 
Context context; 
Events events; 

public MyArrayAdapter(Context baseContext, int customspinnerview, 
     String[] stringArray, Events events) { 
    values = stringArray; 
    context = baseContext; 
    this.events = events; 
    CustomResource = customspinnerview; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return values.length; 
} 

@Override 
public Object getItem(int position) { 
    if (position < values.length) 
     return values[position]; 
    else { 
     return null; 
    } 
} 


@Override 
public View getView(final int position, final View convertView, 
     ViewGroup parent) { 
    View rowView = convertView; 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (rowView == null) { 
     rowView = inflater.inflate(CustomResource, parent, false); 
    } 
    TextView textView = (TextView) rowView.findViewById(R.id.dropdown_text); 
    textView.setText(values[position]); 
    Button button = (Button) rowView.findViewById(R.id.Button_text); 
    return rowView; 
} 

@Override 
public View getDropDownView(final int position, View convertView, 
     ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = convertView;`enter code here` 
    if (rowView == null) { 
     rowView = inflater.inflate(CustomResource, parent, false); 

    } 
    final LinearLayout parentRelative = (LinearLayout) rowView 
      .findViewById(R.id.parent); 
    final TextView textView = (TextView) rowView 
      .findViewById(R.id.dropdown_text); 
    textView.setText(values[position]); 
    Button button = (Button) rowView.findViewById(R.id.Button_text); 
    rowView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      events.onItemSelectedLister(
        (AdapterView<?>) parentRelative.getParent(), 
        parentRelative, position, (long) 0); 

     } 
    }); 
    // Button buttonView = (Button) 
    // rowView.findViewById(R.id.dropdown_button); 

    return rowView; 
} 

イベントIntefaceアクティビティがアダプタからコールバックを受け取るために実装するインターフェイスです。

import android.view.View; 
import android.widget.AdapterView; 

public interface Events { 

public void onItemSelectedLister(AdapterView<?> parent, View view, 
     int position, long id); 
} 

アクティビティの実装。

onItemSelected実装では、setContentView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.customspinner.MainActivity" > 

<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</Spinner> 

</RelativeLayout> 

レイアウトファイルとしてアダプターに渡されたスピナービューのためのあなたの仕事.....

import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Spinner; 
import com.example.adapter.MyArrayAdapter; 

public class MainActivity extends Activity implements Events { 
Spinner spinner; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    spinner = (Spinner) findViewById(R.id.spinner); 
    spinner.setAdapter(new MyArrayAdapter(getBaseContext(), 
      R.layout.customspinnerview, getResources().getStringArray(
        R.array.values), this)); 
} 

@Override 
public void onItemSelectedLister(AdapterView<?> parent, View view, 
     final int position, long id) { 
     //perform your Task....... 
    Method method; 
    try { 
     method = Spinner.class.getDeclaredMethod("onDetachedFromWindow"); 
     method.setAccessible(true); 
     try { 
      method.invoke(spinner); 
     } catch (IllegalAccessException | IllegalArgumentException 
       | InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } catch (NoSuchMethodException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    spinner.post(new Runnable() { 

     @Override 
     public void run() { 
      spinner.setSelection(position); 
      spinner.setSelected(true); 
      ((MyArrayAdapter) spinner.getAdapter()).notifyDataSetChanged(); 
     } 
    }); 

} 
} 

活動XMLファイルを行うことができる場所です。

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#99000000" 
android:id="@+id/parent" 
android:orientation="horizontal"> 
<TextView 
    android:id="@+id/dropdown_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<Button 
    android:id="@+id/Button_text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="remove"/> 

</LinearLayout> 

コードは完全に正常に動作します。コードは完全に動作しています。

+0

ありがとうShahroze。私はあなたのコードが動作していると確信していますが、非常にシンプルに見えるものを達成するのは非常に複雑ですね? – MickeyR

+0

あなたは大歓迎です。機能を実現するために動的アプローチが使用されています。 –

関連する問題