处理的ImageButton自定义列表视图里面点击 [英] Handling imageButton clicks inside a custom listview

查看:141
本文介绍了处理的ImageButton自定义列表视图里面点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的适配器列表视图。在ListView中自定义行containts 2 imagebuttons需要处理的东西,当我点击它。
但它不能很好地工作就像我想要的,因为当我启动应用程序,并单击ImageButton的则什么也不会发生,但如果我先点击行,然后在ImageButton的,那么我的点击被处理。
我尝试了一些解决方案,帮助其他用户在这里SO,但没有人有完全相同的问题,因为我认为它被连续点击后处理。

FavoriteActivity

 公共类FavoriteActivity延伸活动{    私人列表<喜欢> favoriteItem =新的ArrayList<喜欢>();
    ViewHolder持有人=新ViewHolder();
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.favorite);        populateFavoriteList();
        populateListview();
        registerClickCallBack();
    }
    私人无效populateFavoriteList(){
        favoriteItem.add(新宠(R.drawable.icon_camera项目1));
        favoriteItem.add(新宠(R.drawable.icon_camera,项目2));
        favoriteItem.add(新宠(R.drawable.icon_camera项目3));
        favoriteItem.add(新宠(R.drawable.icon_camera项目4));
        favoriteItem.add(新宠(R.drawable.icon_camera项目5));
        favoriteItem.add(新宠(R.drawable.icon_camera项目6));
        favoriteItem.add(新宠(R.drawable.icon_camera,以7));
        favoriteItem.add(新宠(R.drawable.icon_camera,第8项));
    }    私人无效populateListview(){
        ArrayAdapter<喜欢>适配器=新MyListAdapter();
        ListView控件列表=(ListView控件)findViewById(R.id.favoriteList);
        list.setAdapter(适配器);
    }    私有类MyListAdapter扩展ArrayAdapter<喜欢> {
        公共MyListAdapter(){
            超(FavoriteActivity.this,R.layout.favorite_row,favoriteItem);
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            //要确保我们有一个观点,因为空是可能的
            查看ItemView控件= convertView;
            如果(ItemView控件== NULL){
                。ItemView控件= getLayoutInflater()膨胀(R.layout.favorite_row,父母,假);
            }            //查找与合作项目
            收藏CURRENTITEM = favoriteItem.get(位置);            //灌装查看
            holder.thumbnail =(的ImageButton)itemView.findViewById(R.id.favoriteThumbnail);
            holder.thumbnail.setBackgroundResource(currentItem.getThumbnail());            holder.name =(TextView中)itemView.findViewById(R.id.favoriteName);
            holder.name.setText(currentItem.getName());
            返回ItemView控件;
        }
    }    私人无效registerClickCallBack(){        ListView控件列表=(ListView控件)findViewById(R.id.favoriteList);
        list.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,查看viewClicked,INT位置,长的id){                最终收藏favoriteClicked = favoriteItem.get(位置);
                弦乐味精=点击位置+位置+,这是+ favoriteClicked.getName(); //我可以用我持有人这里得到的名字后面?因为它不工作...
                Toast.makeText(FavoriteActivity.this,味精,Toast.LENGTH_LONG).show();                的ImageButton btnDirections =(的ImageButton)viewClicked.findViewById(R.id.favoriteDirections);
                的ImageButton btnDelete =(的ImageButton)viewClicked.findViewById(R.id.favoriteDelete);                btnDirections.setOnClickListener(新OnClickListener(){
                    字符串btnmsg =获取路线为+ favoriteClicked.getName();
                    @覆盖
                    公共无效的onClick(视图v){
                        Toast.makeText(FavoriteActivity.this,btnmsg,Toast.LENGTH_LONG).show();
                    }
                });                btnDelete.setOnClickListener(新OnClickListener(){
                    字符串btnmsg =越来越删除+ favoriteClicked.getName();
                    @覆盖
                    公共无效的onClick(视图v){
                        Toast.makeText(FavoriteActivity.this,btnmsg,Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }    静态类ViewHolder {
        的ImageButton缩略图;
        TextView的名称;
    }
}

FavoriteClass

 公共类收藏{    私人诠释favoriteThumbnail;
    私人字符串favoriteName;    市民最喜爱(INT favoriteThumbnail,弦乐favoriteName){
        超();
        this.favoriteThumbnail = favoriteThumbnail;
        this.favoriteName = favoriteName;
    }    公众诠释getThumbnail(){
        返回favoriteThumbnail;
    }    公共字符串的getName(){
        返回favoriteName;
    }
}

favorite_row.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:weightSum =4
    机器人:descendantFocusability =blocksDescendants>    <的ImageButton
        机器人:layout_width =75dp
        机器人:layout_height =65dp
        机器人:contentDescription =缩略图
        机器人:SRC =@绘制/ icon_camera
        机器人:layout_weight =2
        机器人:ID =@ + ID / favoriteThumbnail
        机器人:scaleType =fitXY
        风格=@风格/ favoriteRowItems
        机器人:可聚焦=FALSE/>    <的TextView
        机器人:ID =@ + ID / favoriteName
        机器人:layout_weight =2
        机器人:layout_gravity =center_vertical
        机器人:文字=姓名
        机器人:TEXTSIZE =30sp
        风格=@风格/ favoriteRowItems/>    <的ImageButton
        机器人:contentDescription =方向
        机器人:layout_weight =0.5
        机器人:SRC =@绘制/ icon_directions
        机器人:ID =@ + ID / favoriteDirections
        风格=@风格/ favoriteRowItems
        机器人:可聚焦=FALSE/>    <的ImageButton
        机器人:contentDescription =从最爱删除
        机器人:layout_weight =0.5
        机器人:SRC =@绘制/ icon_delete
        机器人:ID =@ + ID / favoriteDelete
        风格=@风格/ favoriteRowItems
        机器人:layout_marginRight =5DP
        机器人:可聚焦=FALSE/>< / LinearLayout中>


解决方案

你的两个按钮添加到 getView 法,包括他们在您的处理程序,然后在一通通过 getView 方法,你要分配一个点击监听器是这样的:

  holder.btnDelete.setOnClickListener(新OnClickListener(){
    字符串btnmsg =越来越删除+ favoriteClicked.getName();
    @覆盖
    公共无效的onClick(视图v){
      Toast.makeText(FavoriteActivity.this,btnmsg,Toast.LENGTH_LONG).show();
    }
 });

所以你的 ViewHolder 类将是这样的:

 静态类ViewHolder {
        的ImageButton缩略图;
        TextView的名称;
        的ImageButton btnDelete;
        的ImageButton btnDirections;
    }

和您设置在 getView()方法的按钮按其他视图

  holder.btnDirections =(的ImageButton)itemView.findViewById(R.id.favoriteDirections);
holder.btnDelete =(的ImageButton)itemView.findViewById(R.id.favoriteDelete);

I have a listview with a custom Adapter. A custom row in the listView containts 2 imagebuttons that need to handle a something when I click on it. But it doesn't work well like I want, because when I start the app and click on the imagebutton then nothing happens, but if I first click on the row and then on the imageButton, then my click is being handled. I tried a few solutions that helped other users here SO, but nobody had the exact same problem as me that it gets handled after a row click.

FavoriteActivity

public class FavoriteActivity extends Activity {

    private List<Favorite> favoriteItem = new ArrayList<Favorite>();
    ViewHolder holder = new ViewHolder();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorite);

        populateFavoriteList();
        populateListview();
        registerClickCallBack();
    }


    private void populateFavoriteList() {
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 1"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 2"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 3"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 4"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 5"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 6"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 7"));
        favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 8"));
    }

    private void populateListview() {
        ArrayAdapter<Favorite> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.favoriteList);
        list.setAdapter(adapter);
    }

    private class MyListAdapter extends ArrayAdapter<Favorite> {
        public MyListAdapter() {
            super(FavoriteActivity.this, R.layout.favorite_row, favoriteItem);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // To be sure we have a view, because null is possible
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.favorite_row, parent, false);
            }

            // Find the item to work with
            Favorite currentItem = favoriteItem.get(position);

            // Filling the View         
            holder.thumbnail = (ImageButton) itemView.findViewById(R.id.favoriteThumbnail);
            holder.thumbnail.setBackgroundResource(currentItem.getThumbnail());

            holder.name = (TextView) itemView.findViewById(R.id.favoriteName);
            holder.name.setText(currentItem.getName());
            return itemView;
        }
    }

    private void registerClickCallBack() {

        ListView list = (ListView) findViewById(R.id.favoriteList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {           

                final Favorite favoriteClicked = favoriteItem.get(position);
                String msg = "Clicked position " + position + " which is " + favoriteClicked.getName();//Can I use my holder here to get the name back?? Because it wasn't working...               
                Toast.makeText(FavoriteActivity.this, msg, Toast.LENGTH_LONG).show();

                ImageButton btnDirections = (ImageButton) viewClicked.findViewById(R.id.favoriteDirections);
                ImageButton btnDelete = (ImageButton) viewClicked.findViewById(R.id.favoriteDelete);                

                btnDirections.setOnClickListener(new OnClickListener() {
                    String btnmsg = "getting directions for " + favoriteClicked.getName();
                    @Override
                    public void onClick(View v) {                       
                        Toast.makeText(FavoriteActivity.this, btnmsg, Toast.LENGTH_LONG).show();                        
                    }
                });

                btnDelete.setOnClickListener(new OnClickListener() {
                    String btnmsg = "getting delete for " + favoriteClicked.getName();
                    @Override
                    public void onClick(View v) {                       
                        Toast.makeText(FavoriteActivity.this, btnmsg, Toast.LENGTH_LONG).show();                        
                    }
                });             
            }           
        });         
    }

    static class ViewHolder {
        ImageButton thumbnail;
        TextView name;
    }
}

