1

我使用了自定义 ListView 并使用该 ListView 显示了一些数据。当我单击列表视图项时,onLongClickListener 没有触发。我无法选择任何列表项。

视图.xml:

    <?xml version="1.0" encoding="utf-8"?>



<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <TextView 
        android:id="@+id/jokeTextView"
        android:text="This is a really long sample joke that should be ellipsisized in collapsed mode and show the complete text in the expanded mode."
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textColor="@color/dark"
        android:paddingTop="10dip"
        android:paddingBottom="10dip"
        android:paddingLeft="20dip" 
        android:paddingRight="50dip"
        android:layout_alignParentRight="true"  
        android:focusable="false"       
        android:focusableInTouchMode="false"
            >
    </TextView>

        <RadioGroup
            android:id="@+id/ratingRadioGroup"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical"
            android:layout_alignParentRight="true"          
             >

            <RadioButton
                android:id="@+id/likeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/like"
                android:button="@null"
                android:focusable="false"
                android:focusableInTouchMode="false"
                 />

            <RadioButton
                android:id="@+id/dislikeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/dislike"
                android:button="@null"
                android:focusable="false"
                android:focusableInTouchMode="false"
                />
        </RadioGroup>

    </RelativeLayout>
</RelativeLayout>

活动课:

  package edu.calpoly.android.lab3;

import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import edu.calpoly.android.lab3.Joke;
import edu.calpoly.android.lab3.R;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class AdvancedJokeList extends SherlockActivity {

    private static final String TAG = "MyActivity";

    /** Contains the name of the Author for the jokes. */
    protected String m_strAuthorName;

    /** Contains the list of Jokes the Activity will present to the user. */
    protected ArrayList<Joke> m_arrJokeList;

    /** Contains the list of filtered Jokes the Activity will present to the user. */
    protected ArrayList<Joke> m_arrFilteredJokeList;

    /** Adapter used to bind an AdapterView to List of Jokes. */
    protected JokeListAdapter m_jokeAdapter;

    /** ViewGroup used for maintaining a list of Views that each display Jokes. */
    protected ListView m_vwJokeLayout;

    /** EditText used for entering text for a new Joke to be added to m_arrJokeList. */
    protected EditText m_vwJokeEditText;

    protected ActionMode mActionMode=null;
    /** Button used for creating and adding a new Joke to m_arrJokeList using the
     *  text entered in m_vwJokeEditText. */
    protected Button m_vwJokeButton;

    //protected TextView m_vwJokeTextView;
    /** Menu used for filtering Jokes. */
    protected Menu m_vwMenu;

    /** Background Color values used for alternating between light and dark rows
     *  of Jokes. Add a third for text color if necessary. */
    protected int m_nDarkColor;
    protected int m_nLightColor;
    protected int m_nTextColor;

    /**
     * Context-Menu MenuItem IDs.
     * IMPORTANT: You must use these when creating your MenuItems or the tests
     * used to grade your submission will fail. These are commented out for now.
     */
    //protected static final int FILTER = Menu.FIRST;
    //protected static final int FILTER_LIKE = SubMenu.FIRST;
    //protected static final int FILTER_DISLIKE = SubMenu.FIRST + 1;
    //protected static final int FILTER_UNRATED = SubMenu.FIRST + 2;
    //protected static final int FILTER_SHOW_ALL = SubMenu.FIRST + 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initLayout();
        Resources res = this.getResources();
        m_arrJokeList = new ArrayList<Joke>();
        m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrJokeList);
        m_vwJokeLayout.setAdapter(m_jokeAdapter);

        String author = res.getString(R.string.author_name);
        String[] jokeString = res.getStringArray(R.array.jokeList);
        for (int i=0; i<jokeString.length; i++) {
            addJoke(new Joke(jokeString[i],author));
        }
        initAddJokeListeners();
        //m_vwJokeLayout.setLongClickable(true);
        //m_vwJokeLayout.setFocusable(false);
        m_vwJokeLayout.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(AdvancedJokeList.this, "long click", Toast.LENGTH_LONG).show();

                if (mActionMode != null)
                {

                    return false;


                }
                else
                {

                    mActionMode = AdvancedJokeList.this.startActionMode(mActionModeCallback);

                    v.setSelected(true);
                    return true;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater mi = getSupportMenuInflater();
        mi.inflate(R.menu.mainmenu, menu);
        m_vwMenu = menu;
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem Item) {
        m_arrFilteredJokeList = new ArrayList<Joke>();
        m_arrFilteredJokeList.clear();
        switch (Item.getItemId())
        {
            case R.id.menu_filter :
                //Toast.makeText(this, "Filter Jokes", Toast.LENGTH_LONG).show();
                break;
            case R.id.submenu_like :
                for (Joke i : m_arrJokeList) {
                    if (i.getRating()==1)
                        m_arrFilteredJokeList.add(i);                   
                }
                m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrFilteredJokeList);
                break;
            case R.id.submenu_dislike :
                for (Joke i : m_arrJokeList) {
                    if (i.getRating()==2)
                        m_arrFilteredJokeList.add(i);
                }
                m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrFilteredJokeList);
                //m_jokeAdapter.notifyDataSetChanged();
                break;
            case R.id.submenu_unrated :
                for (Joke i : m_arrJokeList) {
                    if (i.getRating()==0)
                        m_arrFilteredJokeList.add(i);
                }
                m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrFilteredJokeList);
                break;
            case R.id.submenu_show_all :
                m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrJokeList);
                break;
        }       
        m_jokeAdapter.notifyDataSetChanged();
        m_vwJokeLayout.setAdapter(m_jokeAdapter);


        return true;

    }

    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 mi = mode.getMenuInflater();
            mi.inflate(R.menu.actionmenu, menu);

            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId())
            {
                case R.id.menu_remove :

            }
            return false;
        }
    };



    /**
     * Method is used to encapsulate the code that initializes and sets the
     * Layout for this Activity.
     * @param v_mwJokeLayout  
     */
    protected void initLayout() {
        setContentView(R.layout.advanced);
        m_vwJokeLayout = (ListView) findViewById(R.id.jokeListViewGroup);

        m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
        m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);



    }

    /**
     * Method is used to encapsulate the code that initializes and sets the
     * Event Listeners which will respond to requests to "Add" a new Joke to the
     * list.
     */
    protected void initAddJokeListeners() {
        m_vwJokeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String textInput = m_vwJokeEditText.getText().toString();
                m_vwJokeEditText.setText("");
                addJoke(new Joke(textInput,""));

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0);


            }
        });
    }

    /**
     * Method used for encapsulating the logic necessary to properly add a new
     * Joke to m_arrJokeList, and display it on screen.
     * 
     * @param joke
     *            The Joke to add to list of Jokes.
     */
    protected void addJoke(Joke joke) {
        //Joke joke = new Joke(strJoke);
        m_arrJokeList.add(joke);
        m_jokeAdapter.notifyDataSetChanged();
        Log.d(TAG, "Adding new joke:" + joke.getJoke());

        //setContentView(m_vwJokeLayout);

    }
}
4

2 回答 2

5

尝试这个

m_vwJokeLayout.setLongClickable(true);

m_vwJokeLayout.setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return true
    }
});)
于 2013-07-29T03:41:25.783 回答
1

您必须将其标记为 longclickable :)。

m_jokeAdapter = new JokeListAdapter(getApplicationContext(), m_arrJokeList);
m_vwJokeLayout.setAdapter(m_jokeAdapter);    
m_vwJokeLayout.setLongClickable(true);
于 2013-07-29T02:38:40.180 回答