1

我有一个 PullToRefreshListView - 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;
        }
   });

但它不起作用。为什么?

4

2 回答 2

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;
    }
});

希望它有所帮助,你明白了......干杯!

于 2013-07-17T10:15:04.620 回答
0

我有这个错误并保存了它。

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

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

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

});

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

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;

}

于 2014-08-29T01:47:56.473 回答