web-dev-qa-db-ja.com

長いクリックでカスタムリストビューアイテムを強調表示

左側にlistview、右側にImageViewを持つカスタムTextViewを作成しました。そして、Listviewコンテキストメニューを使用して複数選択actionbarを実装します。

問題は、アイテムを長押ししても強調表示されないことです。

listviewListFragmentを設定する方法は次のとおりです。

PasswordsFragment.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");
        }
    });

}

}

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

<?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>
16
user1785900

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

例:必要に応じて以下を変更します

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

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

スナップショット

enter image description here

カスタムアダプタで動作するかどうか疑問に思っているので

enter image description here

編集:

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>

ルート要素のカスタムレイアウトで追加

 style="@style/activated"
21
Raghunandan

リストののレイアウトファイルに設定します(最上位コンポーネント、通常はLinearLayoutまたはRelativeLayout):

Android:background = "?android:attr/activateBackgroundIndicator"

それが正確に何をするのかを理解するには、これをチェックしてください 他の質問

13
bokyun Ko