Showing The Current Selection in a Listview

Showing the current selection in a listview

What Gmail and similar apps use is the activated state, with an appropriate row layout. See:

  • Setting Android Background & Persistence Menu Bar - Using attribute on older versions causes crash - Is there a theme /pattern approach?
  • Change colour of activated list item background on Honeycomb

In a nutshell, you:

  • Use a row layout with an activated background (e.g., android.R.layout.simple_list_item_activated_1)
  • Use setChoiceMode(ListView.CHOICE_MODE_SINGLE) on your ListView
  • "Check" the row that should be activated using setItemChecked() on your ListView to enable the "activated" state and have the persistent highlight

Center a ListView on its current selection

I haven't tried any of this but based on the current selection could you use public void smoothScrollByOffset (int offset) to get the view to scroll to where you want so that your selection is in the middle of the view?

Disabling a ListView in C#, but still showing the current selection

You could also make the ListView ownerdraw. You then have complete control over how the items look whether they are selected or not or whether the ListView itself is enabled or not. The DrawListViewItemEventArgs provides a way to ask the ListView to draw individual parts of the item so you only have to draw the bits you're interested in. For example, you can draw the background of the item but leave it up to the ListView to draw the text.

Showing current/selected item in ListView in WinForms

Try using item.EnsureVisible();

ListView - selected item is not working as expected

Add the default state to the selector xml, specifically as the last item.

<selector ...>
...
...
<!-- Normal list item -->
<item android:drawable="@drawable/lv_bg_normal_state" />
</selector>

Update:

Use the android:state_activated in your selector, setChoiceMode(ListView.CHOICE_MODE_SINGLE) on your listview and call setItemChecked() when an item is clicked.

Inflate android.R.layout.simple_list_item_activated_1 in your getView method to get the CHOICE_MODE_SINGLE working.

For more details, refer this post :
Showing the current selection in a listview

Setting a ListView item to selected

I don't know why the view won't show as selected, even though it is set to selected, but this code should work:

Use ListView.setItemChecked():

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(position, isActivated);

Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/row_master_detail_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/row_master_detail_focused"
android:state_focused="true" />
<item android:drawable="@color/nw_row_master_master_selected"
android:state_activated="true" />
<item android:drawable="@color/nw_row_master_normal" />
</selector>

Because android:state_activated is only available post-Honeycomb, add this to getView():

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// Highlight selected item
if (position == selectedPos) {
view.setBackgroundColor((view.getResources()
.getColor(R.color.nw_row_master_selected)));
} else {
Drawable sel = view.getResources().getDrawable(
R.drawable.selector_row_collection_master);
view.setBackgroundDrawable(sel);
}
}

Enable selection inside ListView

I found the problem.

Defining the xml layout for the row used in the custom adapter, I needed to add this row, to the root element:

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

But, the attribute activatedBackgroundIndicator has been introduced since API level 11, so I have put the previous version of my xml in layout folder, and the new one in the layout-v11 folder.

Also the following statement is required, inside the onListItemClick method:

getListView().setItemChecked(position, true);

Flutter - select only single item in list view

Instead of manually deselecting tiles, just keep track of which tile is currently selected.

I've made a simple example for you. When we click a tile, we just set the selected index to the index we clicked, and each tile looks at that to see if its the currently selected tile.

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(body: Home()),
);
}
}

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
int selectedIndex;

@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item: $index'),
tileColor: selectedIndex == index ? Colors.blue : null,
onTap: () {
setState(() {
selectedIndex = index;
});
},
);
},
);
}
}


Related Topics



Leave a reply



Submit