长按时突出显示自定义列表视图项 [英] Highlight custom listview item when long click

查看:18
本文介绍了长按时突出显示自定义列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义 listview,它的左侧有一个 ImageView,右侧有一个 TextView.并使用 actionbar 上下文菜单实现多选 Listview.

I have created a custom listview which has an ImageView on the left and a TextView on the right. And implementing a multi-selection Listview with actionbar context menu.

问题是,当我长按一个项目时,它没有突出显示.

The problem is, when i long click on an item it didn't gets highlighted.

这是我如何在我的 ListFragment

密码片段.java

package mohd.itcs.safewallet;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class PasswordsFragment extends ListFragment {

private String titles[] = { "item1", "item2", "item3" };

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setListAdapter(new CustomPasswordsList(getActivity(), titles));

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    /*
     * Setup Multiple Selection Mode
     */
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> listview, View item,
                int position, long id) {
            getListView().setItemChecked(position, true);
            return true;
        }

    });

    getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

        @Override
        public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        /*
         * Inflate Actionbar Menu for Passwords Multiple Selection
         */
        @Override
        public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
            getActivity().getMenuInflater().inflate(
                    R.menu.passwords_context_menu, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode arg0) {
        }

        @Override
        public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode,
                int position, long id, boolean arg3) {
            /*
             * Change Title bar to number of selection
             */
            int checkedItems = getListView().getCheckedItemCount();
            actionMode.setTitle(String.valueOf(checkedItems) + " Selected");
        }
    });

}

}

自定义密码列表.java

CustomPasswordsList.java

package mohd.itcs.safewallet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomPasswordsList extends ArrayAdapter<String> {

private final Context context;
private final String values[];

public CustomPasswordsList(Context context, String[] values)
{
    super(context, R.layout.password_list_item, values);
    this.context = context;
    this.values = values;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.password_list_item, parent, false);
    TextView passwordTitle = (TextView) rowView.findViewById(R.id.textView_passwordTitle);
    ImageView passwordIcon = (ImageView) rowView.findViewById(R.id.imageView_passwordIcon);

    passwordTitle.setText(values[position]);

    passwordIcon.setImageResource(R.drawable.facebook);


    return rowView;
}

}

passwords_list_item

passwords_list_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="horizontal" >

<ImageView 
    android:id="@+id/imageView_passwordIcon"
    android:layout_height="60dp"
    android:contentDescription="@string/imageView_passwordIcon"
    android:layout_width="60dp" />

<TextView 
    android:id="@+id/textView_passwordTitle"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:paddingLeft="10dp"
    android:paddingTop="15dp"
    android:textSize="16sp"
    android:textColor="#000000"
    android:layout_weight="1" />

</LinearLayout>

推荐答案

使用getActivity()就够了

setListAdapter(new CustomPasswordsList(getActivity(), titles));

您需要覆盖onItemCheckedStateChanged

  public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        final int checkedCount = getListView().getCheckedItemCount();
        // get checked items count 

从样本中抽取@

android-sdk-linux/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16

示例:根据您的需要修改以下内容

Example : Modify the below according to your needs

public class MainActivity extends ListActivity {
    String[] GENRES = new String[] {
            "Action", "Adventure", "Animation", "Children", "Comedy",
        "Documentary", "Drama",
            "Foreign", "History", "Independent", "Romance", "Sci-Fi",
        "Television", "Thriller"
        };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        lv.setMultiChoiceModeListener(new ModeCallback());
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_activated_1, GENRES));
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getActionBar().setSubtitle("Long press to start selection");
    }

    private class ModeCallback implements ListView.MultiChoiceModeListener {

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.list_select_menu, menu);
            mode.setTitle("Select Items");
            return true;
        }

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

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.share:
                Toast.makeText(MainActivity.this, "Shared " + getListView().getCheckedItemCount() +
                        " items", Toast.LENGTH_SHORT).show();
                mode.finish();
                break;
            default:
                Toast.makeText(MainActivity.this, "Clicked " + item.getTitle(),
                        Toast.LENGTH_SHORT).show();
                break;
            }
            return true;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public void onItemCheckedStateChanged(ActionMode mode,
                int position, long id, boolean checked) {
            final int checkedCount = getListView().getCheckedItemCount();
            switch (checkedCount) {
                case 0:
                    mode.setSubtitle(null);
                    break;
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + checkedCount + " items selected");
                    break;
            }
        }

    }
}

list_select_menu.xml

list_select_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/share"
          android:title="share"
          android:icon="@android:drawable/ic_menu_share"
          android:showAsAction="always" />
</menu>

快照

因为您怀疑它是否适用于自定义适配器

Since you are doubting whether it would work with custom adapter

在 res/values-v11/styles.xml 下

under res/values-v11/styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light"></style>

    <style name="activated" parent="AppTheme">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>

</resources>

在根元素的自定义布局中添加

In the custom layout for the root element add

 style="@style/activated"

这篇关于长按时突出显示自定义列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