单击ListView中的ImageButton

时间:2013-01-29 15:02:40

标签: android

我有一个ListView。每行都是自定义布局,包含一些文本视图和2个图像按钮。每个教程/主题/文档/等。我试过,没有任何关于我的工作,即使你测试的教程也是完美的。所以,我填充ListView,比我想要的那样通过点击行x上的图像按钮来至少向我展示吐司或做某事,就像它应该的那样。

以下是代码(MainActivity.java)。

package ro.pca.pizzarecipes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity{
    protected AdView adView;
    public ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdRequest re = new AdRequest();
        re.setGender(AdRequest.Gender.FEMALE);
        adView = (AdView)findViewById(R.id.ad);
        adView.setVisibility(AdView.VISIBLE);
        adView.loadAd(re);

        ArrayList<HashMap<String, String>> map = new ArrayList<HashMap<String, String>>();
        HashMap mapItem1 = new HashMap();
        mapItem1.put("Nume","Neapolitan style (alla Napoletana)");
        mapItem1.put("Timp","30");
        mapItem1.put("Calorii","");
        mapItem1.put("Ingrediente","1 1/2 cups warm water...");
        mapItem1.put("Preparare"," 1. In the bowl...");
        mapItem1.put("Altele","Makes 8 servings.");
        map.add(mapItem1);

        HashMap mapItem2 = new HashMap();
        mapItem2.put("Nume","Pan fried Hawaiian");
        mapItem2.put("Timp","20");
        mapItem2.put("Calorii","");
        mapItem2.put("Ingrediente","Olive oil...");
        mapItem2.put("Preparare"," Preheat...");
        mapItem2.put("Altele","Makes 2 pizzas.");
        map.add(mapItem2);


        SimpleAdapter adapter = null;   
        adapter = new SimpleAdapter(this, map, R.layout.customlistlayout,
                new String[]{"Nume", "Timp", "Calorii", "Ingrediente", "Preparare", "Altele"}, 
                new int[]{R.id.textView1, R.id.textView2, R.id.textView4, R.id.textView7, R.id.textView9, R.id.textView10}); 


        Collections.sort(map, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                String name1 = o1.get("Nume");
                String name2 = o2.get("Nume");
                if (name1 == null) return -1;
                return name1.compareTo(name2);
            }
        });

        setContentView(R.layout.activity_main);
        lv = (ListView)findViewById(R.id.listView1);
        lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
        lv.setAdapter(adapter);   



    }

}   

主要布局(activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <com.google.ads.AdView
            android:id="@+id/ad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="a150d19cc5b81d8"
            ads:loadAdOnCreate="true" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"   
        android:text="my fridge" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:divider="#C60202"
        android:dividerHeight="3dip"
        android:layout_below="@+id/button1" >
    </ListView>

</RelativeLayout>

行布局(customlistlayout.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toLeftOf="@+id/textView2"
        android:text="time (min):"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toLeftOf="@+id/textView4"
        android:text="calories:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView5"
        android:text="ingredients"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView6"
        android:layout_marginLeft="18dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView7"
        android:text="preparation"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_below="@+id/textView8"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageButton1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/ic_launcher"
        android:tag="2" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView9"
        android:text="other info:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold|italic" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView10"
        android:layout_marginTop="20dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/googletranslate"
        android:tag="1" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView9"
        android:layout_below="@+id/textView11"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@layout/gradient"
        android:text="nume"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp"
        android:textStyle="bold" />

</RelativeLayout>

和一点渐变布局(gradient.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#A0C2F2"      
      android:endColor="#D0E6F2"
      android:angle="135" />
</shape>

4 个答案:

答案 0 :(得分:1)

您需要扩展列表适配器类,然后在getView方法中为每个ImageButton设置OnClickListener。

答案 1 :(得分:0)

试试这个,

而不是ImageButton使用TextView。我在ListView Button点击时遇到了同样的问题。它会起作用

答案 2 :(得分:0)

我完全同意@Dan,而是使用 BaseAdapter 并在其上 getView 设置图像按钮的监听器

//example
class Example extends BaseAdapter{
     Context context;

        public CardAdapt(Context context) {
            this.context = context;

        }

        public int getCount() {
            return //size of the list u have with the button
        }

        public Card getItem(int position) {
            return //position of the button
        }

        public long getItemId(int position) {
            return position;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewHolder viewHolder;
            Card card;

            if (convertView == null) {
                 viewHolder                 = new ViewHolder();
                 convertView                = inflater.inflate(R.layout.//ur layout, null);


                 viewHolder.imgbtn (ImageButton) convertView.findViewById(R.id.//id for the imagebutton);
             convertView.setTag(viewHolder);
            }  

            viewHolder   = (ViewHolder) convertView.getTag();




            return convertView;
        }
        private class ViewHolder{

            ImageButton imgbtn;


        }
//the do the onclicklistner for the button and do what u want

答案 3 :(得分:0)

根据this

问题应该通过在行布局(customlistlayout.xml)

中添加此属性来解决
...
<RelativeLayout
    ...
    android:descendantFocusability="blocksDescendants" >