我在listview中尝试了很多滚动到顶部的方法。但没有任何效果。
这是我的代码(缩短版):
public void placeRandomUsers(final String search) {
Randoms randoms=new Randoms(randomsList);
randomsAdapter=new RandomsAdapter(this,randoms);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomsAdapter);
loadRandomUsers(search);
}
private void loadRandomUsers(String search) {
for (int i=0; i<result.size(); i++) {
randomsList.add(new Random(result.get(i).getAsJsonObject(),0));
}
((RandomsAdapter) randomsAdapter).notifyDataSetChanged();
}
示例:
private void loadRandomUsers(String search) {
for (int i=0; i<result.size(); i++) {
randomsList.add(new Random(result.get(i).getAsJsonObject(),0));
}
################SCROLL AREA#################
((RandomsAdapter) randomsAdapter).notifyDataSetChanged();
}
这些是我特别针对SCROLL AREA
:
listView.setSelectionAfterHeaderView();
listView.smoothScrollToPosition(0);
listView.post( new Runnable() {
@Override
public void run() {
listView.smoothScrollToPosition(0);
}
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:scrollbars="none"
/>
</LinearLayout>
和项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/twitter"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="4dp"
android:paddingRight="4dp" >
<TextView
android:id="@+id/tweet"
style="@android:style/TextAppearance.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#0A0A14"
android:layout_marginLeft="8dp"
android:textSize="15sp"
android:fontFamily="sans-serif-condensed"
/>
<TextView
android:id="@+id/bio"
style="@android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#474747"
android:layout_marginLeft="8dp"
android:maxLines="3"
android:textSize="11sp"
android:layout_marginTop="0dp"/>
</LinearLayout>
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/anchor" />
</LinearLayout>
为什么这些方法不起作用?