我正在使用listView,它将包含包含自定义微调器的项目。我想要做的目标是在每个微调器中有一个条目,当选择时,微调器本身将消失,并且将出现编辑文本。
在我提供的示例中,我在微调器中创建了一个名为“Create New”的条目,选中后,会出现一个编辑文本,允许您输入新名称。
我看到的问题是当选择“新建”时,微调器将更改为visibility.GONE并显示编辑文本。但是你永远不能选择编辑文本。我相信getView会不断被调用,并且可能搞砸了我想要完成的任务。
任何提示都会很棒!
Activity.java
public class Activity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
List<String> categories = new ArrayList<>();
categories.add("Groceries");
categories.add("Gas");
categories.add("Travel");
List<String> entries = new ArrayList<>();
entries.add("first");
entries.add("second");
entries.add("third");
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new ListAdapter(entries, categories));
}
private class ListAdapter extends ArrayAdapter<String> {
SpinnerAdapter spinnerAdapter;
public ListAdapter(List<String> entries, List<String> categories) {
super(Activity.this, 0, entries);
spinnerAdapter = new SpinnerAdapter(categories);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
final Spinner spinner = (Spinner) convertView.findViewById(R.id.spinner);
final EditText categoryEditText = (EditText) convertView.findViewById(R.id.edit_text);
spinner.setAdapter(spinnerAdapter);
spinner.setSelection(spinnerAdapter.getCount());
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if the create item was selected, hide the spinner and show the edit text
if (position == spinnerAdapter.getCount()-1) {
spinner.setVisibility(View.GONE);
categoryEditText.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return convertView;
}
private class SpinnerAdapter extends ArrayAdapter<String> {
public SpinnerAdapter(List<String> categories) {
super(Activity.this, android.R.layout.simple_spinner_dropdown_item, categories);
categories.add("Create New");
categories.add("Choose a Category");
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return super.getCount() - 1; // last item is a place holder
}
}
}
}
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)
我找到了解决问题的方法。事实证明,真正的问题是在ListView中有一个Edit Text。我必须在我的Manifest中的Activity定义中设置这个标志,一切正常!
<activity android:name= ".Activity" android:windowSoftInputMode="adjustPan"/>
也许有人想提供更多澄清?如果是这样,我会将你的答案标记为正确答案。