列表视图OnItemClick听者中的片段无法正常工作 [英] listview OnItemClick listner not work in fragment

查看:184
本文介绍了列表视图OnItemClick听者中的片段无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用列表视图中的片段,但我使用的列表onItemClick听者无法正常工作。我的code以下,以及如何完美的解决方案,请帮助我!

谢谢!

 公共类StoreProfileFragment扩展片段{
        ListView的LV;
        ArrayList的< MyStore_list_dto>名单=新的ArrayList< MyStore_list_dto>();
        所有MyApplication应用程序;
        MyListAdapter adtstore;
        查看rootView;
        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){

            查看rootView = inflater.inflate(R.layout.fragment_store_profile,集装箱,假);
            应用=(所有MyApplication)getActivity()getApplicationContext()。

            表= DBAdpter.getMyStoreData(app.getUserID());
            LV =(ListView控件)rootView.findViewById(R.id.myStore_listview);

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

            公共无效onItemClick(适配器视图<>一种,视图V,INT位置,
                    长ID){
                Log.v(log_tag,列表项,点击);
            }
        });


            返回rootView;
        }
        公共类MyListAdapter扩展了BaseAdapter {
            私人LayoutInflater mInflater;

            公共MyListAdapter(上下文的背景下){
                mInflater = LayoutInflater.from(上下文);

            }

            公众诠释getCount将(){
                返回则为list.size();
            }

            公共对象的getItem(INT位置){
                返回的位置;
            }

            众长getItemId(INT位置){
                返回的位置;
            }

            公共查看getView(最终诠释的立场,观点convertView,
                    ViewGroup中父){
                convertView = mInflater.inflate(R.layout.custome_mystorelist,
                        空值);
                的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(位置)。名称);



                如果(list.get(位置),在图像配!= NULL){
                    byte []的Image_getByte;
                    尝试 {
                        Image_getByte = Base64.de code(list.get(位置)在图像配);
                        ByteArrayInputStream的字节=新ByteArrayInputStream的(
                                Image_getByte);
                        BitmapDrawable BMD =新BitmapDrawable(字节);
                        位图BM = bmd.getBitmap();
                        store_Name_img.setImageBitmap(BM);

                    }赶上(IOException异常E){
                        // TODO自动生成的catch块
                        e.printStackTrace();
                    }
                }
                store_Name_img.setOnClickListener(新View.OnClickListener(){

                    @覆盖
                    公共无效的onClick(视图v){
                        // TODO自动生成方法存根
                        FragmentManager FM = getFragmentManager();
                        FragmentTransaction fragmentTransaction = FM
                                .beginTransaction();
                        MyStoreItemFragment FM2 =新MyStoreItemFragment();
                        fragmentTransaction.replace(R.id.rela_myStore_fragment,
                                FM2,HELLO);
                        fragmentTransaction.addToBackStack(空);
                        fragmentTransaction.commit();
                        束束=新包();
                        bundle.putString(位置,list.get(位置).store_id);
                        fm2.setArguments(包);

                    }
                });

                返回convertView;
            }
        }
    }
 

ListView控件在下面::: XML文件

 <的ListView
    机器人:ID =@ + ID / myStore_listview
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentLeft =真
    机器人:layout_marginTop =5DP
    机器人:dividerHeight =0dip
     >
< / ListView控件>
 

下面Custome的listItem XML ::::

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / rela_store_fragment
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_marginTop =5dip
    机器人:背景=@机器人:彩色/白>


        <的ImageButton
            机器人:ID =@ + ID / my_Store_logo_image
            机器人:layout_width =60dip
            机器人:layout_height =60dip
             机器人:layout_margin =5dip/>

        <的TextView
            机器人:ID =@ + ID / mystore_list_name
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignBottom =@ + ID / my_Store_logo_image
            机器人:layout_marginBottom =18dp
            机器人:layout_marginLeft =18dp
            机器人:layout_toRightOf =@ + ID / my_Store_logo_image
             机器人:layout_margin =5dip
            机器人:文本=dfdsfds
            机器人:文字颜色=#040404
            机器人:TEXTSIZE =15sp
            机器人:TEXTSTYLE =黑体
            机器人:字体=SANS/>

< / RelativeLayout的>
 

解决方案

将此

 安卓descendantFocusability =blocksDescendants
 

在RelativeLayout的 listItem.xml

我觉得的ImageButton 需要当你点击列表行集中。

编辑:

考虑使用 ViewHolder 模式

参考:

http://developer.android.com/training/improving-布局/平滑scrolling.html

I used listview in fragment but i used list onItemClick listner not working .my code below and how to perfect solution please help me !!

Thanks!!!

 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>

解决方案

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

这篇关于列表视图OnItemClick听者中的片段无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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