【发布时间】:2017-06-23 11:07:04
【问题描述】:
我有一个 Activity 扩展 BaseActivity。它包含一个由我的适配器初始化的ListView(扩展BaseAdapter)。列表视图中的每个项目都是 CardView 和 Button、TextView 和 ProgressBar。 TextView 和 ProgressBar 之一是动态的,所以我创建了一个 Runnable Countdown 任务,它更新了 TextView 和 ProgressBar。使用 android.os.Handler.postDelayed() 每 100 毫秒调用一次 Countdown 任务。
问题是我想为ListView 中的每个项目实现OnItemLongClickListener 以使用ActionMode.Callback 实现用于删除、编辑等操作的操作栏。我已经实现了OnItemLongClickListener,但它永远不会得到调用。我能看到的一件奇怪的事情是,当我将 Countdown 任务延迟更改为 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;
}
}
}
CountdownTask:
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;
}
}
【问题讨论】:
标签: java android listview android-adapter android-actionmode