如何在位于listview android上的Relative Layout上添加click事件

时间:2015-10-29 06:30:22

标签: android listview layout relative

我遇到这种情况的问题,如何检测事件点击ListView上的相对布局。这是我的截图设计: enter image description here

这是我的xml布局:

*<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <!-- HEADER -->
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edtsearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/searchicon"
            android:drawableRight="@drawable/deleteicon"
            android:drawablePadding="3dp"
            android:layout_weight="3"
            android:hint="Item name"
            android:padding="3dp"
            android:windowSoftInputMode="stateVisible"
            android:background="@layout/round_border_image"/>
    </LinearLayout >
    <!-- FOOTER -->
    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:background="#b5b5b5">
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/header"
        android:layout_weight="1" >
        <ListView
            android:id="@+id/lvcustomer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:listSelector="@layout/custom_list_selector">
        </ListView>
    </FrameLayout>
    <RelativeLayout
        android:id="@+id/layoutadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        android:padding="8dp"
        android:background="#607D8B">
        <ImageView
            android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/addicon"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

</RelativeLayout>*

这是我的代码:

**protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_customer);
    Global.ViewCustomer=this;
    final Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/pillgothic300mgregular-webfont.ttf");
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#d0d4d0")));
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.backicon);
    getSupportActionBar().setTitle("Back");
    getSupportActionBar().setTitle((Html.fromHtml("<font color=\"#000\">Back</font>")));
    LayoutInflater mInflater = LayoutInflater.from(this);
    AdapterDb = new DataProcessor(this);
    final SQLiteDatabase dbdelete = AdapterDb.getWritableDatabase();
    final SQLiteDatabase dbinsert = AdapterDb.getWritableDatabase();
    final SQLiteDatabase db = AdapterDb.getReadableDatabase();
    try {
        AdapterDb.importIfNotExist();
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ListView lvcustomer=(ListView)findViewById(R.id.lvcustomer);
    ArrayList<CustomerDetail_class> custlist = AdapterDb.getAllCustomer();
    lvcustomer.setAdapter(new ListCustAdapter(getBaseContext(), custlist));

    lvcustomer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub
            final CustomerDetail_class listcust = (CustomerDetail_class) parent.getItemAtPosition(position);
            LayoutInflater li = LayoutInflater.from(ViewCustomer.this);
            final View inputdialogcustom = li.inflate(R.layout.custom_user_default, null);
            AlertDialog.Builder alert = new AlertDialog.Builder(ViewCustomer.this);
            alert.setView(inputdialogcustom);
            TextView tvuser = (TextView) inputdialogcustom.findViewById(R.id.tvuser);
            tvuser.setTypeface(myTypeface);
            tvuser.setTextSize(TypedValue.COMPLEX_UNIT_PX, getBaseContext().getResources().getDimensionPixelSize(R.dimen.lbltitlelistviewitem));
            alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }
            });
            alert.setNegativeButton("No", null);
            alert.show();
        }
    });
    RelativeLayout layoutadd=(RelativeLayout)findViewById(R.id.layoutadd);
    layoutadd.bringToFront();
    layoutadd.setClickable(true);
    layoutadd.setEnabled(true);
    layoutadd.setFocusable(true);
    layoutadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(),"Halo",Toast.LENGTH_SHORT).show();
        }
    });
}**

已经在RelativeLayout上添加了事件onclick,但它会引发listview点击事件,那么你有这个案例的最佳解决方案吗?

2 个答案:

答案 0 :(得分:0)

您可以在listView上添加透明视图。

答案 1 :(得分:0)

您可以在clickable = true中设置Relativelayout以使其正常工作, 来自android doc:

  

机器人:可点击

     

定义此视图是否对点击事件做出反应。

     

必须是布尔值,“true”或“false”。

     

这也可能是对资源的引用(在表单中)   “@ [package:] type:name”)或主题属性(在表单中   “?[package:] [type:] name”)包含此类型的值。

     

这对应于可点击的全局属性资源符号。