简体   繁体   English

如何保持正确引用布局点击?

[英]How do you keep layouts clicks correctly referenced?

Let me explain myself: 让我解释一下自己:

As you know, when you've a view which have to be inflated several times, but changing values, you use a GridView or a ListView. 如您所知,当您必须多次放大一个视图但要更改其值时,可以使用GridView或ListView。 Those two Composite views, have some methods like onItemClick . 这两个Composite视图具有一些方法,例如onItemClick This method is so useful, as it returns the position of the view clicked. 此方法非常有用,因为它返回单击的视图的位置

With this position you can perform some concrete tasks, like retreiving from an ArrayList, the information of that object. 在这个位置上,您可以执行一些具体的任务,例如从ArrayList中检索该对象的信息。 Here's an example: 这是一个例子:

ArrayList<DocumentInfo> documents;

And when you set a setOnItemClickListener() you can get the correct values: 并且,当您设置setOnItemClickListener()您可以获得正确的值:

gallery.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
        getDocumentInfoOf(pos);
    }
});
public void getDocumentInfoOf(int position){
    DocumentInfo doc = documents.get(position);
}

However, when you aren't using a GridView or a ListView, you're in your own. 但是,当您不使用GridView或ListView时,您就属于自己。 You don't have a clear way (AFAIK) to know which layout inflated is the one clicked (I mean like the previous example, the "position" value). 您没有明确的方法(AFAIK)来了解单击的是哪个布局已膨胀(我的意思是像上一个示例一样,“ position”值)。

What I am currently doing, is the following: 我当前正在做的事情如下:

for (int i=0; i<10;i++){
    RelativeLayout documentInflated = (RelativeLayout) this.mInflater.inflate(R.layout.open_document_per_inflar, null);
    documentInflated.setContentDescription(""+i);
    documentInflated.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            openDocument(v);
        }
    });

    container.addView(documentInflated);
}

public void openDocument(View v){
    int idDocument = Integer.parseInt(v.getContentDescription());
    //idDocument is the view clicked
}

Do you guys think this is a clear way of doing this? 你们认为这样做是一种清晰的方法吗?

Thank you!!! 谢谢!!!

If I'm not mistaken you want to get some data from your created Relative Layout when you click on it. 如果我没记错的话,当您单击它时,您想从创建的相对布局中获取一些数据。 The best solution here is to use the method setTag(Object tag). 最好的解决方案是使用setTag(Object tag)方法。 After that you get the informatiom with the method getTag(). 之后,您可以使用getTag()方法获取信息。 This method allows you to add extra information to your view. 此方法使您可以向视图中添加其他信息。 As it says in the documentation: 如文档中所述:

Tags 标签

Unlike IDs, tags are not used to identify views. 与ID不同,标签不用于标识视图。 Tags are essentially an extra piece of information that can be associated with a view. 标签本质上是可以与视图关联的额外信息。 They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure. 它们最常用于方便地在视图本身中存储与视图相关的数据,而不是将它们放在单独的结构中。

Also depending on your needs you can seperate every tag with a key -> value pair with the method setTag(int key, Object tag), after that you can retrieve this object with getTag(int key); 另外,根据您的需要,可以使用setTag(int key,Object tag)方法用键->值对分隔每个标签,然后再使用getTag(int key)检索此对象;

So in your case you will have 因此,在您的情况下,您将拥有

documentInflated.setTag(i)

in the onClick yo will then have: 在onClick中,您将拥有:

int i = (int)v.getTag();

Unless I'm not understanding your desire... 除非我不明白你的愿望...

      @Override
        public void onClick(View v) {
            openDocument(v);
        }

v IS the view being clicked. v是否正在单击视图。 Your code looks like it should do what you're hoping it will do. 您的代码看起来应该像您希望的那样执行。 What are you actually seeing happen? 您实际上看到了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM