很抱歉这个长标题。
我有一个Android应用程序,显示带有5个TextView列的自定义listView。当用户单击一行时,我将布局更改为3个TextView和2个EditTexts。我有两个不同的布局文件。一切都工作正常,行布局正确更改,我可以单击EditText和输入值。但是,我希望2 EditText中的任何一个根据点击的内容自动获得焦点。我已经有了一个有效的代码。我的问题是,以编程方式请求requestFocus()
似乎阻止了我用新视图更改行布局的部分。
以下是更改我的行布局的代码,它在没有requestFocus()
行的情况下正常工作:
private void changeLayout(final View view){
//get views from old row layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
qtyIn = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyIn.setText(itemQty);
qtyIn.setFilters(new InputFilter[] {filter});
discIn = (EditText)child.findViewById(R.id.discInputSearchResult);
discIn.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
//request focus here
if(focusTarget == 1){
Log.d("hello", "focus target is 1 " );
qtyIn.setFocusable(true);
qtyIn.setFocusableInTouchMode(true);
qtyIn.requestFocus();
}
else if(focusTarget == 2){
Log.d("hello", "focus target is 2 " );
discIn.requestFocus();
}
Log.d("hello", "focus state qtyIn = " + qtyIn.isFocused());
Log.d("hello", "focus state discIn = " + discIn.isFocused());
}
有趣的是,Log显示了正确的值,它根据我的需要说明了正确的焦点状态。但是,ll_inflate.addView(child);
行根本不起作用!
有谁知道这里发生了什么?我真的很困惑,为什么布局没有改变,但在执行.addView()
行后行。另一个奇怪的事情是requestFocus();
如何阻止视图的变化。
非常感谢任何帮助。感谢。
答案 0 :(得分:0)
你这样做是错误的。您应该在列表中有两种类型的视图,一种用于显示数据,另一种用于编辑它。 首先设置视图类型计数
final int TYPE_EDIT = 0,TYPE_VIEW = 1; // EDIT 7/9/016 类型需要从0开始
public int getViewTypeCount (){
return 2;
}
public int getItemViewType (int position){
//return type as needed. simple if you add int for this in data model
}
在 getView
内public View getView (int position, View convertView, ViewGroup parent){
int type = getItemViewType ();
if(null==convertView){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type ==TYPE_EDIT){
convertView = inflater.inflate(R.layout.rowEditlayout, parent, false);
}else{
convertView = inflater.inflate(R.layout.rowlayout, parent, false);
}
}
}
点击编辑按钮上的列表数据更改并在适配器上调用 notifydatasetchanged