处理的ImageButton自定义列表视图中点击(Handling imageButton clic

2019-10-18 02:39发布

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

最喜欢的活动

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>

Answer 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);


Answer 2:

好吧,你在初始化ListView的按钮的点击听众onItemClickListener()当然它一定会单击行后,开始工作。

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



文章来源: Handling imageButton clicks inside a custom listview