首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态LinearLayout +触摸事件始终使用最后添加的LinearLayout

动态LinearLayout +触摸事件始终使用最后添加的LinearLayout
EN

Stack Overflow用户
提问于 2015-02-13 03:29:14
回答 1查看 187关注 0票数 0

我打算在布局中添加一个动态的“按钮”列表。现在,我的按钮由LinearLayout、ImageView和TextView组成。我甚至在每次迭代时都会给LinearLayout添加一个触点。所有对象都已添加并正确显示。但从那时起,一切都出了问题。

触摸事件将为每个按钮触发,但相应的发送者对象是最后添加的LinearLayout。

例如:我有3个“按钮”要添加。每一个都被创建并添加到它们需要的位置。在循环中,我为每个“按钮”向Touch事件添加了一个事件处理程序。当我去触摸第一个时,最后一个会突出显示。我触摸第二个,最后一个突出显示。

我对触摸事件使用了一种通用的方法,并通过我存储的第二个值(如标签)来识别它们。

下面提供的是针对这种情况的相关代码。

基于来自web服务的记录集添加“按钮”的代码。

代码语言:javascript
复制
if (calendars != null && calendars.Rows.Count > 0)
{
    LinearLayout buttonCalendar = null;
    TextView labelTitle = null;
    ImageView imageIcon = null;

    Int64 App_CalendarID = 0;
    String Title = "";

    LinearLayout.LayoutParams layoutParams =
        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, Android.DpToPx(45, this.View.Context));

    LinearLayout.LayoutParams imageParams =
        new LinearLayout.LayoutParams(Android.DpToPx(45, this.View.Context), LinearLayout.LayoutParams.MatchParent);

    LinearLayout.LayoutParams labelParams =
        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);

    Drawable icon = null;

    icon = Resources.GetDrawable(Resource.Drawable.image_calendar_light);

    TransitionDrawable buttonTransition = 
        (TransitionDrawable)Resources.GetDrawable(Resource.Drawable.button_transition);

    foreach (DataRow row in calendars.Rows)
    {
        App_CalendarID = Convert.ToInt64(row["App_CalendarID"]);
        Title = Convert.ToString(row["Title"]);

        buttonCalendar = new LinearLayout(layoutFeatureBody.Context)
        {
            Orientation = Orientation.Horizontal,
            LayoutParameters = layoutParams,
            Background = buttonTransition,
            Id = (Int32)App_CalendarID,
        };

        imageIcon = new ImageView(layoutFeatureBody.Context)
        {
            Background = icon,
            LayoutParameters = imageParams,
        };
        imageIcon.SetScaleType(ImageView.ScaleType.CenterInside);

        buttonCalendar.AddView(imageIcon);

        labelTitle = new TextView(layoutFeatureBody.Context)
        {
            Text = Title,
            TextSize = 14f,
            LayoutParameters = labelParams,
            Gravity = GravityFlags.CenterVertical,
        };
        labelTitle.SetTextColor(Color.White);

        buttonCalendar.AddView(labelTitle);

        layoutFeatureBody.AddView(buttonCalendar);

        buttonCalendar.Touch += buttonCalendar_Touch;
    }
}

下面是为Touch事件分配的方法。

代码语言:javascript
复制
void buttonCalendar_Touch(object sender, View.TouchEventArgs e)
{
    LinearLayout buttonLayout = (LinearLayout)sender;

    Android.TransitionButton(ref buttonLayout, e.Event);

    if (e.Event.Action != MotionEventActions.Up)
        return;

}

我猜事件不会像在.Net中那样工作:-X我正在努力在网上找到任何与这种情况相关的帮助。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-02-13 04:08:29

我不太确定我做了什么(代码没有改变,只是移动了)。但我做了一些早就该做的工作,即把所有相关代码移到LinearLayout的一个子类中来表示我的“按钮”。

真的只是让代码更容易阅读和管理。

现在,事件似乎是独立运行的。这是我的更新代码,也许有人能帮我解释一下我在这里看到的情况。

下面的代码添加了按钮:

代码语言:javascript
复制
if (calendars != null && calendars.Rows.Count > 0)
{
    ButtonEx buttonCalendar = null;

    Int64 App_CalendarID = 0;
    String Title = "";

    Drawable icon = null;

    icon = Resources.GetDrawable(Resource.Drawable.image_calendar_light);

    foreach (DataRow row in calendars.Rows)
    {
        App_CalendarID = Convert.ToInt64(row["App_CalendarID"]);
        Title = Convert.ToString(row["Title"]);

        buttonCalendar = new ButtonEx(layoutFeatureBody.Context)
        {
            Text = Title,
            Tag = App_CalendarID,
            Image = icon,
            TextColor = Color.White,
            TextSize = 14,
        };

        layoutFeatureBody.AddView(buttonCalendar);

        buttonCalendar.Touch += buttonCalendar_Touch;
    }
}

下面是我的按钮类:

代码语言:javascript
复制
public class ButtonEx : LinearLayout
{

    ImageView imageView = null;

    TextView textView = null;

    LinearLayout.LayoutParams imageParams = null;

    LinearLayout.LayoutParams labelParams = null;

    public new Object Tag
    { get; set; }

    private String _Text = "";

    public String Text
    {
        get
        {
            return _Text;
        }
        set
        {
            if (textView != null)
                textView.Text = value;
            _Text = value;
        }
    }

    private Color _TextColor = Color.White;

    public Color TextColor
    {
        get
        {
            return _TextColor;
        }
        set
        {
            if (textView != null)
                textView.SetTextColor(value);
            _TextColor = value;
        }
    }

    private float _TextSize = 12f;

    public float TextSize
    {
        get
        {
            return _TextSize;
        }
        set
        {
            if (textView != null)
                textView.TextSize = value;
            _TextSize = value;
        }
    }

    public Drawable _Image = null;

    public Drawable Image
    {
        get
        {
            return _Image;
        }
        set
        {
            if (imageView != null)
                imageView.Background = value;
            _Image = value;
        }
    }

    public ButtonEx(Context context)
        : base(context)
    {
        TransitionDrawable buttonTransition =
            (TransitionDrawable)Resources.GetDrawable(Resource.Drawable.button_transition);

        LinearLayout.LayoutParams layoutParams =
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, Android.DpToPx(45, context));

        this.Background = buttonTransition;
        this.LayoutParameters = layoutParams;
        this.Orientation = Orientation.Horizontal;

        imageParams =
            new LinearLayout.LayoutParams(
                Android.DpToPx(45, context), 
                LinearLayout.LayoutParams.MatchParent);

        labelParams =
            new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MatchParent, 
                LinearLayout.LayoutParams.MatchParent);

        imageView = new ImageView(context)
        {
            LayoutParameters = imageParams,
        };
        imageView.SetScaleType(ImageView.ScaleType.CenterInside);

        this.AddView(imageView);

        textView = new TextView(context)
        {
            LayoutParameters = labelParams,
            Gravity = GravityFlags.CenterVertical,
        };

        this.AddView(textView);

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28486141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档