如何从 ListItem 内部调用按钮?

Posted

技术标签:

【中文标题】如何从 ListItem 内部调用按钮?【英文标题】:How to invoke button from inside ListItem? 【发布时间】:2016-01-06 01:24:03 【问题描述】:

我使用以下 XML 代码为列表项创建了 LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:padding="10dip">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_
        android:layout_
        android:textStyle="normal|bold"
        android:textSize="16dip"
        android:padding="5dip"
        android:text="Title"
        android:textColor="#001a90" />

    <TextView
        android:id="@+id/txtDescription"
        android:layout_
        android:layout_
        android:textStyle="bold"
        android:textSize="18dip"
        android:padding="5dip"
        android:text="Description"
        android:textColor="#000000"
        android:textIsSelectable="false" />

    <Button
        android:layout_
        android:layout_
        android:text="New Button"
        android:id="@+id/button"
        android:onClick="myClickHandler" />

    <View
        android:layout_
        android:layout_
        android:id="@+id/separator1"
        android:visibility="visible"
        android:background="@android:color/darker_gray"/>

</LinearLayout>

下面的布局是包含列表的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:background="@drawable/wallpaper2a"
    android:id="@+id/rlMain">

    <Button
        android:layout_
        android:layout_
        android:text="A-"
        android:id="@+id/btn20"
        android:textSize="20dp"
        android:background="@android:color/transparent"
        android:textColor="#000000"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:textStyle="bold"
        android:nestedScrollingEnabled="false" />

    <Button
        android:layout_
        android:layout_
        android:text="A+"
        android:id="@+id/btn30"
        android:textSize="30dp"
        android:onClick="setTextSizeUp"
        android:background="@android:color/transparent"
        android:textColor="#000000"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/btn20"
        android:textStyle="bold" />

    <ListView
        android:layout_
        android:layout_
        android:id="@android:id/list"
        android:layout_alignEnd="@+id/btn20"
        android:layout_below="@+id/btn20"
        android:layout_above="@+id/adView" />

    <ImageButton
        android:layout_
        android:layout_
        android:id="@+id/imageButton2"
        android:background="@android:color/transparent"
        android:src="@drawable/up"
        android:onClick="scrollListUp"
        android:layout_above="@android:id/list"
        android:layout_toStartOf="@+id/imageButton3" />

    <ImageButton
        android:layout_
        android:layout_
        android:id="@+id/imageButton3"
        android:background="@android:color/transparent"
        android:src="@drawable/down"
        android:onClick="scrollListDown"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/imageButton6" />

    <ImageButton
        android:layout_
        android:layout_
        android:id="@+id/imageButton6"
        android:background="@android:color/transparent"
        android:onClick="setTextColor"
        android:layout_above="@android:id/list"
        android:layout_toStartOf="@+id/btn30"
        android:src="@drawable/t" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_
        android:layout_
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

列表是从 rss 提要中填充的,下面的代码将显示该列表,我需要做的是在单击按钮时共享文本。我想使用该按钮,以便添加多个共享选项,例如facebookemailwhatsapp

private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list)
    
        try
        
            URL url = new URL(urlToRssFeed);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlreader = parser.getXMLReader();
            srcRSSParser theRssHandler = new srcRSSParser(list);

            xmlreader.setContentHandler(theRssHandler);

            InputSource is = new InputSource(url.openStream());

            xmlreader.parse(is);
        
        catch (Exception e)
        
            e.printStackTrace();
        
    

    private class RetrieveRSSFeeds extends AsyncTask<Integer, Void, Void>
    
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Integer... params) 
            retrieveRSSFeed("..........................",itemlist);

            rssadaptor = new RSSListAdaptor(rssfeedFragment.this.getActivity(), R.layout.rssitemview,itemlist,params[0]);

            return null;
        

        @Override
        protected void onCancelled() 
            super.onCancelled();
        

        @Override
        protected void onPreExecute() 
            progress = ProgressDialog.show(
                    rssfeedFragment.this.getActivity(), null, "...");

            super.onPreExecute();
        

        @Override
        protected void onPostExecute(Void result) 
            setListAdapter(rssadaptor);

            progress.dismiss();

            super.onPostExecute(result);
        

        @Override
        protected void onProgressUpdate(Void... values) 
            super.onProgressUpdate(values);
        
    

    private class RSSListAdaptor extends ArrayAdapter<RSSItem> 
        private List<RSSItem> objects = null;
        private Integer txtSize;

        public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects,Integer txtSize) 
            super(context, textviewid, objects);

            this.objects = objects;
            this.txtSize = txtSize;
        

        @Override
        public int getCount() 
            return ((null != objects) ? objects.size() : 0);
        

        @Override
        public long getItemId(int position) 
            return position;
        

        @Override
        public RSSItem getItem(int position) 
            return ((null != objects) ? objects.get(position) : null);
        

        public View getView(int position, View convertView, ViewGroup parent) 
            View view = convertView;

            if(null == view)
            
                LayoutInflater vi = (LayoutInflater)rssfeedFragment.this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.rssitemview, null);
            

            RSSItem data = objects.get(position);

            if(null != data)
            
                TextView title = (TextView) view.findViewById(R.id.txtTitle);
                //TextView date = (TextView)view.findViewById(R.id.txtDate);
                TextView description = (TextView) view.findViewById(R.id.txtDescription);

                title.setText(data.title);
                //date.setText("on " + data.date);
                description.setText(data.description);
                description.setTextSize(txtSize);
                if (txtColor == 0)
                
                    description.setTextColor(Color.BLACK);
                else
                
                    description.setTextColor(Color.WHITE);
                
            

            return view;
        
    

【问题讨论】:

【参考方案1】:

一种方法是创建自定义列表适配器。因此,您必须使用 ArrayAdapter 扩展您的适配器类(例如)。

然后在您的 getView(...) 中,您可以为每一行的按钮分配一个 onClickListener:

public class EventListAdapter extends ArrayAdapter<EventEntry> 

Context context;
List<EventEntry> itemList;
int layoutResID;

public EventListAdapter(Context context, int layoutResourceID, List<EventEntry> listItems) 
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.itemList = listItems;
    this.layoutResID = layoutResourceID;


public void setCourseView(boolean courseView) 
    this.courseView = courseView;


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    // TODO Auto-generated method stub

    ItemHolder itemHolder;

    if (convertView == null) 
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        itemHolder = new ItemHolder();

        convertView = inflater.inflate(layoutResID, parent, false);
        itemHolder.button = (Button) convertView.findViewById(R.id.button);
     else 
        itemHolder = (ItemHolder) convertView.getTag();
    
    EventEntry item = (EventEntry) this.itemList.get(position);

    itemHolder.button.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
//your stuff goes here. Maybe a callback to your activity
        
    );
...

有关自定义适配器的更多信息:http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

另请参阅 section 9.4 以了解我的示例代码中使用的视图持有者模式。

【讨论】:

以上是关于如何从 ListItem 内部调用按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何从按钮外部调用 UIButton 图像上的 UIView 转换?

使用按钮从视图内部调用 httppost actionresult

Android 样式/阴影正确 ListItem 布局

如何从Microsoft.SharePoint.Client.ListItem检索所有属性?

SwiftUI:ListItem 手势

动态创建一个由 N 个 ListItemTexts 组成的 ListItem