[英]Android ListView onItemClickListener being blocked by ProgressBar
我讓我的onItemClickListener在DrawerLayout內為ListView工作正常。 但是我添加了一個ProgressBar,該顯示在任何其他內容之前顯示,然后將其設置為View.GONE。 但是,我無法選擇,並且列表中的項目也無法查看。
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:focusable="false"
android:paddingBottom="25dp"
android:textColor="#ffffff"
android:textSize="20sp" >
</TextView>
</RelativeLayout>
main_activity.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#000"
android:choiceMode="singleChoice" />
<ProgressBar
android:id="@+id/device_progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateDrawable="@drawable/progress"
android:visibility="gone" />
</android.support.v4.widget.DrawerLayout>
Java代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Executing onCreate().");
setContentView(R.layout.activity_devices);
progress = (ProgressBar) findViewById(R.id.device_progress);
progress.setVisibility(View.VISIBLE);
manager = MainScreenActivity.getDeviceManager();
manager = MainScreenActivity.getDeviceManager();
drawerInfo = manager.getDrawerInfo();
mDrawerList = (ListView) findViewById(R.id.drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
adapter = new ArrayAdapter<String>(this,
R.layout.drawer_list_item, R.id.label, drawerInfo);
mDrawerList.setAdapter(adapter);
mDrawerLayout
.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Log.d(TAG, "onDrawerClosed() executed!");
}
@Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
Log.d(TAG, "onDrawerOpened() executed!");
refreshDrawer();
}
});
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int pos, long id) {
Log.d(TAG, "CLICKED " + pos);
selectDevice(pos);
}
});
}
找到了解決方案,而不是在Root DrawerLayout內部添加新元素,而是將其放置在應放置主要內容的FrameLayout內部。 對我來說,這是一個愚蠢的錯誤,但如果有人遇到同樣的問題。 這里是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/device_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminateDrawable="@drawable/progress"
android:visibility="gone" />
</FrameLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#000"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
DrawerLayout中有多個View。 醫生建議您只有兩個!
要添加導航抽屜,請使用DrawerLayout對象聲明用戶界面作為布局的根視圖。 在DrawerLayout內部,添加一個包含屏幕主要內容的視圖(隱藏抽屜時的主要布局),以及另一個包含導航抽屜內容的視圖。
我建議將ListvView和ProgressBar分組在一個容器中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.