Highlight custom listview item when long click

后端 未结 2 1491
Happy的楠姐
Happy的楠姐 2020-11-29 05:32

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

相关标签:
2条回答
  • 2020-11-29 05:59

    Set in the layout file of your list's row (in the top level component, usually a LinearLayout or RelativeLayout):

    android:background="?android:attr/activatedBackgroundIndicator"

    To understand what it does exactly, check this other question.

    0 讨论(0)
  • 2020-11-29 06:15

    using getActivity() is enough

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

    You need to override onItemCheckedStateChanged

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

    Drawing from the samples @

    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

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

    Snap shot

    enter image description here

    Since you are doubting whether it would work with custom adapter

    enter image description here

    Edit:

    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"
    
    0 讨论(0)
提交回复
热议问题