为什么长时间点击PullToRefreshListView不起作用?

时间:2013-07-17 09:30:17

标签: java android

我有一个Pull To Refresh ListView - https://github.com/chrisbanes/Android-PullToRefresh

我需要长按一下。我的代码:

pullToRefreshView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
    pullToRefreshView.setAdapter(adapter);
    pullToRefreshView.setLongClickable(true);
pullToRefreshView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            Log.d("privet ia long click", "yes");
            return true;
        }
   });

但它不起作用。为什么呢?

2 个答案:

答案 0 :(得分:1)

检查与此库一起给出的示例并检查以下代码(此代码仅来自此库的示例代码):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pulltorefresh);

    mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

    // Set a listener to be invoked when the list should be refreshed.
    mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

            // Update the LastUpdatedLabel
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });
    // Add an end-of-list listener
    mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

        @Override
        public void onLastItemVisible() {
            Toast.makeText(PullToRefresh.this, "End of List!", Toast.LENGTH_SHORT).show();
        }
    });

    ListView actualListView = mPullRefreshListView.getRefreshableView();

    // Need to use the Actual ListView when registering for Context Menu
    registerForContextMenu(actualListView);

    mListItems = new LinkedList<String>();
    mListItems.addAll(Arrays.asList(mStrings));

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

    /**
     * Add Sound Event Listener
     */
    SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
    soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
    soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
    soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
    mPullRefreshListView.setOnPullEventListener(soundListener);

    // You can also just use setListAdapter(mAdapter) or
    // mPullRefreshListView.setAdapter(mAdapter)
    actualListView.setAdapter(mAdapter);
}

在上面的代码列表中使用如下

ListView actualListView = mPullRefreshListView.getRefreshableView();

所以在恭维之后你可以在上面使用longpresslistener ..

actualListView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Log.d("privet ia long click", "yes");
        return true;
    }
});

希望它有所帮助,你得到了......干杯!

答案 1 :(得分:0)

我遇到了这个错误并保存了它。

首先我使用此代码但没有工作。

mPullRefreshListView.getRefreshableView().setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
    return true;
}

});

因为长按方法是View的方法,当您长按listview项目时,只需单击列表视图项目视图,就需要将长按侦听器设置为适配器中的项目视图。 jsut是这样的:

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
  holder = new ViewHolder();
  convertView = mInflater.inflate(R.layout.article_list_item, parent, false);
  //your long click is here.  
  convertView.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
      return false;
    }
  });

return convertView;

}