ListView的OnItemClick监听器在片段中无法工作

5

我在片段中使用了ListView,但是我使用的列表onItemClick监听器不起作用。以下是我的代码,以及完美解决方案。

 public class StoreProfileFragment extends Fragment{
        ListView lv;
        ArrayList<MyStore_list_dto> list = new ArrayList<MyStore_list_dto>();
        MyApplication app;
        MyListAdapter adtstore;
        View rootView;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_store_profile, container, false);
            app = (MyApplication) getActivity().getApplicationContext();

            list = DBAdpter.getMyStoreData(app.getUserID());
            lv = (ListView) rootView.findViewById(R.id.myStore_listview);

    adtstore = new MyListAdapter(getActivity().getApplicationContext());
        lv.setAdapter(adtstore);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                Log.v("log_tag", "List Item Click");
            }
        });


            return rootView;
        }
        public class MyListAdapter extends BaseAdapter {
            private LayoutInflater mInflater;

            public MyListAdapter(Context context) {
                mInflater = LayoutInflater.from(context);

            }

            public int getCount() {
                return list.size();
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                convertView = mInflater.inflate(R.layout.custome_mystorelist,
                        null);
                ImageButton store_Name_img = (ImageButton) convertView
                        .findViewById(R.id.my_Store_logo_image);

                TextView store_Name_txt = (TextView) convertView
                        .findViewById(R.id.mystore_list_name);


                store_Name_txt.setText( list.get(position).name);



                if (list.get(position).image != null) {
                    byte[] Image_getByte;
                    try {
                        Image_getByte = Base64.decode(list.get(position).image);
                        ByteArrayInputStream bytes = new ByteArrayInputStream(
                                Image_getByte);
                        BitmapDrawable bmd = new BitmapDrawable(bytes);
                        Bitmap bm = bmd.getBitmap();
                        store_Name_img.setImageBitmap(bm);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                store_Name_img.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        FragmentManager fm = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fm
                                .beginTransaction();
                        MyStoreItemFragment fm2 = new MyStoreItemFragment();
                        fragmentTransaction.replace(R.id.rela_myStore_fragment,
                                fm2, "HELLO");
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();
                        Bundle bundle = new Bundle();
                        bundle.putString("position", list.get(position).store_id);
                        fm2.setArguments(bundle);

                    }
                });

                return convertView;
            }
        }
    }

在下面的XML文件中,有一个ListView:

<ListView
    android:id="@+id/myStore_listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="5dp"
    android:dividerHeight="0dip"
     >
</ListView>

以下是自定义的listItem xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rela_store_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="5dip"
    android:background="@android:color/white" >


        <ImageButton
            android:id="@+id/my_Store_logo_image"
            android:layout_width="60dip"
            android:layout_height="60dip"
             android:layout_margin="5dip" />

        <TextView
            android:id="@+id/mystore_list_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/my_Store_logo_image"
            android:layout_marginBottom="18dp"
            android:layout_marginLeft="18dp"
            android:layout_toRightOf="@+id/my_Store_logo_image"
             android:layout_margin="5dip"
            android:text="dfdsfds"
            android:textColor="#040404"
            android:textSize="15sp"
            android:textStyle="bold"
            android:typeface="sans"  />

</RelativeLayout>

请参阅https://dev59.com/3m035IYBdhLWcg3wNtRw或https://dev59.com/dnI95IYBdhLWcg3w5iY4,了解有关ListView中的OnItemClickListener和可点击可编辑小部件的更多信息。 - Shayan Pourvatan
4个回答

17

@crickpatel0024 当您单击列表行时会发生什么?ImageButton是否会获得焦点?请检查。 - Raghunandan
@crickpatel0024 很高兴能帮忙。还有一个建议,使用ViewHolder可以帮助提高性能。 - Raghunandan
我花了大约6个小时来解决片段中ListView OnItemClick问题(API19)。将rootview.findViewById...移动到Fragment.onCreateView(); 将适配器移到这里; 更改片段,列表视图和列表视图项的布局类型......; 记录和调试所有内容:fragmentActivity,RootView,container,listview,convertedView等...因此,在ListView的项目布局中绝对必须有: 1)android:descendantFocusability =“blocksDescendants”; 2)android:clickable =“false”-甚至最好从ListView Item中删除android:clickable。非常感谢! - Sergio
有史以来最好的答案。 - dokam_scotland

2
在您的自定义 XML 文件中添加这些属性。
 android:focusableInTouchMode="false"
 android:focusable="false"

为您的TextView和ImageButton。


0

每当您的列表项包含类似按钮之类的东西时,就会出现此类问题。 您应该在您的适配器类中使用一个视图持有者。

 private class ViewHolder{
           ImageButton imagebutton;
           TextView textView;        

         }

在你的getview方法中使用它。希望这对你有用。


ViewHolder肯定有助于提高性能。但这与项点击监听器有什么关系呢? - Raghunandan
@Raghunandan,您所说的非常正确,但当列表项包含按钮复选框等元素时,它只会防止选择itemview项。有关详细信息,请参见此处。解释得非常清楚。 - maddy d
阅读完整篇博客,特别是“为什么我的项目视图不再可点击?”这一部分,以充分理解你刚刚链接的博客。 - Raghunandan

0

这里是代码:请使用onActivityCreated(),并使用以下代码:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        ListView lv1 = (ListView)getActivity().findViewById(R.id.myStore_listview);
        adtstore = new MyListAdapter(getActivity().getApplicationContext());
        lv.setAdapter(adtstore);
        lv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                Toast.makeText(getActivity(), "Item clicked : " + position, Toast.LENGTH_SHORT).show();

            }

        });

    }

现在应该可以工作了.. :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接