gpt4 book ai didi

Android ListView 在更新其数据时重置滚动状态

转载 作者:行者123 更新时间:2023-11-30 01:12:31 25 4
gpt4 key购买 nike

我尝试根据 ListView 滚动来更新数据。数据更新的很好。但是在更新数据之后,listview 就到了顶部。我需要保持不变。我从 stockoverflow 尝试了很多。没有任何帮助。我想我的代码有误。请帮我解决。

我的代码是:

public class ViewAll extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all);

new GetMessage().execute();

FinalMessage = new ArrayList<HashMap<String, String>>();

ProgressBar footer = new ProgressBar(this);
ListView lv = (ListView) findViewById(listView);
lv.addFooterView(footer);
}

private void getMessageFromUrl()
{

new GetMessage().execute();
}

private class GetMessage extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
......
}

@Override
protected Void doInBackground(Void... arg0)
{
ServiceHandler sh = new ServiceHandler();
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("page",String.valueOf(page)));
String jsonStr = sh.makeServiceCall(url, ServiceHandler.POST, nameValuePairs);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
......
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else
{
Log.e("SericeHandler","Couldn't get any data from url");
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);


runOnUiThread(new Runnable() {
public void run() {

if(dorefresh==1)
{
ListView lv = (ListView) findViewById(listView);
Customlistadapter adapter = new Customlistadapter(ViewAll.this, title1, msg1);
lv.setAdapter(adapter);
int index = lv.getFirstVisiblePosition();
View v = lv.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
lv.setSelectionFromTop(index, top);

page = page + 1;
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
int currentVisibleItemCount;
int currentScrollState;
int currentFirstVisibleItem;
int totalItem;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
this.currentScrollState = scrollState;
this.isScrollCompleted();
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;
}

private void isScrollCompleted() {

if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
getMessageFromUrl();
}
}
});
Toast.makeText(ViewAll.this, "Updated", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(ViewAll.this, "No More Messages", Toast.LENGTH_SHORT).show();
}



}

});
}

}
}

CustomlistAdapter.java

public class Customlistadapter extends ArrayAdapter<String> {
ArrayList<String> title;
ArrayList<String> msg;
Context context;
public Customlistadapter(Activity context, ArrayList<String> text, ArrayList<String> msgView) {
super(context, R.layout.list_row, text);
// TODO Auto-generated constructor stub
this.title = text;
this.msg = msgView;
this.context = context;
}

public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View single_row = inflater.inflate(R.layout.list_row, null,
true);
TextView textView = (TextView) single_row.findViewById(R.id.title);
TextView msgView = (TextView) single_row.findViewById(R.id.msgView);
textView.setText(title.get(position));
msgView.setText(msg.get(position));

return single_row;
}

}

最佳答案

您的问题是每次更新数据时都会创建一个新的 Customlistadapter。如果您希望 ListView 保持其滚动状态,则必须重用适配器。只需更改适配器中的数据并调用 myAdapter.notifyDataSetChanged()

例如如果您的适配器是 ArrayAdapter,您可以这样做:

ArrayAdapter adapter = (ArrayAdapter)listView.getAdapter();
adapter.clear();
adapter.addAll(newData);
adapter.notifyDataSetChanged();

关于Android ListView 在更新其数据时重置滚动状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285928/

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