【问题标题】:OnLongClickListener - not getting fired + androidOnLongClickListener - 没有被解雇 + android
【发布时间】:2014-04-12 19:08:24
【问题描述】:

我有这个OnLongClickListener,它没有被解雇。我想我已经正确设置了所有内容,并且之前使用过上下文菜单,但现在它没有触发,日志甚至没有给我任何错误。

我真的希望你能给我任何想法。这可能是一个愚蠢的错误。

package fragments;

import interfaces.AsyncTaskCompleteListener;

import java.util.List;

import com.example.slidingmenu.R;

import dialog.EditDialog;

import services.GetRequest;

import model.Post;

import adapter.PostAdapter;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class HomeFragment extends Fragment implements AsyncTaskCompleteListener, View.OnLongClickListener {
    private ListView mainListView;  
    private GetRequest service;
    private ProgressDialog dialog;
    private ActionMode mActionMode;
    public HomeFragment() {}

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedIntanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        mainListView = (ListView) rootView.findViewById(R.id.mainListView);
        mainListView.setOnLongClickListener(this);
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Please wait...");    
        service = new GetRequest(this);
        service.execute();
        dialog.show();
        registerForContextMenu(mainListView);
        return rootView;
    }   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null; 
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()) {
                case R.id.edit:
                    Log.d("edit", "pressed");
                    FragmentManager fragmentManager = getFragmentManager();
                    EditDialog editDialog = new EditDialog();
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.add(android.R.id.content, editDialog).addToBackStack(null).commit();
                    mode.finish();
                    return true;

                default:
                    return false;
            }
        }
    };

    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getActivity(), "PRESSED", Toast.LENGTH_SHORT).show();
        Log.d("hey", "pressed");
        if(mActionMode != null) {
            return false;
        }   
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        mainListView.setSelected(true);
        return true;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            Toast.makeText(getActivity(), "Refresh!", Toast.LENGTH_SHORT).show();
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Please wait while refreshing...");
            dialog.show();
            service = new GetRequest(this);
            service.execute();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTaskComplete(List<Post> posts) {
        PostAdapter adapter = new PostAdapter(getActivity(), R.layout.listview_item_row, posts);
        LayoutInflater inflater = (LayoutInflater)getActivity().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header = (View)inflater.inflate(R.layout.listview_header_row, null);
        if(mainListView.getHeaderViewsCount() == 0) {
            mainListView.addHeaderView(header);
        }
        mainListView.setAdapter(adapter);
        if(dialog.isShowing()) {
            dialog.dismiss();
        }       
    }

    @Override
    public void onTaskComplete(String result) {
        // TODO Auto-generated method stub

    }
}

这是我的 ListView 所在的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainListView"
        android:longClickable="true"
        android:clickable="true"
        ></ListView>

</LinearLayout>

【问题讨论】:

    标签: java android listview onlongclicklistener android-actionmode


    【解决方案1】:

    您正在将侦听器附加到整个视图而不是每一行。我不确定为什么什么都没有被触发,但是这些触摸事件可能被其他视图拦截了。

    您应该使用ListView.setOnItemLongClickListener(),而不是ListView.setOnLongClickListener()

    Here is the link for the Android Docs for this method.

    【讨论】:

    • 这行得通。 :) 但现在我遇到了一个全新的问题。如果我自己不解决,我会发布一个新问题。
    【解决方案2】:

    因为您已经为列表视图 registerForContextMenu。不再需要 setOnLongClickListener。您需要的是 onCreateContextMenu 方法和 onContextItemSelected。

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
        // create menu item here
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item){
        // do your long click listener here
    }
    

    【讨论】:

    • 我已经读到,如果我想为 3.0 及更高版本构建,我应该使用 ActionMode 上下文菜单。
    • 我还在我的代码中删除了该行。 :) 忘了它还在那儿。
    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多