动态的RelativeLayout和点击 [英] Dynamically RelativeLayout and clickable

查看:137
本文介绍了动态的RelativeLayout和点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从code动态创建的ListView有:

I try to create listView dynamically from code with:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.grid_item, null);

        holder = new ViewHolder();
        holder.one = (TextView) v.findViewById(R.id.one );
        holder.two= (TextView) v.findViewById(R.id.two);
        holder.three= (TextView) v.findViewById(R.id.three);
        holder.four= (TextView) v.findViewById(R.id.four);
        holder.five= (TextView) v.findViewById(R.id.five);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final Custom custom = entries.get(position);
    if (custom != null) {
        holder.one .setText(custom.getOne());
        holder.two.setText(custom.getTwo());
        holder.three.setText(custom.getThree());
        holder.four.setText(custom.getFour());
        holder.five.setText(custom.getFive());
    }
    return v;
}

grid_item.xml:

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="0dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_marginRight="4dp" />

        <View android:id="@+id/separator" 
           android:background="#cccccc" 
           android:layout_width="1dip"
           android:layout_height="45dip"
            android:layout_marginRight="4dp"/>

    </LinearLayout>

    <TextView
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/linearLayout1"
        android:layout_toLeftOf="@+id/linearLayout2"
        android:text=""
        android:textSize="25dp"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/alarmTitle"
        android:layout_toRightOf="@+id/linearLayout1"
        android:layout_toLeftOf="@+id/linearLayout2"
        android:text=""
        android:textSize="16dp" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="3dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/three"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/four"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/five"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/six"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12dp" />
    </LinearLayout>

</RelativeLayout>

我想在ListView项,在我的例子是RelativeLayout的,如点击。
我试图用

I want to make item in listview, in my example that is relativelayout, as clickable. I trying with:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true">

和/或

        v.setClickable(true);
        v.setEnabled(true); 
        v.setFocusable(true);

但是,这是行不通的。

But this is not working.

我在哪里出错了?

推荐答案

您可以点击监听器添加到您的 v 视图getView()方法中返回

you can add your click listener to your v view returned within the getView() method

v.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
     }....

这应该工作 - 为我工作。在调用的方法应该移到别处。不知道你要实现的View.OnClickListener在适配器,但它给你我猜。

this should work - works for me. the methods called within should be moved elsewhere. not sure if you want to implement the View.OnClickListener in the Adapter but it's up to you i guess.

干杯

这篇关于动态的RelativeLayout和点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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