简体   繁体   中英

ListView onScrollListener doesn't work

I have a problem. My onScrollListener doesnt work, while onItemClickListener that goes right after onScrollListener is working prefectly. Here is my code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.tracks, container, false);

        list = (ListView) view.findViewById(R.id.list);

        list.setOnScrollListener(new ListView.OnScrollListener() {

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

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                Log.d("SCROLL", "SCROLL");
            }
        });

list.setOnItemClickListener( new ListView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {
            mediaPlayerStart(position);
        }
    });

}

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

UPD: I've added toast. Still nothing. Maybe the reason can be that Tracks is a Fragment?

public class MainActivity extends AppCompatActivity { public ListView list;

ArrayList<String> listItems=new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    list = (ListView)findViewById(R.id.listView);

for(int i=0;i<20;i++) {

listItems.add(" "+String.valueOf(i));
}
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems);
    adapter.notifyDataSetChanged();
    list.setAdapter(adapter);
    list.setOnScrollListener(new ListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            Log.d("SCROLL", String.valueOf(scrollState));
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
        //    Log.d("SCROLL", "SCROLL");
        }
    });

    list.setOnItemClickListener( new ListView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                                int position, long id) {
          //  mediaPlayerStart(position);
            Log.d("Click", String.valueOf(position));
        }
    });

}

} try this code ....it will help you..

For the toast, make sure that you call toast.show() else it won't show up.

Also make sure that the Activity which contains this fragment properly adds it:

eg-

getFragmentManager().beginTransaction().add(R.id.container, new yourFragment()).commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM