当前位置: 代码迷 >> java >> 我如何设置按钮clicklistener进入列表视图以像onitemclicklistener一样工作
  详细解决方案

我如何设置按钮clicklistener进入列表视图以像onitemclicklistener一样工作

热度:54   发布时间:2023-08-04 09:22:55.0

我有一个包含两个按钮的列表视图,一个文本视图和...
我想为一个按钮和文本视图设置onclicklistener,以执行我在onitemclicklistener中为此列表视图设置的任何操作。 但我不知道该怎么办。
这些是我的lists.xml代码

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

这些也是来自mainactivity的onitemclicklistener代码:

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

                }
            });

我想,当我单击按钮(进入列表视图)时,就像我单击列表视图的该项一样。
谁能帮帮我吗?

您可以使用接口来处理这些ButtonsTextView click事件。 您必须将所有可聚焦项(例如Button,RatingBar,EditText,Spinner等)的焦点设置为false。

我给你一个代码片段

适配器代码

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

处理点击事件的界面

public interface ListItemBtnClickInterface {

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

设置适配器和子视图的单击事件

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

ListView的OnItemClick

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

将这些属性添加到focusable项中

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