OnItemCLickListener在ListView自定义适配器中不起作用 [英] OnItemCLickListener not working in ListView custom adapter

查看:103
本文介绍了OnItemCLickListener在ListView自定义适配器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动.一个将所有产品显示在ListView中.选择一个项目时,将运行第二项活动.基本上,第二项活动应在单击项目时启动,并应打开PDF文件.

I have two activities. One displays all the products in a ListView. Second activity runs when an item is selected. Basically second activity should fire up on item click and it should open the PDF file.

好吧,这并不会激发选择项的第二项活动.发布代码以供参考.

Well the thing is that it does not fire up 2nd activity on item select. Posting the code for your reference.

我的.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="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/list_bacg"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/productImage"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="0.25"
        android:adjustViewBounds="true"
        android:padding="5dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.55"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp" >

            <!--
             <TextView 
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="Product Name"
            />
            -->

            <TextView
                android:id="@+id/prodName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Prod Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Price: "
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/storePrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/hori_line"
                android:text="Store Price"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Offer Price: "
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/offerPrice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Offer Price"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

        <RatingBar
            android:id="@+id/ratingbar_prod"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dip" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp" >

            <!--
              <TextView 
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="Category Name"
            />
            -->

            <TextView
                android:id="@+id/storeName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Store Name"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/prodCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pc_edit_b"
            android:inputType="number" />
    </LinearLayout>

</LinearLayout>

这是我要在ListView上设置的自定义视图.

This is custom view which I want to set on ListView.

这是代码文件.

    listView = (ListView)rootView.findViewById(R.id.lv);
    List<StoreProducts> objects = new ArrayList<StoreProducts>();
    storeInfoAdapter = new    StoreInfoAdapter(getActivity(),android.R.layout.simple_list_item_1,objects);
    listView.setAdapter(storeInfoAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Log.e("Click", "Click");
            StoreProducts productDetails = (StoreProducts) parent.getItemAtPosition(position);
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
             fragmentTransaction.setCustomAnimations(R.anim.ani_out,R.anim.ani_in);
            fragmentTransaction.replace(R.id.frame_container,new                ProductDescFragment(productDetails));
            fragmentTransaction.addToBackStack("All Products");
            fragmentTransaction.commit();
         }
       });

推荐答案

向每个自定义布局元素添加 android:focusable ="false" .例如.您的自定义布局元素

Adding android:focusable="false" to each of the custom layout elements. eg. your custom layout element

<EditText 
 android:id="@+id/prodCount"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/pc_edit_b"
 android:inputType="number"
 android:focusable="false" />

这篇关于OnItemCLickListener在ListView自定义适配器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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