gpt4 book ai didi

java - ListView onScrollListener 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:11 26 4
gpt4 key购买 nike

我有一个问题。我的 onScrollListener 不起作用,而 onScrollListener 之后的 onItemClickListener 工作正常。这是我的代码:

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:我加了 toast 。依然没有。也许原因是 Tracks 是一个 Fragment?

最佳答案

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

}

}试试这个代码....它会对你有帮助..

关于java - ListView onScrollListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37610424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com