ListView中怎么才能让特定的ITEM获取焦点
2个回答
2016-01-25
展开全部
ListView默认情况
当item有焦点时,item上的button等子控件获取不到焦点;
当子控件有焦点时,item无焦点无法响应onItemClick事件
ViewGroup.FOCUS_AFTER_DESCENDANTS:表示item的子控件优先于item获得焦点;
ViewGroup.FOCUS_BEFORE_DESCENDANTS:表示item优先于其子控件获得焦点。
解决办法:
[java] view plaincopy
listView.setOnItemSelectedListener(onItemSelectedListener);
private AdapterView.OnItemSelectedListener onItemSelectedListener =
new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//当此选中的item的子控件需要获得焦点时
parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
//else parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
}
当item有焦点时,item上的button等子控件获取不到焦点;
当子控件有焦点时,item无焦点无法响应onItemClick事件
ViewGroup.FOCUS_AFTER_DESCENDANTS:表示item的子控件优先于item获得焦点;
ViewGroup.FOCUS_BEFORE_DESCENDANTS:表示item优先于其子控件获得焦点。
解决办法:
[java] view plaincopy
listView.setOnItemSelectedListener(onItemSelectedListener);
private AdapterView.OnItemSelectedListener onItemSelectedListener =
new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//当此选中的item的子控件需要获得焦点时
parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
//else parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
parent.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
}
展开全部
1.将ListView的Item Layout的子控件focusable属性设置为false
2.对Item Layout的根控件android:descendantFocusability="blocksDescendant"
例如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:background="#ffffffff"
android:descendantFocusability="blocksDescendant >
<LinearLayout
<RatingBar
android:id="@+id/rb_bookRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="2.0"
style="@style/RatingBar"
android:isIndicator="true"
/>
</LinearLayout>
<Button
android:id="@+id/btn_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:text="影讯"
style="@style/Button"
/>
</RelativeLayout>
2.对Item Layout的根控件android:descendantFocusability="blocksDescendant"
例如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:background="#ffffffff"
android:descendantFocusability="blocksDescendant >
<LinearLayout
<RatingBar
android:id="@+id/rb_bookRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="2.0"
style="@style/RatingBar"
android:isIndicator="true"
/>
</LinearLayout>
<Button
android:id="@+id/btn_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:text="影讯"
style="@style/Button"
/>
</RelativeLayout>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询