使列表可点击 [英] Make list clickable

查看:93
本文介绍了使列表可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过使用可点击和可聚焦的方法,但是似乎没有什么可以使列表项可点击的.

I've already tried using clickable and focusable but nothing seems to allow list items to be clickable.

xml文件

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

<Button
    android:id="@+id/add_recipe_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/recipe_activity_add"
    android:background="@color/colorPrimary"
    android:textColor="#fff"/>

<ListView
    android:id="@+id/recipe_list"
    android:layout_width="match_parent"
    android:focusable="false"
    android:clickable="true"
    android:layout_height="match_parent" />

java代码

recipeListView = (ListView)findViewById(R.id.recipe_list);


    //make viewRecipe Work
    recipeListView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("MainActivity", "ListView item clicked.");
        }
    });

推荐答案

当您在Listview行的子项上设置onClickListner时,尝试在Listview项目上设置setOnItemClickListner时,Android有时会显示奇怪的行为.尝试检查您的适配器"类,并确保一切正常,并根据标准进行了良好的管理.使用ViewHolder访问Adapter类中的子项. 这是Adapter的示例类.使用这个,希望它能起作用.

Android sometimes shows a weird behavior when you are trying to setOnItemClickListner on Listview item while you have set onClickListner on the sub-items of Listview row. Try to check you "Adapter" class and make sure everything is fine and well managed according to the standards. Use ViewHolder to access the sub-items in Adapter class. Here is the sample class of Adapter. Use this one, hope it'll work.

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.TextView;



import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

 /**
 * Created by Zohaib Hassan on 11/28/2016.
 */

 public class InboxAdapter extends ArrayAdapter<InboxRow> {

ArrayList<InboxRow> items;
Context context;


public InboxAdapter(Context context, int resource, ArrayList<InboxRow> items) {
    super(context , resource , items);
    this.context = context;
    this.items = items;

}



@Override

public View getView(int position, View convertView, ViewGroup parent) {

    // Get the data item for this position

    InboxRow rowItem = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view

    ViewHolder viewHolder; // view lookup cache stored in tag

    if (convertView == null) {

        viewHolder = new ViewHolder();

        LayoutInflater inflater = LayoutInflater.from(context);

        convertView = inflater.inflate(R.layout.inbox_row, null);

        viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_user_name_inbox);
        viewHolder.tvMessage = (TextView) convertView.findViewById(R.id.tv_message_inbox);
        viewHolder.tvTimeCount = (TextView) convertView.findViewById(R.id.tv_time_count_inbox);
        viewHolder.userProfilePic = (CircleImageView) convertView.findViewById(R.id.inbox_profile_image);

        convertView.setTag(viewHolder);

    } else {

        viewHolder = (ViewHolder) convertView.getTag();

    }


    /*CircleImageView ivProfileImage Set Background with Picasso*/

    return convertView;

}

private static class ViewHolder {

    TextView tvUserName , tvMessage , tvTimeCount;
    CircleImageView userProfilePic;

}

}

最后一件事,请确保布局类中的"ListView"高度和宽度均设置为"match-parent".

One last thing, please make sure the the "ListView" in your layout class is set to "match-parent" for both height and width.

这篇关于使列表可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