【问题标题】:Handling imageButton clicks inside a custom listview处理自定义列表视图中的 imageButton 点击
【发布时间】:2013-05-09 11:11:05
【问题描述】:

我有一个带有自定义适配器的列表视图。 listView 中的自定义行包含 2 个图像按钮,当我单击它时需要处理它们。 但它并不能像我想要的那样工作,因为当我启动应用程序并单击 imagebutton 时没有任何反应,但是如果我先单击行然后单击 imageButton,那么我的单击正在处理中。 我在这里尝试了一些可以帮助其他用户的解决方案,但是没有人遇到与我完全相同的问题,即在单击行后得到处理。

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

最喜欢的类

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>

【问题讨论】:

    标签: android android-listview imagebutton custom-adapter


    【解决方案1】:

    将您的两个按钮添加到 getView 方法中,并将它们包含在您的处理程序中,然后在通过 getView 方法时,您希望像这样分配一个点击侦听器:

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

    所以你的ViewHolder 类将如下所示:

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

    然后您按照其他视图在getView() 方法中设置按钮

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

    【讨论】:

    • 这行得通。但是现在单击图像按钮并没有给我名字。将其更改为holder.name,不再需要与持有人在一起吗?使用String btnmsg = "getting delete for " + holder.name;
    【解决方案2】:

    好吧,您正在初始化 ListView 的onItemClickListener() 中按钮的单击侦听器,当然它只有在您单击该行后才会起作用。

    您需要在getView() 方法中设置这些。但是请注意,在 ListView 项目中提供可点击项目并不是真正的标准做法。

    【讨论】:

      猜你喜欢
      • 2015-07-16
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2017-09-03
      相关资源
      最近更新 更多