2011-08-11 2 views
1

그래서 클래스는 다음과 같습니다 BaseAdaper을 확장했다 : 내 활동에안드로이드에서 GridLayout과 컨텍스트 메뉴를 표시하는 방법

public class ProfileTileAdapter extends BaseAdapter { 

private Context context; 
private ForwardingProfile[] profiles; 

public ProfileTileAdapter(Context context, ForwardingProfile[] profiles) { 
    this.context = context; 
    this.profiles = profiles; 
} 

@Override 
public int getCount() { 
    return profiles.length; 
} 

@Override 
public Object getItem(int position) { 
    return profiles[position]; 
} 

@Override 
public long getItemId(int position) { 
    return profiles[position].getID(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup arg2) { 
    ProfileTile tile = null; 
    if (convertView == null) { 
     tile = new ProfileTile(context, profiles[position]); 
     LayoutParams lp = new GridView.LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
     tile.setLayoutParams(lp); 
    } else { 
     tile = (ProfileTile) convertView; 
    } 
    return tile; 
} 

}

A A GridLayout과를 가지고 있고 그것의 어댑터를 설정 ProfileTileAdapter의 인스턴스 내 활동에서 사용자가 오랫동안 뷰 (이 경우 ProfileTile) 중 하나를 길게 누르면 상황에 맞는 메뉴를 열고 싶지만 어떻게해야할지 모르겠다. 또한 사용자가 컨텍스트 메뉴에서 옵션을 선택할 때 길게 눌러 진 ProfileTile을 찾아야합니다. 모든 자습서는 액티비티의 정적 뷰를 사용하여 계속 수행하지만 이와는 다릅니다.

답변

4

그래서 나는 대답을 알아 냈습니다. 그래서 분명히 Activity.registerForContextMenu(GridView)을 사용하여 Activity의 상황에 맞는 메뉴를 위해 GridView를 등록하면 어댑터에서 독립적으로 반환하는 각보기가 등록됩니다. 그래서 이것은 활동 (어댑터가 변경되지 않고 유지)처럼 보이는 방법입니다

public class SMSForwarderActivity extends Activity { 
private GridView profilesGridView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.main); 
    setUpProfilesGrid(); 
} 

    private void setUpProfilesGrid() { 
    profilesGridView = (GridView) this.findViewById(R.id.profilesGrid); 
    this.registerForContextMenu(profilesGridView); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) menuInfo; 
    ProfileTile tile = (ProfileTile) aMenuInfo.targetView;//This is how I get a grip on the view that got long pressed. 
} 

    @Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item 
      .getMenuInfo(); 
    ProfileTile tile = (ProfileTile) info.targetView;//Here we get a grip again of the view that opened the Context Menu 
    return super.onContextItemSelected(item); 
} 

}

그래서 해결책은 아주 간단했다, 그러나 때때로 우리는 이상의 일을 복잡하게.