我为列表项创建了LinearLayout
,其中包含以下XML代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<TextView
android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="normal|bold"
android:textSize="16dip"
android:padding="5dip"
android:text="Title"
android:textColor="#001a90" />
<TextView
android:id="@+id/txtDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dip"
android:padding="5dip"
android:text="Description"
android:textColor="#000000"
android:textIsSelectable="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:onClick="myClickHandler" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="@+id/separator1"
android:visibility="visible"
android:background="@android:color/darker_gray"/>
</LinearLayout>
下面的Layout是包含列表的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_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/wallpaper2a"
android:id="@+id/rlMain">
<Button
android:layout_width="30dp"
android:layout_height="50dp"
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_width="50dp"
android:layout_height="50dp"
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_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_alignEnd="@+id/btn20"
android:layout_below="@+id/btn20"
android:layout_above="@+id/adView" />
<ImageButton
android:layout_width="25dp"
android:layout_height="50dp"
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_width="25dp"
android:layout_height="50dp"
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_width="40dp"
android:layout_height="50dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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 feed填充的,下面的代码将显示,我需要做的是在单击按钮时共享文本。我想使用该按钮,以便添加多个共享选项,例如facebook
,email
或whatsapp
。
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;
}
}
答案 0 :(得分: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了解我在示例代码中使用的视图持有者模式。