Android Listview: Get Data Index of Visible Item

Android ListView: get data index of visible item

It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.

No need for all the scary stuff shown in the reply above :)

retrieve the index of the first and the last visible item in the ListView or GridView?

You can write a custom ScrollListener and setOnScrollListener to the listView.

Hope this link helps.

How to get items currently displayed in AdapterView?

implements OnScrollListener

public class NewsCategoryDC extends Activity implements OnScrollListener {

and set OnScrollListener in listView

listView.setOnScrollListener(NewsCategoryDC.this);

and you can get first and last visible rows

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
firstVisibleRow = listView.getFirstVisiblePosition();
lastVisibleRow = listView.getLastVisiblePosition();
/*Now you get the first and last visible rows, and you easily get the rows from first to last visible row and allocate resources to visible rows or deallocate resources to rows except visible rows..,.*/
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}

TRY THIS..,.

and if some other best way you got please post, this is very useful and good question..,.

Thanks..,.

EDIT............................

add code in onScroll() method

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
int firstVisibleRow = listView.getFirstVisiblePosition();
int lastVisibleRow = listView.getLastVisiblePosition();

for(int i=firstVisibleRow;i<=lastVisibleRow;i++)
{
//Write your code here(allocation/deallocation/store in array etc.)
System.out.println(i + "=" + listView.getItemAtPosition(i));
}
}

Android load only visible elements of ListView

After checking the code: I am sure that ONLY the images that are visible are being loaded!

BUT because some of the images down have the same url! You will think that they are loading in a different order. However they are loaded based on the images visible but any other listitem with the same url will show the image.


I'm using Lazy Loading List in my application and I need to load only the visible elements of ListView.

getView is called for each position that is visible on the ListView

The pictures you need to load must be added to the queue from the getView of the Adapter

So you should Override

public View   getView  (int position, View convertView, ViewGroup parent){
super(position,convertView,parent);
//now add the picture at position to the queue
}

Note: Be careful

Say you added photo 1 to the queue: This does not mean that getView will not be called again with position 1.

In other words, account for the fact that a position may be called many times using getView.

Number of visible list items in dynamic list view

After you initialise the list and set an adapter you can use the ListView's getLastVisiblePosition() and getFirstVisiblePosition() methods too see how many items are visible.

Note: The ListView has to be drawn on screen in order for these methods to work. If you want to get the count just after the list is drawn you could try posting a runnable on the view itself like so:

lvRecords.post(new Runnable() {
@Override
public void run() {
int itemsShown = lvRecords.getLastVisiblePosition() - lvRecords.getFirstVisiblePosition();
}
});

If you are planning to count the items every time the user scrolls, you can implement a scroll listener:

    lvRecords.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE) {
int noOfItemsVisible = lvRecords.getLastVisiblePosition() - lvRecords.getFirstVisiblePosition();
Log.e("no of items visible", String.valueOf(noOfItemsVisible));
}
}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.e("no of items visible", String.valueOf(visibleItemCount));
}
});

The onScrollStateChanged method gets called every time the state changes (the user is scrolling, a fling is performed, or the list stops scrolling).

The onScroll method gets called every time the list position changes, and will be called multiple times per second while the list is being scrolled, so you should avoid doing too many operations here.

Android: Get the position of a ListView from the actual top

I fixed it myself after many painful attempts. It was a lack of understanding how ListView optimization operates and what the convertView parameter does ...

I moved my onClickListener outside the if (convertView == null) block and placed it right before getView returns its view. This way the location that is taken is not the recycled visable location but the ACTUAL location all the way from the top.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
final AiringModel am = list.get(position);
am.setRealPosition(position, list.get(position).getUrl());
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.addtowatchlist, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
if (convertView == null) {
view.setTag(viewHolder);
} else {
view = convertView;
((ViewHolder)view.getTag()).addbutton.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
viewHolder.addbutton = (Button) view.findViewById(R.id.check);
viewHolder.addbutton.setTag((Integer)position);
viewHolder.addbutton
.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View buttonview) {
// TODO Auto-generated method stub
int realPosition = am.getRealPosition();
String realName = am.getRealName();
Log.d("Pair created!", "Clicked button with position: "+ realPosition + " For item name: " + realName);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(list.get(realPosition).getUrl()));
context.startActivity(browserIntent);
}
});
return view;
}

android listview first visible position

Have a look at this question.

Android ListView get current position in visible items

getFirstVisiblePosition() can get first visible position in all items,so you can use getSelectedItemPosition()-getFirstVisiblePosition() to make it.



Related Topics



Leave a reply



Submit