Reputation: 2142
I have a listview where you can see your friends and in every listview item I have a button where you can follow them. Above the listview I have another button and if you click on it I want it to follow all your friends instantly.
Is there a way where you can click all the buttons from an listview at once?
Upvotes: 0
Views: 88
Reputation: 10177
Won't work, not that way.
The listView is a view of only a section of your data. If only 10 items fit on screen, then only 10 buttons exist. Buttons that fall off the screen are "recycled" so that they can be used for the data that is coming into view. ListViews don't read ahead, that's what makes them efficient at displaying large data sets.
Rather cycle the data that backs your listView, instead of trying to cycle actual listView buttons.
Upvotes: 0
Reputation: 4239
i'm not sure about what all you're using, as far as frameworks, libraries, etc., but with angular, it's actually pretty easy to do something like this. check out this plunker. http://plnkr.co/W3Pm5uOBt9WgVtyIXNky.
it's a simple to-do prototype. note the "mark all" button.
$scope.markAll = function() {
$scope.todos = $scope.todos.map(function(e) {
e.done = $scope.allDone;
return e;
});
};
(html file below)
<p>
<input type="checkbox" ng-model="allDone" ng-click="markAll()" />
Mark All
</p>
Upvotes: 0
Reputation: 12674
The problem here is that a ListView
recycles Views so you don't really have a "follow" button for all your friends. You will have Views for all the friends you see on the screen and a few off the screen but that is it. If you have a 100 or more friends in that list or even a dozen or so more than you see on the screen, there is no "follow" button.
In your case, you have two options.
notifyDatasetChanged
on your ListView to update it.getView
for each position, get the button you want from there and call performClick
on it. While this will work and probably have the desired result, I would not recommend it as it's just a lot of processing for no reason. Upvotes: 0
Reputation: 506
This is how I would suggest setting up your code:
You should have a custom list adapter where you should have your button listener that gets called when a listview item is clicked. This button listener should call a method that 'checks' this friend as followed, or execute some code to follow that person.
In your activity class (where you set the list adapter to your listview), you can then have a button listener for the master follow button. When this button is clicked, iterate through each of the items in your listview and call the method to 'follow' them.
If you post your list adapter and base code, I can help you set it up. The base of the code can be referenced from: https://stackoverflow.com/a/8166802/1869576
Upvotes: 2