我正在使用两个列表视图的 list_A 和 list_B,其中列表项的数量相同,如图 1 所示,其中 list_B 是自定义数组适配器,每个项目作为列表(字符串)动态创建所需宽度的按钮。
public class LarrayAdapter extends ArrayAdapter<List<String>> {
private final Context context;
private List<List<String>> list;
public LarrayAdapter(Context context, List<List<String>> list) {
super(context, R.layout.hrowlayout, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
HorizontalScrollView sv = (HorizontalScrollView) inflater.inflate(
R.layout.hrowlayout, parent, false);
LinearLayout rl1 = (LinearLayout) sv.findViewById(R.id.ll);
List<String> name2 = list.get(position);
final Button[] b = new Button[name2.size()];
float x;
for (int i = 0; i < name2.size(); i++) {
b[i] = new Button(rl1.getContext());
b[i].setGravity(Gravity.CENTER);
b[i].setBackgroundResource(R.drawable.bakground_list);
b[i].setSingleLine(true);
String einfo = name2.get(i);
String[] einfosplit = einfo.split("-",2);
x = calwidth(Integer.parseInt(einfosplit[1]));
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams((int) x, 80);
params2.topMargin = 15;
b[i].setTextSize(18);
b[i].setTextColor(Color.WHITE);
b[i].setText(einfosplit[0]);
b[i].setLayoutParams(params2);
final int z = i;
b[i].setOnFocusChangeListener(new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if(hasFocus){
b[z].setTextColor(0xff7BCFFC);
}
else
b[z].setTextColor(Color.WHITE);
}
} );
rl1.addView(b[i],params2);
}
sv.scrollTo(0, 0);
return sv;
}
public int calwidth(int dur){
return (253/3)*dur;
}
}
面临的问题:当从 list_A 导航到 list_B 时,我想将 list_B 的焦点设置到 list_A 项目的相同位置,为此我正在使用
list_B.setSelection(list_A_cur);//list_A_cur gives position of list_A focused item
list_B.setFocusable(true);
list_B.requestFocus();
list_B item如图1所示正确获得焦点,但是item在一段时间后失去焦点并每次返回第一个item如图2所示,请帮助...谢谢,
图1
图 2