3

我有一个自定义列表适配器。这里是:

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在每一行的布局中都有 s,我希望用户能够单击它们,并为每个项目获取一个上下文菜单。如何在我的适配器中实现这一点?

我的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>
4

2 回答 2

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
    }
});

注意spnPositiononItemSelected 回调中的使用。如果您使用position,那么您将无法再访问 getView() 参数position,因此请使用不同的名称,以便您可以访问两者/如果需要。

于 2012-07-29T03:16:35.000 回答
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);
    }
}

不要忘记将组件注册到上下文菜单“侦听器”。这些是实际的 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,因为您可能想在点击时做一些事情(比如文件选择和其他东西),并在长点击时提供访问较少的功能,比如重命名/删除等

所以你去。上下文菜单很简单;)

于 2012-07-29T03:50:35.550 回答