Where Am I in Listview Android? - android

I use Listview with Remote Control (Q1 Himedia SettopBOx)
Now down and up keydown ،How do I know where I am in listview
for example: when arrived in last item in listview then list refreshed or going to first item?
Please Help Me....
thanks

Take a look at OnScrollListener of ListView. You can set a new callback using ListView.setOnScrollListener() and get first visible item position and number of visible entries by implementing onScroll function of the listener.

Use an OnScrollListener for the listview like so.
myListView.setOnScrollListener(new OnScrollListener()
{
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
// Your code goes here. Determine the position with the firstVisibleItem.
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING)
{
Log.d(TAG, "User is flinging through the view");
}
}
});

Related

How to load more records on scrolling in tv android box

I have set up setOnScrollListener event on my listview its working good in mobile here is my code
movieList.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
userScrolled = true;
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(userScrolled && (firstVisibleItem + visibleItemCount >= totalItemCount)){
pageNum++;
int index = movieList.getFirstVisiblePosition();
float indexY = movieList.getScrollY();
load_loader.setVisibility(View.VISIBLE);
getMovies();
movieList.setSelection(index);
userScrolled = false;
}
}
});
Now i have installed my app to android tv box my full app is working good in tv box but list view not loading more records i think because of touch scroll tv is not touch its operated by remote navigation please help me to solve this problem thank you
Try setting listView.setItemsCanFocus(true); to your ListView so that focusable views inside of your listItems won't be ignored. Source.
You may also check this documentation on how you can actually navigate around your app when using remote control buttons instead of a touch screen.

OnScoll called multiple times in ListView

While using this, if i scroll a small amount, why is onScroll() called for 20 or so times i.e 5-6 times for each firstVisibleItem. Why not its called once for each scroll action?
listView.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("hey","called"+firstVisibleItem);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
});
If you look at implentation of ListView, you can see that mOnScrollListener.onScroll is called only once in ListView, actually in invokeOnItemScrollListener() method.
void invokeOnItemScrollListener() {
if (mFastScroll != null) {
mFastScroll.onScroll(mFirstPosition, getChildCount(), mItemCount);
}
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(this, mFirstPosition, getChildCount(), mItemCount);
}
onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these.
}
So let's try find some usages of invokeOnItemScrollListener(). We got a lot, including usages in AbsListView. In AbsListView, invokeOnItemScrollListener() is called indirectly every time in onTouchMove method. So now, it should be clear why you receive onScroll callback so many times.

Detect Top of ListView Scroll

I want to detect the Top of List view and i am using this method.
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
swipeRefreshLayout.setEnabled(true);
else
swipeRefreshLayout.setEnabled(false);
}
This works fine, but the problem is I have attached the Header View to list as well. When I scrolled up, as soon as the first item is visible(not the Header view) it calls pull to refresh of list view. How can i detect that the List's Header is completely visible.
My List View Is
View imageSlider = inflater.inflate(R.layout.image_slider_layout, null, false);
findViewById(imageSlider);
mPullRefreshListView.addHeaderView(imageSlider);
private void findViewById(View view) {
mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
}
hmm, Interesting answer lies in onTouch listener rather then onScroll, see the implementation below, it exactly tells u when header is completely visible, you can refine the logic further.. its just a quick implementation form me.
// Set a on touch listener for your list
mList.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// get the top of first child
View mView = mList.getChildAt(0);
int top = mView.getTop();
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:
// see if it top is at Zero, and first visible position is at 0
if(top == 0 && mList.getFirstVisiblePosition() == 0){
Toast.makeText(MainActivity.this, "Header Item Visible",
Toast.LENGTH_SHORT).show();
}
}
// dont forget to retrun false here
return false;
}
});
You can simply identifies it with visibleitemCount in onScroll Listener also.
Firstly identify your listview's visible item count at the top of listview include the header. you can get the count by toasting or debugg the code.
in my case
my listview's normal visibleItemCount is 3 and at the place of top it is 2 (with headerview), then my code is work for me.
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem==0&&visibleItemCount==2)
{
swp.setEnabled(true);
}
else{
swp.setEnabled(false);
}
}

Android: ListView onScrollListener doesn't stop listening

I've implemented ListView in my application and working on onScrollListener. The ListView is working fine and in the onScroll() method of the onScrollListener, i'm trying to show a progressbar when the user has scrolled to the bottom.
The problem is that the listener doesn't stop listening and there are multiple progressbar loading on the screen if the user is trying to scroll even more. I don't have any idea how to solve this.
I want the onScrollListener to stop listening when the progressbar shows.
Please help
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (list.getLastVisiblePosition() == list.getAdapter().getCount() -
&& list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()) {
ProgressBar pb = new ProgressBar(getActivity());
pb.setLayoutParams(pbParams);
linearLayout.addView(pb);
}
}
});
Thanks in advance
Can't you just check to see if there is already a progress bar and not add another?

How to Control Fling action of Listview

We have used Custom List View inside that one Text View and two Edit text present. I have put On Item CLick Listener On the Edit text Listener is set properly. What Is problem when fling action is happen then that listener is not setting. Is their any other Listener which can help me to control fling action.
may be you can't control the flying action, you can listen the flying action:
ListView.OnScrollListener can listen the scroll action:
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == OnScrollListener.SCROLL_STATE_FLING){ //flying
}
}
you can try this, hope it can help you -):

Categories

Resources