突出显示列表行

时间:2017-08-10 11:41:02

标签: android listview android-arrayadapter multiple-choice onitemlongclicklistener

我遇到一个问题,长按一行以突出显示。

我查看了如何处理单击以导致另一个活动,并长按以启动上下文操作栏。我决定通过ListView.CHOICE_MODE_MULTIPLE_MODAL和ListView.CHOICE_MODE_NONE切换listview选项,让内置的android方法对选定的行进行工作。单击即可按预期工作。  多模式正在工作,上下文操作栏显示所选注释的数量,但行未突出显示。我检查了主题,它对Theme.AppCompat和Theme.AppCompat.Light都不起作用。

以下是代码 -

MainActivity extends AppCompatActivity {}

ObjectListFragment extends ListFragment implements
    AdapterView.OnItemLongClickListener, AbsListView.MultiChoiceModeListener {}

NoteListAdapter extends ArrayAdapter<NoteTO> {}

初始化ListFragment中的侦听器和其他对象

//get the data
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LogUtils.LOGD(getClass().getSimpleName(), "onViewCreated");

    ListView listView = getListView();
    listView.setOnItemLongClickListener(this);
    listView.setMultiChoiceModeListener(this);

    int choiceMode = (savedInstanceState == null ? ListView.CHOICE_MODE_NONE : savedInstanceState.getInt(STATE_CHOICE_MODE));
    listView.setChoiceMode(choiceMode);
}

动作模式方法,长按方法和私有方法

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    l.setItemChecked(position, true);

    NoteTO note = (NoteTO) l.getItemAtPosition(position);
    Intent i = new Intent(getActivity(), NoteActivity.class);
    NotePreferences.setNote(getActivity(), note);
    AppPreferences.setActionFlag(getActivity(), AppConstants.ACTION_UPDATE);
    startActivityForResult(i, 1);
}

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
    if (mActiveMode != null) {
        updateSubtitle(actionMode);
    }
}

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.contextual_menu, menu);

    this.mActiveMode = actionMode;
    updateSubtitle(mActiveMode);

    return true;
}

@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
    boolean result = performActions(menuItem);

    updateSubtitle(mActiveMode);

    return(result);
}

@Override
public void onDestroyActionMode(ActionMode actionMode) {
    if (mActiveMode != null) {
        mActiveMode = null;
        getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
        getListView().setAdapter(getListView().getAdapter());
    }
}

public boolean performActions(MenuItem item) {
    List<NoteTO> list = listAdapter.getList();
    Set<Integer> positionSet = listAdapter.getCurrentCheckedPosition();
    Integer[] positions = listAdapter.getCurrentCheckedPosition().toArray(new Integer[positionSet.size()]);

    if(item.getItemId() == R.id.item_delete) {
        List<NoteTO> notesToBeDeleted = new ArrayList<NoteTO>();
        String text = "";

        for(int i = 0; i < positions.length; i++) {
            NoteTO note = (NoteTO) list.get(positions[i]);
            notesToBeDeleted.add(note);
        }
        task = new ObjectListFragment.deleteNotesTask().execute(notesToBeDeleted);
        return true;
    }

    else if(item.getItemId() == R.id.item_share) {
        String text = "";

        for(int i = 0; i < positions.length; i++) {
            NoteTO note = (NoteTO) list.get(positions[i]);
            text = text + note.getBody() + "\r\n"+ "\r\n";
        }
        shareNotes(text);
        return true;
    }

    return false;
}

private void updateSubtitle(ActionMode mode) {
    mode.setSubtitle("(" + getListView().getCheckedItemCount() + ")");
}

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setItemChecked(position, true);

    return(true);
}

适配器 - 没有任何代码覆盖行的选择器。

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View row = super.getView(position, convertView, parent);
    NoteHolder holder = (NoteHolder) row.getTag();

    if(holder == null) {
        holder = new NoteHolder(row);
        row.setTag(holder);
    }

    NoteTO note = getItem(position);

    if(mSettings.getListSingleLine() == 0) {
        holder.textviewNote.setEllipsize(null);
        holder.textviewNote.setSingleLine(false);
    }
    else {
        holder.textviewNote.setEllipsize(TextUtils.TruncateAt.END);
        holder.textviewNote.setSingleLine();
    }

    holder.textviewNote.setText(note.getBody());
    holder.textviewNote.setTextSize(getmSettings().getListViewTextSize());
    holder.textviewNote.setTypeface(getmSettings().getGeneralFontStyleTypeFace());
    //holder.textviewNote.setBackgroundColor(ContextCompat.getColor(context, android.R.color.background_dark));
    //holder.textviewNote.setBackground(selector);

    //arraylist has containers to get the boolean of the position
    if (mSelection.get(position) != null) {

        //holder.textviewNote.setSelected(true);
        //holder.textviewNote.setBackgroundColor(ContextCompat.getColor(context, android.R.color.holo_blue_light));
        //holder.textviewNote.setTextColor(mSettings.getListViewTextColor());
        //holder.textviewNote.setBackground(selector);
    }

    return row;
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我忽略了将获得突出显示的行的样式 -

<item name="android:background">?android:attr/activatedBackgroundIndicator</item>

感谢

Highlight custom listview item when long click

Commonsware - 繁忙的编码器Android开发指南