我在listview中实现了无限的scrool。我已经实现了onScrollListner
来实现它。这就是我的代码。
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// TODO Auto-generated method stub
if (lv_connectionlist.getLastVisiblePosition() == lv_connectionlist.getAdapter().getCount() -1 &&
lv_connectionlist.getChildAt(lv_connectionlist.getChildCount() - 1).getBottom() <= lv_connectionlist.getHeight())
{
if (scrollState == SCROLL_STATE_IDLE) {
Constants.page=String.valueOf((Integer.parseInt(Constants.page)+Integer.parseInt(Constants.count)));
if(Integer.parseInt(totconn)>Integer.parseInt(Constants.page)){
//myList = new ArrayList<ConnectionListData>();
ConnectionListTask cnListtask = new ConnectionListTask();
cnListtask.execute("");
//lv_connectionlist.setSelection(Integer.parseInt(Constants.page.trim()));
lv_connectionlist.smoothScrollToPosition(Integer.parseInt(Constants.page.trim()));
}else{
Constants.page=String.valueOf((Integer.parseInt(Constants.page)-Integer.parseInt(Constants.count)));
}
}
}
}
值Constants.page最初为零,我将它递增5.问题是每当我滚动我的列表视图,显示5个元素后,列表视图再次从顶部显示。它完美地绑定下一个5元素,但我想从最后一项显示它。任何帮助??
这是listview的适配器。
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ConnectionsListAdapters extends BaseAdapter{
ArrayList<ConnectionListData> myList = new ArrayList<ConnectionListData>();
LayoutInflater inflater;
Context context;
RelativeLayout.LayoutParams llps = null;
ImageView ivArrow = null;
BaseActivity mBaseAct = null;
public ImageLoader imageLoader;
public ConnectionsListAdapters(Context context, ArrayList<ConnectionListData> myList) {
mBaseAct = new BaseActivity();
this.myList = myList;
this.context = context;
imageLoader = new ImageLoader(this.context);
inflater = LayoutInflater.from(this.context); // only context can also be used
}
@Override
public int getCount() {
return myList.size();
}
@Override
public ConnectionListData getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.connectionlist, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
((TextView) convertView.findViewById(R.id.favouritetext)).setTag(myList.get(position).getMemId());
((TextView) convertView.findViewById(R.id.connecttext)).setTag(myList.get(position).getMemId());
mViewHolder.tvmemname=detail(convertView,R.id.tvmemname,myList.get(position).getname());
mViewHolder.tvcompname=detail(convertView, R.id.tvcompname,myList.get(position).getcompanyname());
mViewHolder.tvdescription=detail(convertView, R.id.tvdescription,myList.get(position).getdescription());
mViewHolder.memimage=detail_file_name(convertView, R.id.memimage, myList.get(position).getmemimg());
ImageView image = mViewHolder.memimage;
imageLoader.DisplayImage(Constants.memimageurl+"/"+myList.get(position).getmemimg(),image);
return convertView;
}
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText((text.length()>18)?(text.substring(0, 18)+"..."):text);
return tv;
}
private ImageView detail_file_name(View v, int resId, String icon) {
ImageView iv = (ImageView) v.findViewById(resId);
//iv.setImageURI(uri); //
return iv;
}
private class MyViewHolder {
TextView tvmemname = null;
TextView tvcompname = null;
TextView tvdescription = null;
ImageView memimage = null;
}
}
答案 0 :(得分:0)
得到解决方案:
cadapter=new ConnectionsListAdapters(context, myList);
lv_connectionlist.setAdapter(cadapter);
((ConnectionsListAdapters)lv_connectionlist.getAdapter()).notifyDataSetChanged();
lv_connectionlist.setSelection(Integer.parseInt(Constants.page.trim()));
在将适配器绑定到listview之后,我已经将setSelection
方法放在 asyncTask 的onPostExecute()
内。它就像魅力一样:)