在Scrolling issue of android中需要帮助

时间:2012-07-19 09:08:35

标签: android listview

我正在编辑listview的滚动。 我想在listview的滚动中加载数据,我这样做:

    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        } else {
            if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + threshold)) {
                new loadMoreListView().execute();

                int currentPosition = lv.getFirstVisiblePosition();
                View v = lv.getChildAt(0);
                int distFromTop = (v == null) ? 0 : v.getTop();
                lv.setSelectionFromTop(currentPosition, distFromTop);
            }
        }
    } 

loadMoreListView是使用AsynTask加载数据的类。 我的问题是,当我来到第10个位置时,listview开始按预期加载数据,但它会冻结整个列表视图。我无法与listview交互,直到数据加载完毕。有没有解决方案来加载数据并同时与listview交互。 请不要建议Endless Adapter或Prime。我已经尝试过那个。 任何帮助都非常感谢。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

public class LListviewsActivity extends Activity {
 ListView myList;
 Button getChoice;
 String[] listContent = {
        "January",
        "February",
         "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
         "October",
        "November",
        "December"
};

/** Called when the activity is first created. */
 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myList = (ListView)findViewById(R.id.list);
     getChoice = (Button)findViewById(R.id.getchoice);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_list_item_multiple_choice,listContent);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(adapter);
    getChoice.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
           // TODO Auto-generated method stub
                       String selected = "";
              int cntChoice = myList.getCount();
              SparseBooleanArray sparseBooleanArray =              
     myList.getCheckedItemPositions();
           for(int i = 0; i < cntChoice; i++){
               if(sparseBooleanArray.get(i)) {
                   selected +=                      myList.getItemAtPosition(i).toString() + "\n";
               }
           } Toast.makeText(LListviewsActivity.this, selected          ,Toast.LENGTH_LONG).show();
         }
      });
  }
   }

答案 1 :(得分:0)

试试这个,它有用吗

list_event.setOnScrollListener(new AbsListView.OnScrollListener() {

        @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) {
            if (scrollState != 0)
                eventAdapter.isScrollingEvent = true;
            else {
                eventAdapter.isScrollingEvent = false;
                eventAdapter.notifyDataSetChanged();
            }
        }
    });

写boolean isScrollingEvent = true
你的适配器是liake eventAdapter

检查此链接是否非常实用 List View Scroll Not Smooth