FavoriteClass

public class Favorite {

    private int favoriteThumbnail;
    private String favoriteName;    

    public Favorite(int favoriteThumbnail, String favoriteName){
        super();
        this.favoriteThumbnail = favoriteThumbnail;
        this.favoriteName = favoriteName;
    }

    public int getThumbnail(){
        return favoriteThumbnail;
    }

    public String getName(){
        return favoriteName;
    }
}

favorite_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4" 
    android:descendantFocusability="blocksDescendants">

    <ImageButton 
        android:layout_width="75dp"
        android:layout_height="65dp"
        android:contentDescription="thumbnail"
        android:src="@drawable/icon_camera"
        android:layout_weight="2"
        android:id="@+id/favoriteThumbnail"
        android:scaleType="fitXY"
        style="@style/favoriteRowItems"
        android:focusable="false"/>

    <TextView
        android:id="@+id/favoriteName"
        android:layout_weight="2"
        android:layout_gravity="center_vertical"
        android:text="Name"
        android:textSize="30sp"
        style="@style/favoriteRowItems"/>

    <ImageButton 
        android:contentDescription="Direction"
        android:layout_weight="0.5"
        android:src="@drawable/icon_directions"
        android:id="@+id/favoriteDirections"
        style="@style/favoriteRowItems"
        android:focusable="false"/>

    <ImageButton
        android:contentDescription="Delete from favorite" 
        android:layout_weight="0.5"
        android:src="@drawable/icon_delete"
        android:id="@+id/favoriteDelete"
        style="@style/favoriteRowItems"
        android:layout_marginRight="5dp"
        android:focusable="false"/>  

</LinearLayout>

解决方案

Add your two buttons to the getView method, and include them in your handler, then on a pass through the getView method you want to assign a click listener like this:

holder.btnDelete.setOnClickListener(new OnClickListener() {
    String btnmsg = "getting delete for " + favoriteClicked.getName();
    @Override
    public void onClick(View v) {                       
      Toast.makeText(FavoriteActivity.this, btnmsg, Toast.LENGTH_LONG).show();
    }
 }); 

So your ViewHolder class will look like this:

  static class ViewHolder {
        ImageButton thumbnail;
        TextView name;
        ImageButton btnDelete;
        ImageButton btnDirections;
    }

and you set up the buttons in the getView() method as per the other Views

holder.btnDirections=(ImageButton)itemView.findViewById(R.id.favoriteDirections);
holder.btnDelete=(ImageButton)itemView.findViewById(R.id.favoriteDelete);

这篇关于处理的ImageButton自定义列表视图里面点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