大佬教程收集整理的这篇文章主要介绍了android – Spinner下拉列表在选择项目后在listview中绘制屏幕顶部,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
1)在视觉上可以看到问题here.
2)更改ListItem属性后
android:descendantFocusability="afterDescendants"
我的行为更好,但仍有一些事情发生.看起来列表中的项目似乎没有收到事件,因此属性更改对我来说很有意义.
Here is a video更新该属性后微调器的行为.
一切正常,除非我实际选择了一个项目.
3)设置ListView的layout_height =“match_parent”后,问题似乎在选择项目后消失. See here为该视频.
活动:
public class SELEctOractivity extends Activity { public static final String TAG = SELEctOractivity.class.getSimplename(); @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); Log.v(tag,"onCreate"); setContentView(R.layout.activity_SELEctor); LayoutInflater inflater = (LayoutInflater) getSystemservice(Context.LAYOUT_INFLATER_serviCE); ListView contents = (ListView) findViewById(R.id.list_view); contents.addHeaderView(new TestView(this)); contents.addFooterView(new View(this)); SimpleBaseAdapter listadapter = new SimpleBaseAdapter(this); // LOW RANGE LinearLayout lowRange = (LinearLayout) inflater.inflate(R.layout.list_item_edit,null); TextView lowRangeText = (TextView) lowRange.findViewById(R.id.text); EditText lowRangeEditText = (EditText) lowRange.findViewById(android.R.id.edit); // HIGH RANGE LinearLayout highRange = (LinearLayout) inflater.inflate(R.layout.list_item_edit,null); TextView highRangeText = (TextView) highRange.findViewById(R.id.text); EditText highRangeEditText = (EditText) highRange.findViewById(android.R.id.edit); // UNITS LinearLayout units = (LinearLayout) inflater.inflate(R.layout.list_item_units,null); TextView unitsText = (TextView) units.findViewById(android.R.id.text1); // SPINNERS LinearLayout spinners = (LinearLayout) inflater.inflate(R.layout.list_item_spinners,null); Spinner spinner1 = (Spinner) spinners.findViewById(R.id.spinner1); Spinner spinner2 = (Spinner) spinners.findViewById(R.id.spinner2); Spinner spinner3 = (Spinner) spinners.findViewById(R.id.spinner3); DebugAdapterViewListeners.set(spinner1,"spinner1"); // VIEW SETUP lowRangeText.setText("text1"); highRangeText.setText("text2"); unitsText.setText("text3"); // SPINNER SETUP String[] massUnits1 = new String[]{"one","two"}; String[] massUnits2 = new String[]{"three","four"}; String[] timeUnits = new String[]{"five","six"}; ArrayAdapter<String> adapt1 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); ArrayAdapter<String> adapt2 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); ArrayAdapter<String> adapt3 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); adapt1.addAll(massUnits1); adapt2.addAll(massUnits2); adapt3.addAll(timeUnits); spinner1.setAdapter(adapt1); spinner2.setAdapter(adapt2); spinner3.setAdapter(adapt3); listadapter.addView(lowRangE); listadapter.addView(highRangE); listadapter.addView(units); listadapter.addView(spinners); contents.setAdapter(listadapter); } @Override public Boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.SELEctor,menu); return false; } }
这是SimpleBaseAdapter类:
public class SimpleBaseAdapter extends BaseAdapter { private ArrayList<View> views; private Context context; public SimpleBaseAdapter(Context context) { this.context = context; this.views = new ArrayList<View>(); } public void addView(View view) { this.views.add(view); } @Override public int getCount() { return views.size(); } @Override public Object getItem(int position) { View view = views.get(position); if (view instanceof AbsListView) { return ((AbsListView)view).getItemAtPosition(position); } else if (view instanceof AbsSpinner) { return ((AbsSpinner)view).getItemAtPosition(position); } else { return null; } } @Override public long getItemId(int position) { View view = views.get(position); if (view instanceof AbsListView) { return ((AbsListView)view).getItemIdAtPosition(position); } else if (view instanceof AbsSpinner) { return ((AbsSpinner)view).getItemIdAtPosition(position); } else { return 0; } } @Override public View getView(int position,View convertView,ViewGroup parent) { return views.get(position); } }
活动布局:
<LinearLayout xmlns:android="@R_616_10107@://scheR_551_11845@as.android.com/apk/res/android" xmlns:tools="@R_616_10107@://scheR_551_11845@as.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:BACkground="@color/green_1" android:orientation="vertical" > <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:headerDividersEnabled="true" android:footerDividersEnabled="true" android:dividerHeight="0.5sp" android:divider="@color/black" android:cliPTOPadding="false" android:layout_marginTop="18sp" android:layout_marginBottom="18sp" /> </LinearLayout>
编辑列表项布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="@R_616_10107@://scheR_551_11845@as.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="@dimen/row_padding" android:BACkground="@android:color/white" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:layout_weight="50" android:gravity="top" android:textSize="@dimen/font_size_standard" android:textColor="@drawable/SELEctor_row_item_detail_text" /> <EditText android:layout_width="0dip" android:layout_height="wrap_content" android:id="@android:id/edit" android:layout_weight="50" android:inputType="number" android:gravity="right" /> </LinearLayout>
微调器行项目布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="@R_616_10107@://scheR_551_11845@as.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/spinner_container" android:orientation="horizontal" android:BACkground="@android:color/white" android:paddingTop="@dimen/header_row_padding_vertical" > <Spinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner1" android:gravity="center" android:spinnerMode="dropdown" /> <Spinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner2" android:gravity="center" android:spinnerMode="dialog" /> <Spinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner3" android:gravity="center" android:spinnerMode="dialog" /> </LinearLayout>
创建一个名为NoDefaultSpinner.java的新类&在该副本中粘贴此代码
public class NoDefaultSpinner extends Spinner { public NoDefaultSpinner(Context context) { super(context); } public NoDefaultSpinner(Context context,AttributeSet attrs) { super(context,attrs); } public NoDefaultSpinner(Context context,AttributeSet attrs,int defStylE) { super(context,attrs,defStylE); } @Override public void setAdapter(SpinnerAdapter orig ) { final SpinnerAdapter adapter = newProxy(orig); super.setAdapter(adapter); try { final Method m = AdapterView.class.getDeclaredMethod( "setNextSELEctedPositionInt",int.class); m.setAccessible(true); m.invoke(this,-1); final Method n = AdapterView.class.getDeclaredMethod( "setSELEctedPositionInt",int.class); n.setAccessible(true); n.invoke(this,-1); } catch( Exception e ) { throw new RuntimeException(E); } } protected SpinnerAdapter newProxy(SpinnerAdapter obj) { return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance( obj.getClass().getClassLoader(),new Class[]{SpinnerAdapter.class},new SpinnerAdapterProxy(obj)); } /** * Intercepts getView() to display the prompt if position < 0 */ protected class SpinnerAdapterProxy implements InvocationHandler { protected SpinnerAdapter obj; protected Method getView; protected SpinnerAdapterProxy(SpinnerAdapter obj) { this.obj = obj; try { this.getView = SpinnerAdapter.class.getmethod( "getView",int.class,View.class,ViewGroup.class); } catch( Exception e ) { throw new RuntimeException(E); } } public Object invoke(Object proxy,Method m,Object[] args) throws Throwable { try { return m.equals(getView) && (Integer)(args[0])<0 ? getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) : m.invoke(obj,args); } catch (InvocationTargetException E) { throw e.getTargetException(); } catch (Exception E) { throw new RuntimeException(E); } } protected View getView(int position,ViewGroup parent) throws illegalaccessexception { if( position<0 ) { final TextView v = (TextView) ((LayoutInflater)getContext().getSystemservice( Context.LAYOUT_INFLATER_serviCE)).inflate( android.R.layout.simple_spinner_item,parent,falsE); v.setText(getPrompt()); return v; } return obj.getView(position,convertView,parent); } } }
在微调器行项目布局中,将微调器的类型更改为< com.example.appname.NoDefaultSpinner,如下所示
<com.example.appname.NoDefaultSpinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner1" android:gravity="center" android:spinnerMode="dropdown" /> <com.example.appname.NoDefaultSpinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner2" android:gravity="center" android:spinnerMode="dialog" /> <com.example.appname.NoDefaultSpinner android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="33" android:id="@+id/spinner3" android:gravity="center" android:spinnerMode="dialog" />
活动:像这样将Spinner的类型更改为NoDefaultSpinner
public class SELEctOractivity extends Activity { public static final String TAG = SELEctOractivity.class.getSimplename(); @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); Log.v(tag,"onCreate"); setContentView(R.layout.activity_SELEctor); LayoutInflater inflater = (LayoutInflater)getSystemservice(Context.LAYOUT_INFLATER_serviCE); ListView contents = (ListView) findViewById(R.id.list_view); contents.addHeaderView(new TestView(this)); contents.addFooterView(new View(this)); SimpleBaseAdapter listadapter = new SimpleBaseAdapter(this); // LOW RANGE LinearLayout lowRange = (LinearLayout) inflater.inflate(R.layout.list_item_edit,null); TextView lowRangeText = (TextView) lowRange.findViewById(R.id.text); EditText lowRangeEditText = (EditText) lowRange.findViewById(android.R.id.edit); // HIGH RANGE LinearLayout highRange = (LinearLayout) inflater.inflate(R.layout.list_item_edit,null); TextView highRangeText = (TextView) highRange.findViewById(R.id.text); EditText highRangeEditText = (EditText) highRange.findViewById(android.R.id.edit); // UNITS LinearLayout units = (LinearLayout) inflater.inflate(R.layout.list_item_units,null); TextView unitsText = (TextView) units.findViewById(android.R.id.text1); // SPINNERS LinearLayout spinners = (LinearLayout) inflater.inflate(R.layout.list_item_spinners,null); NoDefaultSpinner spinner1 = (NoDefaultSpinner) spinners.findViewById(R.id.spinner1); NoDefaultSpinner spinner2 = (NoDefaultSpinner) spinners.findViewById(R.id.spinner2); NoDefaultSpinner spinner3 = (NoDefaultSpinner) spinners.findViewById(R.id.spinner3); DebugAdapterViewListeners.set(spinner1,"spinner1"); // VIEW SETUP lowRangeText.setText("text1"); highRangeText.setText("text2"); unitsText.setText("text3"); // SPINNER SETUP String[] massUnits1 = new String[]{"one","two"}; String[] massUnits2 = new String[]{"three","four"}; String[] timeUnits = new String[]{"five","six"}; ArrayAdapter<String> adapt1 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); ArrayAdapter<String> adapt2 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); ArrayAdapter<String> adapt3 = new ArrayAdapter<String>(this,R.layout.spinner_list_item_centered); adapt1.addAll(massUnits1); adapt2.addAll(massUnits2); adapt3.addAll(timeUnits); spinner1.setAdapter(adapt1); spinner2.setAdapter(adapt2); spinner3.setAdapter(adapt3); listadapter.addView(lowRangE); listadapter.addView(highRangE); listadapter.addView(units); listadapter.addView(spinners); contents.setAdapter(listadapter); } @Override public Boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.SELEctor,menu); return false; }
此解决方案依赖于反射来调用AdapterView.setNextSELEctedPositionInt()和AdapterView.setSELEctedPositionInt(),&在API 4上成功运行到API 19.
以上是大佬教程为你收集整理的android – Spinner下拉列表在选择项目后在listview中绘制屏幕顶部全部内容,希望文章能够帮你解决android – Spinner下拉列表在选择项目后在listview中绘制屏幕顶部所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。