2016-06-12 98 views
3

我有一個微調器,它使用getDropDownView()被覆蓋的自定義適配器。自定義下拉視圖中的每個項目都由TextView和Button組成。微調控制器自定義下拉視圖不觸發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上放置一個點擊偵聽器,然後再觸發onItemSelected(),也許使用spinner.setSelection(pos)?

回答

1

解決方法是在TextView和Button的佈局中設置android:focusable =「false」。

或者也可以做到這一點代碼:

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

找到了答案here。這是可行的,因爲Spinner實現只允許視圖中的一個可聚焦項目。這就是爲什麼我無法選擇該項目。

+0

你是否在getDropDownView中定義了OnClickListener,它是行得通的?在我的情況下,當我在getDropDownView中添加任何OnClickListener時,單擊後不會關閉微調視圖。 – Kenji

2

事件基本上是Activity實現的接口,通過單擊Spinner的DropDown視圖的LinearLayout來接收callBack。

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它是一個接口,Activity實現爲了從Adapter接收回調。

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

你歡迎。動態方法已被用於實現功能。 –