android-如何为我的ListView项目添加微调功能?

android-如何为我的ListView项目添加微调功能?,第1张

概述我有一个自定义列表适配器.这里是:publicclassFilesAdapterextendsArrayAdapter<PutioFileLayout>{Contextcontext;intlayoutResourceId;List<PutioFileLayout>data=null;publicFilesAdapter(Contextcontext,intlayoutResourceId,List&lt

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

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

很短很甜.我在每一行的布局中都有Spinners,并且我希望用户能够单击它们,并为每个项目获取上下文菜单.如何在适配器中实现此功能

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

解决方法:

在您的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()参数的位置,因此请使用其他名称,以便您可以同时访问两个/两个.

总结

以上是内存溢出为你收集整理的android-如何为我的ListView项目添加微调功能?全部内容,希望文章能够帮你解决android-如何为我的ListView项目添加微调功能?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存