如何设置按钮clicklistener到列表视图像onitemclicklistener工作 [英] how can i set button clicklistener in to list view to work like onitemclicklistener

查看:206
本文介绍了如何设置按钮clicklistener到列表视图像onitemclicklistener工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个按钮的列表视图,文本视图和......结果
我想设置onclicklistener一个按钮和一个TextView干什么就干什么,我在onitemclicklistener设置此列表视图。但我不知道我怎么能做到这一点。结果
这些都是我lists.xml codeS结果

I have a listview that contains two buttons, a text view and...
I want to set onclicklistener for one button and a textview to do whatever that I set in onitemclicklistener for this listview. but I don't know how can I do that.
these are my lists.xml codes

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp">

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="160dp" 
        android:layout_gravity="center_horizontal" 
        android:padding="15dp"
        android:background="@android:drawable/dialog_holo_light_frame"
        android:focusable="false"
        android:focusableInTouchMode="false">
    <FrameLayout
            android:layout_width="132dp"
            android:layout_height="120dp"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:background="@drawable/company1" 
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:clickable="true"
            android:id="@+id/frameLayout">
    </FrameLayout>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/id"
            android:visibility="gone"/>

    <LinearLayout 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <TextView
                android:layout_width="match_parent"
                  android:layout_height="35dp"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:text="@string/company_name"
                  android:id="@+id/header_factory_list" 
                  android:gravity="right"
                  android:focusable="false"
                  android:focusableInTouchMode="false"
                  android:textColor="#21313f"
                  android:textSize="15dp"
                  android:singleLine="true"/>
        <RatingBar
                style="@android:style/Widget.DeviceDefault.Light.RatingBar.Small"
                   android:focusable="false"
                   android:focusableInTouchMode="false"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/ratingBar2"
                   android:animationResolution="3"
                   android:rating="2.5"
                   android:stepSize=".05" 
                   android:layout_marginLeft="20dp"/>
        <Button
                android:layout_width="fill_parent"
                android:layout_height="25dp"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="@string/add_fav_list"
                android:id="@+id/button_add_fav_list"
                android:background="#cf000f"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:textColor="#ffffff"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:padding="2dp"
                android:textSize="10dp"/>
        <Button
                android:layout_width="fill_parent" 
                android:layout_height="25dp"
                android:focusable="false"
                android:text="@string/show_details"
                android:id="@+id/button_detsils"
                android:background="#21313f"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:textColor="#ffffff"
                android:layout_marginBottom="5dp"
                android:padding="2dp"
                android:textSize="10dp"/>
    </LinearLayout>
</LinearLayout>

也是这些都是我onitemclicklistener codeS由mainactivity:结果

also these are my onitemclicklistener codes from mainactivity:

Inside_Factory_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Inside_Factory_ListView.setVisibility(View.GONE);
                    TextView f= (TextView) view.findViewById(R.id.id);

                    Log.i("GETVIEW",view.getId()+"\n"+f.getText().toString());
                    setInfoInFactory(Integer.parseInt(f.getText().toString()));
                    Inside_Factory_Layout.setVisibility(View.VISIBLE);
                    final TranslateAnimation animation1 = new TranslateAnimation(0.0f, 0.0f,
                            3000.0f, 0.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
                    animation1.setDuration(1500);  // animation duration
                    animation1.setRepeatCount(0);  // animation repeat count
                    animation1.setRepeatMode(0);   // repeat animation (left to right, right to left )
                    //animation.setFillAfter(true);
                    Handler mHandler = new Handler();
                    Frist_Tab.setVisibility(View.VISIBLE);
                    Frist_Tab.startAnimation(animation1);
                    mHandler.postDelayed(new Runnable() {
                        public void run() {
                            Second_Tab.setVisibility(View.VISIBLE);
                            Second_Tab.startAnimation(animation1);
                        }
                    }, 1000);
                    mHandler.postDelayed(new Runnable() {
                        public void run() {
                            Third_Tab.setVisibility(View.VISIBLE);
                            Third_Tab.startAnimation(animation1);
                        }
                    }, 1000);
                    //  Log.i("CLICK","CLICKED="+position);
                    if(L[1]){
                        L[2]=true;
                    }

                }
            });

我想,当我点击按钮(进入列表视图)偏偏喜欢我的列表视图中该项目点击。
结果谁能帮帮我吗?

I want, when I click on the button(into the listview) happens like i click on that item of list view.
Can anyone please help me?

推荐答案

您可以使用一个接口,用于处理那些click事件按钮的TextView 。你必须将所有可获得焦点项目(如按钮,的RatingBar,EditText上,微调等),重点为false。

You can use an interface for handling click event of those Buttons and TextView. You have to set all focusable items'(like Button, RatingBar, EditText, Spinner etc.) focus to false.

我给你一个code段

适配器code

public class PropertyListAdapter extends ArrayAdapter<PropertyInfo> {

    private Context _context;
    private int layoutId;
    private List<PropertyInfo> dataList;
    private ListItemBtnClickInterface _interface;

