滚动回到 listvview 的顶部
Scrolling back to top of listvview
我有一个从 parse.com 检索数据的列表视图。我添加了 onscroll 侦听器以在每 15 个项目之后加载列表项目。它工作正常,但是当加载整个列表并且当我们想要滚动回顶部时,滚动并不平滑,就像上下移动一样。为了更好地理解我的问题watch this video
我的代码
public class InterActivity extends Activity
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
private int limit = 15;
View footerView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
shrdPreference = new SharedPreference();
//Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading");
// Set progressdialog message
mProgressDialog.setMessage("Please wait loading ...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit);
ob = query.find();
for (ParseObject inter : ob) {
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.inter_layoutListView);
// Pass the results into ListViewAdapter.java
adapter = new FinalAdapter(InterActivity.this,
codelist);
// AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
// animationAdapter.setAbsListView(listview);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
//listview.setOnItemClickListener(InterActivity.this);
//listview.setOnItemLongClickListener(InterActivity.this);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) { // TODO Auto-generated method stub
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
}
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter = new FinalAdapter(InterActivity.this,
codelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
您不应在每次加载更多数据时重新创建列表和适配器。用这个替换 LoadMore class:
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist.clear();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter.notifyDataSetChanged();
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
告诉我:)
尝试使用 OnTouchListener
而不是 OnScrollListener
(这将帮助您更轻松地检测滚动方向并做出相应反应):
listView.setOnTouchListener(new View.OnTouchListener() {
float height;
@Override
public boolean onTouch(View v, MotionEvent event) {
float height = event.getY();
int action = event.getAction();
int threshold = 1;
int count = listview.getCount();
if(action == MotionEvent.ACTION_DOWN){
// do nothing
this.height = height;
}else if(action == MotionEvent.ACTION_UP){
if(this.height < height){
Log.v(TAG, "Scrolled up");
// do nothing - user is going up
}else if(this.height > height){
Log.v(TAG, "Scrolled down");
//execute LoadMore Task
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
return false;
}
});
我有一个从 parse.com 检索数据的列表视图。我添加了 onscroll 侦听器以在每 15 个项目之后加载列表项目。它工作正常,但是当加载整个列表并且当我们想要滚动回顶部时,滚动并不平滑,就像上下移动一样。为了更好地理解我的问题watch this video
我的代码
public class InterActivity extends Activity
{
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
List<CodeList> codelist = null;
SharedPreference shrdPreference;
private int limit = 15;
View footerView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
shrdPreference = new SharedPreference();
//Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading");
// Set progressdialog message
mProgressDialog.setMessage("Please wait loading ...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit);
ob = query.find();
for (ParseObject inter : ob) {
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.inter_layoutListView);
// Pass the results into ListViewAdapter.java
adapter = new FinalAdapter(InterActivity.this,
codelist);
// AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
// animationAdapter.setAbsListView(listview);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
//listview.setOnItemClickListener(InterActivity.this);
//listview.setOnItemLongClickListener(InterActivity.this);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) { // TODO Auto-generated method stub
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
}
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter = new FinalAdapter(InterActivity.this,
codelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
您不应在每次加载更多数据时重新创建列表和适配器。用这个替换 LoadMore class:
private class Loadmore extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(InterActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("load More Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist.clear();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
query.setLimit(limit += 15);
ob = query.find();
for (ParseObject inter : ob) {
ParseFile listpic = (ParseFile) inter.get("alphabetimg");
ParseFile levelpic = (ParseFile) inter.get("levelimg");
ParseFile apipic = (ParseFile) inter.get("apiimg");
ParseFile descpicone = (ParseFile) inter.get("descimgone");
ParseFile descpictwo = (ParseFile) inter.get("descimgtwo");
ParseFile videopic = (ParseFile) inter.get("videoimg");
ParseFile hashtagpic = (ParseFile) inter.get("hashimg");
ParseFile video = (ParseFile) inter.get("demovideo");
// ParseFile downloadfile = (ParseFile) inter.get("download");
CodeList map = new CodeList();
map.setIntroduction((String) inter.get("intro"));
map.setFinalCodeText((String) inter.get("codetext"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int position = listview.getLastVisiblePosition();
adapter.notifyDataSetChanged();
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
告诉我:)
尝试使用 OnTouchListener
而不是 OnScrollListener
(这将帮助您更轻松地检测滚动方向并做出相应反应):
listView.setOnTouchListener(new View.OnTouchListener() {
float height;
@Override
public boolean onTouch(View v, MotionEvent event) {
float height = event.getY();
int action = event.getAction();
int threshold = 1;
int count = listview.getCount();
if(action == MotionEvent.ACTION_DOWN){
// do nothing
this.height = height;
}else if(action == MotionEvent.ACTION_UP){
if(this.height < height){
Log.v(TAG, "Scrolled up");
// do nothing - user is going up
}else if(this.height > height){
Log.v(TAG, "Scrolled down");
//execute LoadMore Task
if (listview.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMoreDataTask AsyncTask
new Loadmore().execute();
}
}
}
return false;
}
});