我正在使用两个列表视图的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项正确获得焦点,如图1所示,但是项目在一段时间后失去焦点并且每次都返回到第一项,如图2所示,请帮助...谢谢,
图1
图2