在Android 4.0中禁用了DialogFragment中的ListView

时间:2015-06-23 14:19:37

标签: android dialogfragment

我已经制作了一个自定义DialogFragment,用于从后端选择用户。用户是异步加载的,因此我无法使用AlertDialog.Builder.setItems()因为onCreateDialog已被调用。

所以我使用带有ListView和ProgressBar的自定义布局。 ListView变为可见,设置适配器并在加载数据后设置onItemClickListener(并隐藏进度条)。

在使用Android 5的另一台设备上没有问题,但在我的Transformer TF101测试设备上使用Android 4.0.3我无法按下项目。

奇怪的是,当我切换应用程序并返回我的应用程序时,ListView已启用,现在我可以选择一些内容。

布局的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview_gebruikers"
    android:layout_gravity="center_horizontal"
    />

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progress_gebruikers"
    android:layout_gravity="center" />
</FrameLayout>

DialogFragment:

public static final String TAG = OverdragenDialogFragment.class.getSimpleName();

private static final String ARG_OVEREENKOMST = "arg_overeenkomst";
private static final String ARG_ACCOUNT_NAME = "arg_account_name";

private static final String STATE_ACCOUNT_NAME = "state_account_name";
private static final String STATE_OVEREENKOMST = "state_overeenkomst";

private OverdragenCallbacks mListener;

private ListView mListView;
private ProgressBar mProgressBar;


private Overeenkomst mOvereenkomst;
private String mAccountName;

/**
 * A handler object, used for deferring UI operations.
 */
protected Handler mHandler = new Handler();


/**
 * Create a new ChangeStatusDialogFragment
 * @return
 */
public static OverdragenDialogFragment newInstance(String accountName, Overeenkomst overeenkomst) {
    Bundle arguments = new Bundle();
    arguments.putString(ARG_ACCOUNT_NAME, accountName);
    arguments.putSerializable(ARG_OVEREENKOMST, overeenkomst);


    OverdragenDialogFragment fragment = new OverdragenDialogFragment();
    fragment.setArguments(arguments);
    return fragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //getActivity().getLayoutInflater();
    View parent = inflater.inflate(R.layout.dialog_gebruikers, null);

    if(savedInstanceState != null){
        mOvereenkomst = (Overeenkomst) savedInstanceState.getSerializable(STATE_OVEREENKOMST);
        mAccountName = savedInstanceState.getString(STATE_ACCOUNT_NAME);
    }else{
        mOvereenkomst = (Overeenkomst) getArguments().getSerializable(ARG_OVEREENKOMST);
        mAccountName = getArguments().getString(ARG_ACCOUNT_NAME);
    }

    initViews(parent);

    final Loader<List<GebruikerWrapper>> loader = getLoaderManager().getLoader(LoaderIds.API_GEBRUIKERS);
    if(loader != null){
        if(!loader.isStarted()) {
            loader.reset();
            loader.startLoading();
        }
    }else {
        getLoaderManager().initLoader(LoaderIds.API_GEBRUIKERS, null, new LoaderManager.LoaderCallbacks<List<GebruikerWrapper>>() {
            @Override
            public Loader<List<GebruikerWrapper>> onCreateLoader(int id, Bundle args) {
                mProgressBar.setVisibility(View.VISIBLE);
                return new GetGebruikersLoader(getActivity(), mAccountName);
            }


            @Override
            public void onLoadFinished(Loader<List<GebruikerWrapper>> loader, List<GebruikerWrapper> data) {
                mProgressBar.setVisibility(View.GONE);
                if (data == null) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(), "Kan de verkopers niet ophalen...", Toast.LENGTH_SHORT).show(); //TODO fatsoenlijke foutmelding
                            dismiss();
                        }
                    });
                }
                mListView.setAdapter(new GebruikerArrayAdapter(getActivity(), data));
                mListView.setOnItemClickListener(OverdragenDialogFragment.this);
                mListView.requestFocus();
            }

            @Override
            public void onLoaderReset(Loader<List<GebruikerWrapper>> loader) {

            }
        });
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.title_overdragen)
            .setView(parent);
    return builder.create();
}

private void initViews(View parent) {
    mListView = findView(parent, R.id.listview_gebruikers);
    mProgressBar = findView(parent, R.id.progress_gebruikers);
}

@SuppressWarnings("unchecked")
public <T extends View> T findView(View parent, int id) {
    return (T) parent.findViewById(id);
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(STATE_ACCOUNT_NAME, mAccountName);
    outState.putSerializable(STATE_OVEREENKOMST, mOvereenkomst);
}

public OverdragenDialogFragment setListener(OverdragenCallbacks mListener) {
    this.mListener = mListener;
    return this;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //[..] not relevant [..]        
}

2 个答案:

答案 0 :(得分:1)

Sounds like a focus problem. Some view is stealing the focus from your ListView and it becomes un-touchable. You can try debugging in your activity with similar code:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        Log.e(TAG, "Activity gained focus: " + getCurrentFocus());
    } else {
        Log.e(TAG, "Activity lost focus");
        try {
            Log.e(TAG, "Dialog window focused on: " +
                    dialogFragment.getDialog().getWindow().getCurrentFocus());
        } catch (NullPointerException e) {
            Log.i(TAG, "NPE", e);
        }
    }
}

Also this shouldn't be needed: mListView.requestFocus();

答案 1 :(得分:1)

正如Android Developer Guide for Dialogs中所述,您可以使用AlertDialog.Builder.setAdapter代替AlertDialog.Builder.setItems来使用动态数据备份列表(它专门提到Loader中的数据)。< / p>

为简单起见,以下示例使用Handler在启动后5秒填充列表而不是Loader。在运行Android 4.4.4的设备上运行良好。

public class ListDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {

    ArrayAdapter<String> arrayAdapter;
    List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                arrayAdapter.addAll(places);
            }
        }, 5000);

        arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Test List").setAdapter(arrayAdapter, this);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Log.d(getClass().getSimpleName(), String.format("User clicked item at index %d", which));
    }

}