Search code examples
androidlistviewonscroll

How to get list row item(textview) id and text in onScroll of Listview in android?


I have used base adapter in to create a custom listview. There is an ImageView and TextView in each row of listview. What I want to do is that when I scroll listview I want to get the text of textview in firstvisible row of listview and then I want to make the textview invisible of firstvisible row of listview. How can I get Id and data of a view in onScroll method of listview.

@Override
        public View getView(int arg0, View convertView, ViewGroup arg2) {
            // TODO Auto-generated method stub

            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_row, arg2, false);
            TextView tv = (TextView)convertView.findViewById(R.id.textViewrow);
            tv.setText("position " + arg0);
            tv_item = tv;
//          tv.setTag("position " + arg0);




            return convertView;
        }





@Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
//              ViewGroup viewGroup = (ViewGroup) view.getAdapter()
//                        .getView(firstVisibleRow, null, view);//
                int post = lv.getFirstVisiblePosition();
                TextView tv = (TextView)lv.findViewWithTag("position " + post);
                System.out.println("position " + post);
//              View v= lv.findViewWithTag("position " + post);
//              v.findViewById(R.id.textViewrow).gett
//              System.out.println("position text" + tv.getText());



                /*tv_item.getHitRect(rect2);
                if (Rect.intersects(rect1, rect2)) {
                    Toast.makeText(getApplicationContext(), "intersected", Toast.LENGTH_LONG).show();
                }*/

            }

Solution

  • I guess this problem is connected with this :)

    To resolve your problem I searched a little and I found this

    Now to fix your problem you have to follow these steps:

    You have to get the View of your first visible row like this:

        ....
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
            View view = mListView.getAdapter().getView(firstVisibleItem, null, mListView);
            TextView mTextView = (TextView) view.findViewById(R.id.textViewrow);
            mTextView.setVisbility(View.GONE);
    

    I hope it helps you,