我有个问题。我的onScrollListener无法正常工作,而onItemClickListener恰好在onScrollListener正常工作之后运行。
这是我的代码:

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);
        }
    });

}


这是我的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:
我加了吐司。依然没有。也许原因可能是“曲目是片段”?

最佳答案

公共类MainActivity扩展了AppCompatActivity {
    公共ListView列表;

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));
        }
    });

}


}
试试这个代码..它将帮助您..

07-27 13:27