    public PropertyListAdapter(Context context, int resource,
            List<PropertyInfo> objects, ListItemBtnClickInterface _interface) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        _context = context;
        layoutId = resource;
        dataList = objects;
        this._interface = _interface;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) _context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layoutId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.propertyImgView = (ImageView) convertView.
                    findViewById(R.id.propertyImgView);
            viewHolder.editImgView = (ImageView) convertView.
                    findViewById(R.id.editImgView);
            viewHolder.deleteImgView = (ImageView) convertView.
                    findViewById(R.id.deleteImgView);
            viewHolder.propertyNameTxtView = (TextView) convertView.
                    findViewById(R.id.propertyNameTxtView);
            viewHolder.ownerTypeTxtView = (TextView) convertView.
                    findViewById(R.id.ownerTypeTxtView);
            viewHolder.checkBox1 = (CheckBox) convertView.
                    findViewById(R.id.checkBox1);
            viewHolder.propertyNameTxtView.setTypeface(Utils.getRegularTypeface(_context));
            viewHolder.ownerTypeTxtView.setTypeface(Utils.getRegularTypeface(_context));

            viewHolder.checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                    PropertyInfo element = (PropertyInfo) viewHolder.checkBox1
                      .getTag();
                  element.setSelected(buttonView.isChecked());

                }
              });

            convertView.setTag(viewHolder);
            viewHolder.checkBox1.setTag(dataList.get(position));
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
            ((ViewHolder) convertView.getTag()).checkBox1.setTag(dataList.get(position));
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.checkBox1.setChecked(dataList.get(position).isSelected());

        int height = viewHolder.propertyImgView.getLayoutParams().height;
        int width = viewHolder.propertyImgView.getLayoutParams().width;

        viewHolder.deleteImgView.setTag(position);
        viewHolder.editImgView.setTag(position);

        viewHolder.deleteImgView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Object data = v.getTag();
                if(data != null) {
                    _interface.listItemBtnClickListener(data, v.getId());
                }
            }
        });

        viewHolder.editImgView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Object data = v.getTag();
                if(data != null) {
                    _interface.listItemBtnClickListener(data, v.getId());
                }
            }
        });

        PropertyInfo addPropertyInfo = dataList.get(position);
        String propertyName = "";
        String ownerType = "";
        String imgPath = "";
        propertyName = addPropertyInfo.getPropertyName();
        ownerType = addPropertyInfo.getOwnerTypeName();
        imgPath = addPropertyInfo.getImagePath();

        viewHolder.propertyNameTxtView.setText(propertyName);
        viewHolder.ownerTypeTxtView.setText(ownerType);
        if(imgPath != null && !imgPath.equalsIgnoreCase("")) {
            Uri uri = Uri.parse(imgPath);
            Picasso.with(_context).load(uri)
            .resize(width, height).centerCrop().into(viewHolder.propertyImgView);
        } else {
            viewHolder.propertyImgView.setImageResource(R.drawable.no_img);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView propertyImgView;
        ImageView editImgView;
        ImageView deleteImgView;
        TextView propertyNameTxtView;
        TextView ownerTypeTxtView;
        CheckBox checkBox1;
    }
}

接口作为处理click事件

Interface for handling click event

public interface ListItemBtnClickInterface {

    public void listItemBtnClickListener(Object obj, int viewId);
}

设置适配器,然后单击子视图事件

setting adapter and click event of child views

propertyList = dbHelper.getAllProperties();
        dbHelper.closeDB();

        if(propertyList != null && propertyList.size() > 0) {
            noPropertyTxtView.setVisibility(View.GONE);
            adapter = new PropertyListAdapter(
                    PropertyListActivity.this, 
                    R.layout.row_property, propertyList, new ListItemBtnClickInterface() {

                        private int clickedPosition;

                        @Override
                        public void listItemBtnClickListener(Object obj, int viewId) {
                            // TODO Auto-generated method stub
                            clickedPosition = Integer.parseInt(obj.toString());
                            PropertyInfo addPropertyInfo = propertyList.get(clickedPosition);
                            final long propertyId = addPropertyInfo.getId();
                            switch (viewId) {
                            case R.id.editImgView:
                                Intent intent = new Intent(PropertyListActivity.this,
                                        AddPropertyActivity.class);
                                intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID, 
                                        propertyId);
                                startActivity(intent);
                                break;

                            case R.id.deleteImgView:
                                new AlertDialog.Builder(PropertyListActivity.this)
                                .setTitle(getResources().getString(R.string.delete_property_title))
                                .setIcon(R.drawable.delete_icon)
                                .setMessage(R.string.delete_property_msg).setCancelable(true)
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        dbHelper.deleteProperty(propertyId);
                                        dbHelper.closeDB();
                                        propertyList.remove(clickedPosition);
                                        adapter.notifyDataSetChanged();
                                        if (propertyList.size() == 0)
                                            noPropertyTxtView.setVisibility(View.VISIBLE);
                                        else
                                            noPropertyTxtView.setVisibility(View.GONE);
                                    }
                                }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub

                                    }
                                }).show();
                                break;

                            default:
                                break;
                            }
                        }
                    });
            propertyListView.setAdapter(adapter);
            propertyListView.setOnItemClickListener(this);
        } else {
            noPropertyTxtView.setVisibility(View.VISIBLE);
        }

OnItemClick 的ListView

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        PropertyInfo addPropertyInfo = propertyList.get(position);
        long propertyId = addPropertyInfo.getId();
        Intent intent = new Intent(PropertyListActivity.this,
                AddPropertyActivity.class);
        intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID, 
                propertyId);
        startActivity(intent);
    }

在添加这些属性可聚焦项目

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

这篇关于如何设置按钮clicklistener到列表视图像onitemclicklistener工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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