在OnItemClickListener上的适配器内部访问imageview [英] Accessing imageview inside adapter on OnItemClickListener

查看:78
本文介绍了在OnItemClickListener上的适配器内部访问imageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的导航抽屉包含许多项目.每个项目都有不可见的背景图像(ID为 optionbackground ).我想使图像仅在用户单击项目时可见.我该怎么做?我要更改其可见性的ImageView在Adapter中名为 optionBackground .我的 setOnItemClickListener 正在活动中,我正在为导航抽屉设置自定义适配器.

My navigation drawer consists of many items. Each item has background image(with id optionbackground) which is invisible. I want to make the image visible only when user clicked on the item. How can i do it? ImageView whose visiblity I want to change is named optionBackground in Adapter. And my setOnItemClickListener is in activity in which i am setting the custom adapter for navigation drawer.

活动

mDrawerList.setAdapter(new CustomAdapter(this, mdrawerTitles));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if(position==0)
    {
      ...
    }
    else if(position==1)
    {
      ...
    }
  }
});

适配器

public class CustomAdapter extends ArrayAdapter
{
    private final Context context;
private String[] optionList;
    public CustomAdapter(Context context,String[] data)
    {
       super(context, R.layout.drawer_list_item);
       this.context = context;
        optionList=data;
    }
    @Override
    public int getCount() {
        return optionList.length;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View v = inflater.inflate(R.layout.drawer_list_item, parent, false);
        ImageView icon=(ImageView)v.findViewById(R.id.optionlogo);
        TextView optiontext=(TextView)v.findViewById(R.id.optiontext);
        TextView optionsubtext=(TextView)v.findViewById(R.id.optionsubtext);
        ImageView optionBackground=(ImageView)v.findViewById(R.id.optionbackground);

        Log.e("bhuvnesh", optionList[position]);
        if(optionList[position].equals("ABOUT")) {


            optiontext.setText("AB");
            icon.setImageResource(R.drawable.g_icon);
            optionsubtext.setText("The what");
        }
        else if(optionList[position].equals("SHAR")) {
            optiontext.setText("SHA");
            icon.setImageResource(R.drawable.s_icon);
            optionsubtext.setText("Tell your ");
        }
        else if(optionList[position].equals("CONNEC")) {
            optiontext.setText("CONN");
            icon.setImageResource(R.drawable.fb_icon);
            optionsubtext.setVisibility(View.GONE);
        }
        else if(optionList[position].equals("LOG")) {
            optiontext.setText("LOGOUT");
            icon.setImageResource(R.drawable.l_icon);
            optionsubtext.setVisibility(View.GONE);
        }


        return v;
    }
}

推荐答案

有多种解决方案可以解决此问题.

There are multiple solutions to solve this issue.

您可以通过 onClickListener

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   view.findViewById(R.id.optionbackground).setVisibility(View.VISIBLE);
   ...
}

或者您在适配器内定义 onClickListener ,并保留这些视图为最终视图.因此,您可以在 onClick 事件中设置可见性.

or you define the onClickListener inside your adapter and keep those views final. So you can set the visibility inside the onClick event.

View v = inflater.inflate(R.layout.drawer_list_item, parent, false);
...
final ImageView optionBackground=(ImageView)v.findViewById(R.id.optionbackground);

v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        optionBackground.setVisibility(View.VISIBLE);
    }
});

...


如果您打算更新到RecyclerView.该视图不再带有onItemClickListener,并且您需要将Listener添加到Adapter内部的视图中.


If you plan to update to the RecyclerView. This view does not come with an onItemClickListener anymore, and you will need to add the Listener to the view inside the Adapter.

您仍然可以将其传递给包含的活动或片段,就像我在一个开放源代码项目中所做的那样:

You can still pass it out to the containing activity or fragment, as i've done it in one of my Open Source projects:

https://github.com/mikepenz/wallsplash-android/blob/master/app/src/main/java/com/mikepenz/unsplash/views/adapters/ImageAdapter.java#L178

有关在触摸上显示和隐藏它的其他信息

For the additional info on showing it and hiding it onTouch

.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //press
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                //unpress
                break;
        }

        // Pass all events through to the host view.
        return false;
    }
}

这篇关于在OnItemClickListener上的适配器内部访问imageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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