2014-01-16 2 views
0

OnItemClickListener에 어려움이 있습니다. OnScrollListener가있을 때 onItemClick이 호출되지 않습니다. 나는 아래 문제를 좁힐 :이 절차에서 내가 adapter.notifyDataSetChanged()를 호출하면OnItemClickListener가 작동하지 않습니다 ListView의 icm OnScrollListener

public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) 

OnItemClickListener가 작동하지 않습니다. 아무 이유 나 제안, 또는이 문제를 해결하는 방법? onScroll과 onScrollStateChanged 모두에서 중단 점을 사용하지만 손으로 ​​만져도 호출되지는 않습니다.

public class ArticleListActivity extends Activity implements AdapterView.OnItemClickListener, AbsListView.OnScrollListener { 

    private long categoryId = 0; 
    private List<Article> objects; 
    private ArticleListAdapter adapter; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.article_list); 

     Bundle extras = getIntent().getExtras(); 
     if(extras != null){ 
      if(extras.containsKey("categoryId")) categoryId = extras.getLong("categoryId"); 
     } 

     ListView lv = (ListView) findViewById(R.id.article_list_activity); 
     iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext()); 

     try { 
      objects = app.getDataManager().getArticlesForCategory(categoryId, 15); 
      adapter = new ArticleListAdapter(this, R.layout.article_list_cell, objects, categoryId); 
      lv.setOnScrollListener(this); 
      lv.setOnItemClickListener(this); 
      lv.setAdapter(adapter); 
     } catch (Exception e){ 
      Log.e(iDomsAndroidApp.TAG, "Error " + e.getMessage(), e); 
     } 


    } 

    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
     // When clicked, show a toast with the TextView text 
     Toast.makeText(getApplicationContext(), objects.get(position).getTitle(), 
       Toast.LENGTH_SHORT).show(); 


     Intent intent = new Intent(view.getContext(), ArticleActivity.class); 
     intent.putExtra("articleId", objects.get(position).getId()); 
     startActivity(intent); 


    } 

    public void onScrollStateChanged(AbsListView absListView, int i) { 

    } 

    public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) { 
     if(totalCount == 0) return; 

     // For the moment only add, not remove items out of view. Buffer of 5. 
     int buffer = 5; 

     if(adapter.getCount() + buffer < firstVisible + visibleCount){ 
      Log.d(iDomsAndroidApp.TAG, adapter.getCount() + " + " + buffer + " < " + firstVisible + visibleCount); 
      // Get next set 
      try { 
       iDomsAndroidApp app = ((iDomsAndroidApp) adapter.getContext().getApplicationContext()); 
       List<Article> newObjects = app.getDataManager().getArticlesForCategory(categoryId, adapter.getCount(), totalCount + buffer - adapter.getCount()); 
       for(Article item : newObjects){ 
        adapter.add(item); 
       } 
      } catch (Exception e){ 
       Log.e(iDomsAndroidApp.TAG, "Error " + e.getMessage(), e); 
      } 
     } 

     adapter.notifyDataSetChanged(); 
    } 

} 

답변

0

나는 돈, t는 이유를 알고 있지만, 나는 (다른 성능 향상으로) 업데이트 루프에 adapter.notifyDataSetChanged()를 이동하여 문제를 해결했다. 현명한 성능이라면 어쨌든 말이 될 것입니다. 그렇지 않으면 뷰의 각 이동에 의해 호출 될 것이므로 필요하지 않습니다. 그것은 그 문제를 해결했습니다.

관련 문제