更改视图列表项属性选择 [英] Change attributes of view on list item selection

查看:112
本文介绍了更改视图列表项属性选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView其中包含自定义行。这个自定义行具有以下UI元素

I have a ListView which contains custom rows. This custom row has following UI elements

  1. ImageView的imageView1,imageView2
  2. TextView的textview1,textView2,textView3

要求是每当列表选择行会有以下更改

The requirement is whenever the list row is SELECTED there would be following changes

imageView1背景颜色变为

imageView1 background,color is changed

imageView1颜色更改

imageView1 color is changed

textview1颜色,大小和字体是修改

textview1 color,size and typeface is changes

textview2颜色,大小更改

textview2 color,size is changed

textview3颜色,大小更改

textview3 color,size is changed

什么是最好的方式来设计呢?

What would be the best way to design for this?

AFAIK,我们可以选择不应用样式。有没有更好的方法来研究这个,而不是处理在java code?

AFAIK we cannot apply styles in the selector. Is there a better way to work on this rather than handling in the java code?

我们有setOnItemSelectedListener,可以在一个ListView这将下面的回调方法都被设置:

We have setOnItemSelectedListener that can be set on a Listview which would have following callback methods:

我)onItemSelected

i) onItemSelected

二)onNothingSelected

ii) onNothingSelected

但没有回调方法,它提供了已经失去了其重点项目的明细。这是一个更好的地方进行更改?

However there is no callback method which provide details of the item which has lost its focus. Is this a better place to make the changes?

在此先感谢。

推荐答案

我想你想在这里做什么是创建的复合控制。你可以找到在样品// ApiDemos子目录的SDK目录为例(您可能需要下载 Android源,如果​​你不具备这些目录)。

I think what you want to do here is create a Compound Control. You can find an example in your SDK directory in the samples//ApiDemos subdirectory (you may want to download the Android source if you don't have these directories).

我会做的就是创建一个类继承无论从任何样的布局,你正在使用您的自定义行。让我们假设这是一个的LinearLayout 在这个例子中。在子类构造函数,你可以从一个资源夸大你的布局,找到子视图,并将其连接到实例变量,和的LinearLayout返回给调用者。

What I would do is create a class that inherits from whatever kind of layout you are using for your custom row. Let's suppose it's a LinearLayout for this example. In your subclass constructor, you can inflate your layout from a resource, find the subViews and attach them to instance variables, and return the LinearLayout to the caller.

在本小类中,你可以覆盖的setSelected 和操作的任何方式你想要的子视图。

In this subclass, you can override setSelected and manipulate the subViews in whatever way you want.

下面是什么,我根据您的文章,描述一个例子:

Here's an example of what I am describing based on your post:

public class MyCustomLayout extends LinearLayout {
    public ImageView imageView1;   // These are public since you likely want
    public ImageView imageView2;   // to set them in your Adapter.
    public TextView textView1;
    public TextView textView2;
    public TextView textView3;

    public MyCustomLayout(Context ctxt) {
        super(ctxt);

        // The call below attaches the items in the mycustomlayout.xml file to this
        // instance, which serves as the root element in the layout.
        LayoutInflater.from(ctxt).inflate(R.layout.mycustomlayout, this, true);

        imageView1 = (ImageView)findViewById(R.id.imageview1);
        imageView2 = (ImageView)findViewById(R.id.imageview2);
        textView1 = (TextView)findViewById(R.id.textview1);
        textView2 = (TextView)findViewById(R.id.textview2);
        textView3 = (TextView)findViewById(R.id.textview3);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setChecked(selected);
        if (selected) {
            imageView1.setBackgroundColor(0xFFFF0000); // whatever color you want
            // Do all the rest of the setting of your subviews here for
            // the case when the row is selected.
        } else {
            imageView1.setBackgroundColor(0xFF000000); // whatever color you want
            // Do all the rest of the setting of your subviews here for
            // the case when the row is not selected
        }
    }
}

现在,在mycustomlayout.xml文件,要使用<合并> 标签,这样你就不会产生不需要的布局:

Now, in the mycustomlayout.xml file, you want to use the <merge> tag so that you don't create an unneeded layout:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/imageview1" />
    <ImageView
        android:id="@+id/imageview2" />
    <TextView
        android:id="@+id/textview1" />
    <TextView
        android:id="@+id/textview2" />
    <TextView
        android:id="@+id/textview3" />
</merge>

我省略了子视图的所有配置上面,很明显,但你应该得到有关如何设置XML文件的想法。你也可以在code做,如果你不想愚弄XML和LayoutInflater。你可以看一下这是很有帮助的博客文章上的Andr​​oid开发博客,其中讨论接近尾声了类似的情况。

I've elided all the configuration of the subviews above, obviously, but you should get the idea about how to set up the XML file. You could also do this in code if you don't want to fool with XML and a LayoutInflater. You can look at this helpful blog post on the Android dev blog which discusses a similar case towards the end.

然后,在你的适配器的 getView 你可以创建(或再循环) MyCustomLayout 的情况下,将它们设置,和让框架照顾跟踪其中行被选中而未被选择

Then, in your Adapter's getView you can just create (or recycle) instances of MyCustomLayout, set them up, and let the framework take care of keeping track of which rows are selected and which aren't selected.

这篇关于更改视图列表项属性选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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