使用BaseAdapter实现未在ListView上调用OnItemLongClickListener

时间:2017-06-23 11:07:05

标签: java android listview android-adapter android-actionmode

我有一个Activity延长BaseActivity。它包含ListView,它由我的适配器初始化(扩展BaseAdapter)。列表视图中的每个项目都是CardViewButtonTextView& ProgressBar。其中一个TextViewProgressBar是动态的,因此我创建了一个Runnable倒计时任务,用于更新TextView和ProgressBar。使用android.os.Handler.postDelayed()每100毫秒调用倒计时任务。

问题在于,我想为OnItemLongClickListener中的每个项目实施ListView,以便使用ActionMode.Callback实施显示操作栏,以执行删除,编辑等操作。我已实施{ {1}}但它永远不会被调用。我能看到的一个奇怪的事情是,当我将倒计时任务延迟更改为 3000毫秒时,它会按预期工作。

以下是我的基本实施:

OnItemLongClickListener

<ListView android:id="@+id/list_accounts" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@null" android:dividerHeight="0dp" android:scrollbars="vertical" android:smoothScrollbar="true"></ListView> (列表中的每个项目):

CardView

我的ListActivity:

<android.support.v7.widget.CardView android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="180dp"
card_view:cardBackgroundColor="@color/white"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="1dp"
card_view:cardUseCompatPadding="true"
xmlns:android="http://schemas.android.com/apk/res/android">

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

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

        <TextView android:id="@+id/applicationName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15sp"
            android:textColor="@color/CustomBlack"
            android:layout_alignParentTop="true"
            android:textSize="18sp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="top"
            android:text="miniorange"
            android:fontFamily="sans-serif-light"/>

        <TextView
            android:id="@+id/user_acc_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35sp"
            android:layout_marginLeft="10dp"
            android:textColor="@color/CustomBlack"
            android:textSize="18dp"
            android:fontFamily="sans-serif-light"
            />


    </FrameLayout>



    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="40dip" >

        <TextView android:id="@+id/token_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginTop="15dip"
            android:textColor="@color/CustomBlack"
            android:textSize="50dp"
            android:layout_marginLeft="5dp"
            android:fontFamily="sans-serif-light"
            />


         <Button

            android:id="@+id/button1"
            android:layout_gravity="right"
            android:layout_marginTop="30dip"
            android:text="SCAN QR"
            android:background="@color/ButtonNavyBlue"
            android:textColor="@color/white"
            android:drawableLeft="@drawable/qrcode_scan_white"
            android:paddingLeft="15dp"
            android:gravity="center"
            android:layout_marginRight="10dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="130dip"
            android:layout_height="40dip"
            android:fontFamily="sans-serif-light"/>


        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="4dip"
            android:id="@+id/progress"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:max="100"
            android:layout_marginTop="90dp"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:progressDrawable="@drawable/progress_bar"
            />


    </FrameLayout>
</RelativeLayout>

</android.support.v7.widget.CardView>

CountdownTask:

public class ListAccountsActivity extends BaseActivity {

private ListView mUserList;
private List<Users> mUsers;
private UserAccountsListAdapter mUserAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mUserList = (ListView) findViewById(R.id.list_accounts);

    // Initialize Users list from Database
    mUsers = new ArrayList<>();

    mUserAdapter = new UserAccountsListAdapter(ListAccountsActivity.this, mUsers, new
            BtnClickListener() {
        @Override
        public void onBtnClick() {
            // My onclick action here
        }
    });

    mUserList.setAdapter(mUserAdapter);
    mUserList.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> parent,
                                       View view, int position, long id) {
            onListItemSelect(position);
            return true;
        }
    });
}

@Override
public void onStart() {
    super.onStart();

    startCountdown();
}

private void startCountdown() {
    if (mUserList != null) {
        countdownTask = new CountdownTask(ListAccountsActivity.this, new CountdownTask
                .Listener() {
            @Override
            public void updateTextAndProgress() {
                updateTextAndProgressView();
            }
        });
        countdownTask.run();
    }
}

