Android - 仅OnItemClickListener *有时*无效

时间:2016-02-27 19:26:05

标签: android listview

我的一项活动中有一个ListView,我使用自定义ArrayList绑定到ArrayAdapter。我已将OnItemClickListener设置为ListView,该ListView应调用启动其他活动的方法。但是,我发现当我点击isEnabled项时,只有有时有效。有时它会开始活动;其他时候它似乎检测到咔嗒声(涟漪效果出现在列表项上)但什么也没做;其他时候它似乎甚至没有检测到咔嗒声(涟漪效应不会出现)。

我已经尝试了我遇到的所有常见建议:在父视图项上阻止后代,在项目视图的所有组件上设置clickable并将其设置为false,设置{{1}在自定义适配器等中返回true,但行为保持不变。任何帮助赞赏。以下是相关代码:

包含ListView的活动:

    public class ViewCollectionActivity extends AppCompatActivity {

    private final String className = this.getClass().getSimpleName();
    private CollectionHandler collectionHandler;
    private Context context;
    private ArrayList<Game> displayedCollection;
    private GameCollectionAdapter collectionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_collection);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        context = this;

        collectionHandler = CollectionHandler.getInstance(this);

        TextView view = null;
        if (collectionHandler.getDisplayedCollection().size() > 0) {
            view = (TextView) findViewById(R.id.no_items_textview);
            view.setVisibility(View.GONE);
        }
        String currentDate = collectionHandler.getDateLastSynchronised();

        view = (TextView) findViewById(R.id.last_updated_textview);
        view.setText("Last synchronised: " + currentDate + "   Total games: " + String.valueOf(collectionHandler.getDisplayedCollection().size()));

        collectionAdapter = collectionHandler.getCollectionAdapter();
        ListView listView = (ListView) findViewById(R.id.collection_list_view);
        listView.setAdapter(collectionAdapter);
        AdapterView.OnItemClickListener collectionItemClickListener = new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                launchGameDetailsActivity(position);
            }
        };
        listView.setOnItemClickListener(collectionItemClickListener);

    }

    public void launchGameDetailsActivity(int position){
        Log.d(className,"Starting lauchGameDetailsActivity method");
        collectionHandler.setSelectedGame(position);
        Intent intent = new Intent(this,ViewGameDetailsActivity.class);
        startActivity(intent);
        Log.d(className, "Ending lauchGameDetailsActivity method");
    }

活动的XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bleachedlizard.ludome.viewcollection.ViewCollectionActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Synchronise Collection"
        android:onClick="synchroniseCollection"/>

    <TextView
        android:id="@+id/last_updated_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Last synchronised: "
        android:textAlignment="center"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Display Collection"
        android:visibility="gone"
        android:onClick="displayCollection"/>

    <ListView
        android:id="@+id/collection_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


    </ListView>
    <TextView
        android:id="@+id/no_items_textview"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="You have no items in your collection."
        android:textAlignment="center"
        android:textSize="20sp"/>

</LinearLayout>

项目视图的XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/collection_item_layout"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:orientation="horizontal"
    android:clickable="false"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <ImageView
        android:id="@+id/collection_item_image"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/testimage"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        />

    <TextView
        android:id="@+id/collection_item_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:padding="16dp"
        android:singleLine="false"
        android:textColor="@android:color/darker_gray"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:textIsSelectable="false"/>

    <TextView
        android:id="@+id/collection_item_plays"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:padding="8dp"
        android:textColor="@android:color/darker_gray"
        android:text="Plays: 0"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:textIsSelectable="false"/>

</LinearLayout>

自定义适配器的代码:

    public class GameCollectionAdapter extends ArrayAdapter<Game> {

    private ArrayList<Game> collection;

    public GameCollectionAdapter(Context context, int resource, ArrayList<Game> collection){
        super(context, resource, collection);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout gameView = (LinearLayout) convertView;
        LayoutInflater mInflater = LayoutInflater.from(getContext());

        if (gameView == null) {
            gameView = (LinearLayout) mInflater.inflate(R.layout.collection_item_view, null);

        }

        //Game game = collection.get(position);
        Game game = super.getItem(position);


        if (game != null) {

            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.

            TextView gameTitle = (TextView) gameView.findViewById(R.id.collection_item_name);
            TextView numOfPlays = (TextView) gameView.findViewById(R.id.collection_item_plays);
            ImageView thumbnail = (ImageView) gameView.findViewById(R.id.collection_item_image);

            // check to see if each individual textview is null.
            // if not, assign some text!
            if (gameTitle != null){
                gameTitle.setText(game.getTitle());
            }
            if (numOfPlays != null){
                numOfPlays.setText("Plays: " + String.valueOf(game.getNumOfPlays()));
            }
            if (thumbnail != null){
                thumbnail.setImageBitmap(game.getThumbnail());
            }
        }

        // the view must be returned to our activity
        return gameView;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

我认为问题是由于项目选择的位置,只要你点击你有一个列表位置传递给你的方法launchGameDetailActivity(int position)检查日志或吐司项目点击你得到的所有位置做必要的。

如果有帮助,这是我的代码尝试这样。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(RecipeClass.this, "Position is" + position, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RecipeClass.this, RecipeIngredients.class)
            intent.putExtra("position", position);
            startActivity(intent);
        }

检查你的arraylist值是否为空。

答案 1 :(得分:1)

我发现导致问题的原因是:我设置支持ListView的数组的方式意味着它一直在为数组中的每个元素下载和存储Bitmaps。一旦我更改了实现,以便它只按ListView要求下载图像,那么这似乎提高了性能并且onClickListener开始正常工作。

我使用的实现与此处显示的完全相同:

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html