简体   繁体   English

Android ListView onItemClickListener被ProgressBar阻止

[英]Android ListView onItemClickListener being blocked by ProgressBar

I had my onItemClickListener working for my ListView inside of my DrawerLayout working fine. 我让我的onItemClickListener在DrawerLayout内为ListView工作正常。 But I added a ProgressBar that is displayed before anything else then it is set to View.GONE. 但是我添加了一个ProgressBar,该显示在任何其他内容之前显示,然后将其设置为View.GONE。 However, I can't select and items from my list view any more. 但是,我无法选择,并且列表中的项目也无法查看。

list_item.xml 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: 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 Code: 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);
        }
    });
}

Found the solution, instead of adding a new element inside of the Root DrawerLayout, put it inside of the FrameLayout where your main content should go. 找到了解决方案,而不是在Root DrawerLayout内部添加新元素,而是将其放置在应放置主要内容的FrameLayout内部。 This was a dumb mistake by me, but if anyone else is having the same problem. 对我来说,这是一个愚蠢的错误,但如果有人遇到同样的问题。 Here it is: 这里是:

<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>

There are multiple View inside your DrawerLayout. DrawerLayout中有多个View。 The doc suggest you only have two! 医生建议您只有两个!

To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. 要添加导航抽屉,请使用DrawerLayout对象声明用户界面作为布局的根视图。 Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer. 在DrawerLayout内部,添加一个包含屏幕主要内容的视图(隐藏抽屉时的主要布局),以及另一个包含导航抽屉内容的视图。

I would suggest to group the ListvView and the ProgressBar in one container 我建议将ListvView和ProgressBar分组在一个容器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM