ListView选择不正确

时间:2014-03-18 13:11:41

标签: android android-listview android-actionbar

我实施了Fragment ListView,其中ActionBar具有上下文ActionMode

OnLongClick启动上下文ListView以选择onLongClick的项目并删除所选项目。这部分工作得很好。我无法选择ListView的第一个条目 - 但这只是一个小问题。

正常&#39; onClick打开另一个活动,其中包含相应Activity条目的详细信息。打开新的ListView可以正常工作。但问题是:OnClick还会选择相应的<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context="com.test.resultfragment" > <ListView android:id="@+id/fragment_result_listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:choiceMode="multipleChoice" android:divider="@color/ktbl_gray_3" android:dividerHeight="1dp" android:listSelector="@drawable/selectable_background" android:saveEnabled="true" android:smoothScrollbar="true" > </ListView> <TextView android:id="@+id/fragment_result_textView_empty" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="No entries" android:textColor="@color/android_icon_dark" android:textSize="16sp" android:visibility="gone" /> </FrameLayout> 条目。

如何解决这个问题。有什么建议吗?

片段的XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:drawable="@drawable/list_focused" 
        android:state_focused="true" 
        android:state_pressed="false"/>
    <item android:drawable="@drawable/list_focused" 
        android:state_activated="true"/>
    <item android:drawable="@drawable/list_pressed" 
        android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

选择器:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:columnCount="4"
    android:orientation="horizontal"
    android:padding="@dimen/listview_padding"
    android:rowCount="4" >
    <!-- etc. ... -->
</GridLayout>

行的XML的短版本:

public class ResultFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener {

    private static final String TAG = ResultFragment.class.getName();

    private Activity mActivity;

    private ActionModeCallback mActionModeCallback;
    private ActionMode mActionMode;

    private ListView mListView;
    private ResultAdapter mAdapter;

    private ResultDAO mResultDAO;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated");

        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView");

        mActivity = getActivity();
        mActionModeCallback = new ActionModeCallback();

        mResultDAO = new ResultDAO(mActivity);

        View fragmentView = inflater.inflate(R.layout.myfragment, container,    false);

        setupView(fragmentView);

        return fragmentView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        Log.i(TAG, "onCreateOptionsMenu");

        inflater.inflate(R.menu.result_fragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "onOptionsItemSelected");

        switch (item.getItemId()) {
        case R.id.menu_item_result_fragment_help:
            startActivity(new Intent(getActivity(), ResultHelpActivity.class));
            break;
        default:
            Log.w(TAG, "onOptionsItemSelected - default case");
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.w(TAG, "onItemClick");

        if (mActionMode == null) {
            /* No contextual ActionBar active - show detail Activity */

            Result result = mAdapter.getItem(position);

            Intent resultDetailsIntent = new Intent(mActivity, ResultDetails.class);
            resultDetailsIntent.putExtra(AppConstants.TAG_RESULT, result);
            startActivity(resultDetailsIntent);
        } else {
            /* Contextual ActionBar active - Selectie/deselect ListView entries */
            mListView.setItemChecked(position, mListView.isItemChecked(position));
            updateCABTitle(mActionMode);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.w(TAG, "onItemLongClick");

        if (mActionMode == null) {
            mActionMode = mActivity.startActionMode(mActionModeCallback);
            /* false : onItemLongClick was not consumed --> open onItemClick() */
            return false;
        }

        /* true : onItemLongClick was consumed --> do nothing */
        return true;
    }

    private void setupView(View fragmentView) {
        Log.i(TAG, "setupView");

        setHasOptionsMenu(true);

        mAdapter = new ResultAdapter(mActivity);

        mListView = (ListView) fragmentView.findViewById(R.id.fragment_result_listView);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener((OnItemClickListener) this);
        mListView.setOnItemLongClickListener((OnItemLongClickListener) this);

        /* Set emptyView */

        TextView emptyView = (TextView) fragmentView.findViewById(R.id.fragment_result_textView_empty);
        mListView.setEmptyView((View) emptyView);
    }

    private void selectAllEntries() {
        Log.i(TAG, "selectAllEntries");

        for (int i = 0; i < mListView.getCount(); ++i) {
            mListView.setItemChecked(i, true);
        }

        updateCABTitle(mActionMode);
    }

    private void deselectAllEntries() {
        Log.i(TAG, "deselectAllEntries");

        for (int i = 0; i < mListView.getCount(); ++i) {
            mListView.setItemChecked(i, false);
        }

        updateCABTitle(mActionMode);
    }

    private void deleteSelectedEntries() {
        Log.i(TAG, "deleteSelectedEntries");

        SparseBooleanArray checkedItems = mListView.getCheckedItemPositions();

        for (int i = 0; i < checkedItems.size(); ++i) {
            if (checkedItems.valueAt(i)) {
                Result result = (Result) mListView.getItemAtPosition(i);
                if (result != null) {
                    mResultDAO.deleteResult(result);
                }
            }
        }

        refreshView();
    }

    private void refreshView() {
        Log.i(TAG, "refreshView");

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void updateCABTitle(ActionMode actionMode) {
        Log.i(TAG, "updateCABTitle");

        if (actionMode != null) {
            int numberOfCheckedItems = mListView.getCheckedItemCount();
            actionMode.setTitle(String.valueOf(numberOfCheckedItems)  + " " + getString(R.string.selektiert));
        }
    }

    private class ActionModeCallback implements ActionMode.Callback {

        /** Called when the action mode is created. This is called by startActionMode() */
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            Log.i(TAG, "onCreateActionMode");

            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.result_fragment_cab, menu);

            updateCABTitle(mode);

            return true;
        }

        /** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            Log.i(TAG, "onPrepareActionMode");
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            Log.i(TAG, "onActionItemClicked");

            switch (item.getItemId()) {
            case R.id.menu_item_cab_result_fragment_delete_selected:
                deleteSelectedEntries();
                /* Close contextual ActionBar */
                mode.finish();
                return true;
            case R.id.menu_item_cab_result_fragment_select_all:
                selectAllEntries();
                return true;
            case R.id.menu_item_cab_result_fragment_deselect_all:
                deselectAllEntries();
                return true;
            default:
                Log.w(TAG, "onActionItemClicked - default case");
                return false;
            }
        }

        /** Called when user exits action mode */
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            Log.w(TAG, "onDestroyActionMode");

            mActionMode = null;
            deselectAllEntries();
        }

    }

}

代码:

{{1}}

0 个答案:

没有答案