我有一个自定义列表适配器。这是:
public class FilesAdapter extends ArrayAdapter<PutioFileLayout> {
Context context;
int layoutResourceId;
List<PutioFileLayout> data = null;
public FilesAdapter(Context context, int layoutResourceId, List<PutioFileLayout> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
FileHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new FileHolder();
holder.textName = (TextView) row.findViewById(R.id.text_fileListName);
holder.textDescription = (TextView) row.findViewById(R.id.text_fileListDesc);
holder.imgIcon = (ImageView) row.findViewById(R.id.img_fileIcon);
row.setTag(holder);
} else {
holder = (FileHolder) row.getTag();
}
PutioFileLayout file = data.get(position);
holder.textName.setText(file.name);
holder.textDescription.setText(file.description);
holder.imgIcon.setImageResource(file.icon);
return row;
}
static class FileHolder {
TextView textName;
TextView textDescription;
ImageView imgIcon;
}
}
非常短而甜蜜。我的布局中每行都有Spinner
个,我希望用户能够点击它们,并获得每个项目的上下文菜单。如何在我的适配器中实现它?
我的row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dp" >
<ImageView
android:id="@+id/img_fileIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/img_fileIcon"
android:orientation="vertical" >
<TextView
android:id="@+id/text_fileListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="File name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<FrameLayout
android:id="@+id/descriptionFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="30dp" >
<TextView
android:id="@+id/text_fileListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File description" />
</FrameLayout>
</LinearLayout>
<Spinner
android:id="@+id/item_fileSpinner"
android:layout_width="44dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/spinner_background_ab_putio" />
</RelativeLayout>
答案 0 :(得分:2)
在getView()方法中,获取对微调器的引用,如下所示:
Spinner spn = row.findViewById(R.id.item_fileSpinner);
获得参考后,您可以通过创建包含所有值的SpinnerAdapter来设置其项目,并使用
进行设置spn.setAdapater(mSpinAdapter);
如果您能告诉我您希望填充微调器的数据,我可以为您提供一个更具体的示例,说明您如何创建和填充适配器。
然后剩下的就是设置一个OnItemSelectedListener,你可以这样做。
spn.setOnItemSelectedListner(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int spnPosition, long id){
//Do something
}
});
请注意在onItemSelected回调中使用spnPosition
。如果您使用position
,那么您将无法再访问getView()参数position
,因此请使用其他名称,以便在需要时可以同时访问/。
答案 1 :(得分:2)
为什么你不能使用createContextMenu()?万一你可以,覆盖onContextCreate()并在那里添加你的视图ID ....
我没有示例代码atm,但我会在星期一到达办公室。实际上,上下文菜单是最简单的方法。
然后,蒂姆的想法似乎已经成功了。
编辑: 正如星期一所承诺的,示例代码。
res \ menu \ context_menu.xml - 请注意您可以更改此名称。
<item
android:id="@+id/context_menu_call"
android:title="@string/context_menu_call"/>
<item
android:id="@+id/context_menu_video_call"
android:title="@string/context_menu_video_call"/>
<item
android:id="@+id/context_menu_send_im"
android:title="@string/context_menu_send_im"/>
<item
android:id="@+id/context_menu_call_number"
android:title="@string/context_menu_call_number"/>
<item
android:id="@+id/context_menu_view_profile"
android:title="@string/context_menu_view_profile"/>
然后在代码中
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
switch (v.getId())
{
case R.id.listConversations: //mainContactList from initMainLayoutElements()
contextList = mainContactList;
inflater.inflate(R.menu.context_menu, menu);
break;
case R.id.listContacts: //chatContacts from initChatLayoutElements()
contextList = chatContactList;
inflater.inflate(R.menu.context_menu, menu);
break;
case R.id.listContactsManagement: //contactList from initContactLayoutElements()
contextList = contactMgmtList;
inflater.inflate(R.menu.context_menu, menu);
break;
case R.id.btn_sms_settings:
inflater.inflate(R.menu.sms_menu, menu);
break;
}
}
现在我们已经定义了如何打开上下文菜单,我们需要定义实际项目的作用......
@Override
public boolean onContextItemSelected(MenuItem item)
{
item.getMenuInfo();
switch (item.getItemId())
{
case R.id.context_menu_call:
onCallStartFromContextMenu(false);
return true;
case R.id.context_menu_video_call:
onCallStartFromContextMenu(true);
return true;
case R.id.context_menu_send_im:
layoutState(LayoutState.CHAT);
Log.d(TAG, "Preparing chat session from context menu for " + getChattersFromList(contextList).toString());
chatSource = ChatSource.FROM_CONTEXT_MENU;
prepareChatSessionWith(getChattersFromList(contextList).toArray(new String[getChatterCount(contextList)]));
return true;
case R.id.context_menu_call_number:
Buddy selectedBuddy = (Buddy) contextList.getAdapter().getItem(contextList.getSelectedItemPosition());
lastCaller = selectedBuddy.toString();
showAlternateCallDialog(selectedBuddy, !selectedBuddy.getNumberHome().isEmpty(),
!selectedBuddy.getNumberMobile().isEmpty(), !selectedBuddy.getNumberOffice().isEmpty());
return true;
case R.id.context_menu_view_profile:
// Buddy who = buddyAdapter.getItem(listConversations.getSelectedItemPosition());
Buddy who = (Buddy) contextList.getAdapter().getItem(contextList.getSelectedItemPosition());
Log.d(TAG, "viewing profile " + who.toString());
lastProfileName = who.getIdentity();
showProfileScreen(who);
return true;
//transfers
default:
contextList = null;
return super.onContextItemSelected(item);
}
}
不要忘记将该组件注册到上下文菜单&#39;听众&#39;。这些是实际的ListView引用。
registerForContextMenu(mainContactList);
registerForContextMenu(chatContactList);
最后,唯一剩下的就是打开项目的上下文菜单......
chatContactList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d(TAG, "chatContacts.onItemLongClick fired!");
chatContactList.setItemChecked(position, true);
openContextMenu(chatContactList);
return true;
}
});
mainContactList.setOnItemLongClickListener(new ListView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d(TAG, "mainContactList.onItemLongClick fired!!!");
mainContactList.setItemChecked(position, true);
openContextMenu(mainContactList);
return false;
}
});
我更喜欢在LongClick上使用上下文菜单而不是onItemClicked,因为你可能想要点击(例如,文件选择和东西),并在长时间点击提供较少访问的功能,如重命名/删除等
所以你去吧。上下文菜单很简单;)