gpt4 book ai didi

java - OnItemLongClickListener 未在具有 BaseAdapter 实现的 ListView 上调用

转载 作者:行者123 更新时间:2023-11-30 00:29:26 25 4
gpt4 key购买 nike

我有一个 Activity 扩展 BaseActivity。它包含一个 ListView,它由我的适配器(扩展 BaseAdapter)初始化。 ListView 中的每个项目都是一个带有 ButtonTextViewProgressBarCardViewTextViewProgressBar 之一是动态的,因此我创建了一个 Runnable 倒计时任务来更新 TextView 和 ProgressBar。使用 android.os.Handler.postDelayed() 每 100 毫秒调用一次倒计时任务。

问题是我想为 ListView 中的每个项目实现 OnItemLongClickListener 以使用 ActionMode.Callback 实现操作来显示操作栏像删除、编辑等。我已经实现了 OnItemLongClickListener 但它从未被调用。我看到的一件奇怪的事情是,当我将倒计时任务延迟更改为 3000 毫秒时,它会按预期工作。

这是我的基本实现:

<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(列表的每一项):

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

我的 ListActivity:

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);
}
}

适配器:

public class UserAccountsListAdapter extends BaseAdapter {

private Context context;
private List<Users> accountList;
private BtnClickListener btnClickListener;
private SparseBooleanArray mSelectedItemsIds;

public UserAccountsListAdapter(Context context, List<Users> accountList, BtnClickListener btnClickListener) {
this.context = context;
this.accountList = accountList;
this.btnClickListener = btnClickListener;
mSelectedItemsIds = new SparseBooleanArray();
}

@Override
public int getCount() {
return accountList.size();
}


@Override
public Object getItem(int position) {
return accountList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.activity_account_layout2, null);
convertView.setLongClickable(true);
}

Button button1 = (Button) convertView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnClickListener.onBtnClick();
}
});

TextView accountEmail = (TextView) convertView.findViewById(R.id.user_acc_email);
ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
progressBar.setMax(100);
accountEmail.setText(accountList.get(position).getUserKey());

return convertView;
}

public int getSelectedCount() {
return mSelectedItemsIds.size();
}

public void remove(UserAccountItem object) {
accountList.remove(object);
notifyDataSetChanged();
}

public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}

public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}


public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
}

最佳答案

添加:

android:descendantFocusability="blocksDescendants"

到您的 Listview 行项目布局——在 android.support.v7.widget.CardView

问题是您的 Listview 具有可聚焦元素 Button,它阻止了列表项点击事件。

关于java - OnItemLongClickListener 未在具有 BaseAdapter 实现的 ListView 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com