티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

What is the main purpose of setTag() getTag() methods of View?

View 타입 객체의 setTag()와 getTag() 메서드의 주요 목적은 무엇인가요?

 문제 내용 

What is the main purpose of such methods as setTag() and getTag() of View type objects?

View 타입 객체의 setTag()와 getTag()와 같은 메서드의 주요 목적은 무엇인가요

 

Am I right in thinking that I can associate any number of objects with a single View?

하나의 View에 여러 개의 객체를 연결할 수 있는 것이 맞나요?

 

 

 

 높은 점수를 받은 Solution 

Let's say you generate a bunch of views that are similar. You could set an OnClickListener for each view individually:

예를 들어, 유사한 뷰들을 생성했다고 가정해 봅시다. 각각의 뷰에 대해 OnClickListener를 개별적으로 설정해야 합니다.
button1.setOnClickListener(new OnClickListener ... );
button2.setOnClickListener(new OnClickListener ... );
 ...

 

Then you have to create a unique onClick method for each view even if they do the similar things, like:

그러면, 다음과 같이 유사한 작업을 수행하더라도 각 뷰에 대해 고유한 onClick 메서드를 생성해야 합니다.
public void onClick(View v) {
    doAction(1); // 1 for button1, 2 for button2, etc.
}

 

This is because onClick has only one parameter, a View, and it has to get other information from instance variables or final local variables in enclosing scopes. What we really want is to get information from the views themselves.

이는 onClick이 하나의 View 매개 변수만 가지고 있기 때문입니다. 다른 정보를 인스턴스 변수나 스코프 내부의 final 지역 변수에서 가져와야 합니다. 우리가 원하는 것은 뷰에서 정보를 가져오는 것입니다.

 

Enter getTag/setTag:

getTag/setTag 입력:
button1.setTag(1);
button2.setTag(2);

 

Now we can use the same OnClickListener for every button:

이제 모든 버튼에 대해 동일한 OnClickListener를 사용할 수 있습니다.
listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        doAction(v.getTag());
    }
};

 

It's basically a way for views to have memories.

그것은 기본적으로 뷰가 기억하는 방법입니다.

 

 

 

 가장 최근 달린 Solution 

Setting of TAGs is really useful when you have a ListView and want to recycle/reuse the views. In that way the ListView is becoming very similar to the newer RecyclerView.

TAG를 설정하는 것은 ListView가 있고 뷰를 재활용/재사용하려는 경우에 매우 유용합니다. 이를 통해 ListView는 새로운 RecyclerView와 매우 유사해집니다.
@Override
public View getView(int position, View convertView, ViewGroup parent)
  {
ViewHolder holder = null;

if ( convertView == null )
{
    /* There is no view at this position, we create a new one. 
       In this case by inflating an xml layout */
    convertView = mInflater.inflate(R.layout.listview_item, null);  
    holder = new ViewHolder();
    holder.toggleOk = (ToggleButton) convertView.findViewById( R.id.togOk );
    convertView.setTag (holder);
}
else
{
    /* We recycle a View that already exists */
    holder = (ViewHolder) convertView.getTag ();
}

// Once we have a reference to the View we are returning, we set its values.

// Here is where you should set the ToggleButton value for this item!!!

holder.toggleOk.setChecked( mToggles.get( position ) );

return convertView;
}

 

 

출처 : https://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view

반응형
댓글
공지사항
최근에 올라온 글