我想知道为什么 ListView 的 layout_height="wrap_content"会弄乱列表末尾的 Spinners。我在下面尝试了不同的修复方法。我希望有人可以解释这种行为,或者指出我在绘制 View /ui 事件方面缺乏哪些 android 知识。
1)目测可以看出问题here .
2) 改变 ListItem 属性后
android:descendantFocusability="afterDescendants"
我的行为有所改善,但仍有问题。看起来列表中的项目似乎没有收到事件,所以属性更改对我来说很有意义。 Here is a video更新该属性后微调器的行为方式。 一切正常,除非我实际选择了一个项目。
3) 在设置 ListView 的 layout_height="match_parent"之后,问题似乎在选择一个项目后消失了。 See here对于那个视频。
Activity :
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);
}
}
Activity 布局:
<LinearLayout 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: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="http://schemas.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="http://schemas.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>
最佳答案
您面临的问题是 Spinner 的基本行为,因此需要修改 Spinner。这是微调器的代码,其初始即。默认情况下,可视化是“选择项目”(如果在 .xml 中声明的提示类似于 android:prompt="@string/Select Item" )并且下拉 View 与原始微调器的大小相同。这种修改后的微调器的局限性在于,如果项目为空,它不会显示提示。
创建一个名为 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, View convertView, 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"
/>
Activity :像这样将 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 - 选择项目后,微调器下拉列表在 ListView 中绘制屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318464/
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基
相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or
我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模