繁体   English   中英

ListView onScrollListener不起作用

[英]ListView onScrollListener doesn't work

我有个问题。 我的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:我加入了吐司。 依然没有。 也许原因可能是Tracks是片段?

公共类MainActivity扩展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));
        }
    });

}

试试这个代码....它会帮助你..

对于吐司,请确保您调用toast.show()否则它将不会显示。

还要确保包含此片段的Activity正确添加它:

例如-

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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