Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

android - listview OnItemClick listener not work in fragment

I used listview in fragment but I used list onItemClick listener not working. My code below and how to perfect solution.

 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;
            }
        }
    }

ListView in xml file below:

<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>

Custome listItem xml below:

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add this

android:descendantFocusability="blocksDescendants"

to the RelativeLayout in listItem.xml.

I guess ImageButton takes focus when you click on list row.

Edit:

Consider using a ViewHolder pattern

Reference:

http://developer.android.com/training/improving-layouts/smooth-scrolling.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...