private void updateTextAndProgressView() {
    for (int i = 0; i < mUserList.getChildCount(); i++) {
        View view = mUserList.getChildAt(i);
        ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress);
        TextView text = (TextView) view.findViewById(R.id.token_num);           

        // Update this TextView and ProgressBar
    }
}

private void onListItemSelect(int position) {
    mUserAdapter.toggleSelection(position);
    boolean hasCheckedItems = mUserAdapter.getSelectedCount() > 0;

    if (hasCheckedItems && mActionMode == null)
        // there are some selected items, start the actionMode
        mActionMode = startActionMode(new ActionModeCallback());
    else if (!hasCheckedItems && mActionMode != null)
        // there no selected items, finish the actionMode
        mActionMode.finish();

    if (mActionMode != null)
        mActionMode.setTitle(String.valueOf(mUserAdapter
                .getSelectedCount()) + " selected");
}

private class ActionModeCallback implements ActionMode.Callback {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_delete:

                SparseBooleanArray selected = mUserAdapter
                        .getSelectedIds();
                for (int i = (selected.size() - 1); i >= 0; i--) {
                    if (selected.valueAt(i)) {
                        Users selectedItem = (Users) mUserAdapter
                                .getItem(selected.keyAt(i));
                        mUserAdapter.remove(selectedItem);
                    }
                }
                mode.finish();
                return true;
            default:
                return false;
        }
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public void onDestroyActionMode(ActionMode mode) {
        mUserAdapter.removeSelection();
        mActionMode = null;
    }
}
}

适配器:

public class CountdownTask implements Runnable {

private final Handler mHandler = new Handler();

private Context context;
private Listener listner;

public CountdownTask(Context context, Listener listener) {
    this.context = context;
    this.listner = listener;
}

interface Listener {
    void updateTextAndProgress();
}

@Override
public void run() {
    listner.updateTextAndProgress();
    mHandler.postDelayed(this, 100);
}
}

3 个答案:

答案 0 :(得分:1)

添加:

android:descendantFocusability="blocksDescendants"

Listview行项目布局 - android.support.v7.widget.CardView

问题是您的Listview具有可关注元素Button,它会阻止列表项单击事件。

答案 1 :(得分:0)

在ListView中添加

android:longClickable="true"

答案 2 :(得分:0)

  

使用以下代码更新您的ListView XML

<ListView
        android:id="@+id/list_accounts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:focusable="true"
        android:scrollbars="vertical"
        android:smoothScrollbar="true"></ListView>

更新自定义视图

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="5dp"
        card_view:cardElevation="1dp"
        android:focusable="false"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout 
                android:layout_width="match_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:id="@+id/applicationName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_gravity="top"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="15sp"
                    android:fontFamily="sans-serif-light"
                    android:text="miniorange"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/CustomBlack"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/user_acc_email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="35sp"
                    android:fontFamily="sans-serif-light"
                    android:textColor="@color/CustomBlack"
                    android:textSize="18dp" />


            </FrameLayout>


            <FrameLayout 
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_marginTop="40dip">

                <TextView
                    android:id="@+id/token_num"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="15dip"
                    android:fontFamily="sans-serif-light"
                    android:textColor="@color/CustomBlack"
                    android:textSize="50dp" />


                <Button

                    android:id="@+id/button1"
                    android:layout_width="130dip"
                    android:layout_height="40dip"
                    android:layout_gravity="right"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="30dip"
                    android:background="@color/ButtonNavyBlue"
                    android:drawableLeft="@drawable/qrcode_scan_white"
                    android:fontFamily="sans-serif-light"
                    android:gravity="center"
                    android:paddingLeft="15dp"
                    android:text="SCAN QR"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/white" />


                <ProgressBar
                    android:id="@+id/progress"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="4dip"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="15dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="90dp"
                    android:max="100"
                    android:progressDrawable="@drawable/progress_bar" />


            </FrameLayout>
        </RelativeLayout>

    </android.support.v7.widget.CardView>