ListView项目适配器无法单击

时间:2018-09-18 20:55:00

标签: android xml android-layout listview adapter

我无法单击列表视图中的项目,所有这些突然发生。我已经为此工作了一个星期,但是在此之前,它完全没有问题。我可以单击一个项目。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getActivity(), list.get(i).getPassword(), Toast.LENGTH_SHORT).show();
        }
});

CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.cardview_layout_locked, list);                            
listView.setAdapter(adapter);

这是我的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:descendantFocusability="blocksDescendants"
tools:context=".LobbyFragment">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView
        android:id="@+id/lobbieslistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />
</RelativeLayout>
</FrameLayout>

这是我的customlistadapter

public class CustomListAdapter extends ArrayAdapter<cardviewTest> {

private static final String TAG = "CustomListAdapter";

private Context mContext;
private int mResource;
private int lastPosition = -1;

/**
 * Holds variables in a View
 */
private static class ViewHolder {
    TextView lobbyTextView, organizerTextView;
    ImageView image;
}

/**
 * Default constructor for the PersonListAdapter
 * @param context
 * @param resource
 * @param objects
 */

public CustomListAdapter(Context context, int resource, ArrayList<cardviewTest> objects) {
    super(context, resource, objects);

    mContext = context;
    //mResource = resource;
}

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

    //sets up the image loader library
    setupImageLoader();

    //get the persons information
    String lobbyname = getItem(position).getLobbyname();
    String organizer = getItem(position).getOrganizer();
    String imgUrl = getItem(position).getImageURL();
    String password = getItem(position).getPassword();

    try{


        //create the view result for showing the animation
        final View result;

        //ViewHolder object
        ViewHolder holder;

        if(convertView == null){

            if(!password.equals("") || password!=null){ this.mResource = R.layout.cardview_layout_locked; }
            if(password.equals("") || password==null){ this.mResource = R.layout.cardview_layout; }

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder= new ViewHolder();
            holder.organizerTextView = (TextView) convertView.findViewById(R.id.organizerTV);
            holder.lobbyTextView = (TextView) convertView.findViewById(R.id.lobbynameTV);
            holder.image = (ImageView) convertView.findViewById(R.id.thumbnail);

            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }


        Animation animation = AnimationUtils.loadAnimation(mContext,
                (position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
        result.startAnimation(animation);
        lastPosition = position;

        holder.organizerTextView.setText(organizer);
        holder.lobbyTextView.setText(lobbyname);

        //create the imageloader object
        ImageLoader imageLoader = ImageLoader.getInstance();

        int defaultImage = mContext.getResources().getIdentifier("@drawable/image_failed",null,mContext.getPackageName());

        //create display options
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true)
                .showImageForEmptyUri(defaultImage)
                .showImageOnFail(defaultImage)
                .showImageOnLoading(defaultImage).build();

        //download and display image from url
        imageLoader.displayImage(imgUrl, holder.image, options);

        return convertView;
    }catch (IllegalArgumentException e){
        Log.e(TAG, "getView: IllegalArgumentException: " + e.getMessage() );
        return convertView;
    }
}

/**
 * Required for setting up the Universal Image loader Library
 */
private void setupImageLoader(){
    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            mContext)
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);
    // END - UNIVERSAL IMAGE LOADER SETUP
}
}

我完全不知道为什么我无法再单击项目。所有这些都是突然发生的。就在一个月前,这是可行的,然后,发生了

2 个答案:

答案 0 :(得分:0)

在代码中未发现错误,请检查CustomListAdapter类文件以查看是否禁用了该项目。

已编辑 注意:

//download and display image from url
imageLoader.displayImage(imgUrl, holder.image, options);

以上行非常耗时,这会阻塞UI线程,请不要将它们放在CustomListAdapter中。

您可以创建一个界面。单击listview item时触发接口回调,并处理回调中的事件。有关自定义适配器的示例很多,请谷歌搜索。

答案 1 :(得分:0)

首先,我先设置Adapter,然后再处理ListView项目上的点击:

CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.cardview_layout_locked, list);                            
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getActivity(), list.get(i).getPassword(), Toast.LENGTH_SHORT).show();
        }
});

另外,尝试将这两个设置为true;

android:focusable=“true”
android:focusableInTouchMode=“true”

那我希望它能工作。


此外,现在考虑使用RecyclerView