Android:ListView - 在选择项目时切换图像的可见性

时间:2014-02-25 13:25:39

标签: android

我有一个包含文字和图片的列表视图。我想要实现的是当我选择一个列表项时,我需要只将行中的相关图像设置为可见。当我选择另一个列表时,先前关联的图像应该是不可见的,当前的图像应该是可见的(类似于ListView.CHOICE_MODE_SINGLE)。

请允许任何人给我一个关于同样的开端。如果有人想要更多解释,请告诉我。

编辑:我的自定义适配器类

public class CustomListAdapter extends ArrayAdapter<String> {

    private Context context;
    private List<String> list = null;
    private LayoutInflater layoutInflator;
    private Typeface typeFaceRegular;

    //constructor
    public CustomListAdapter(Context context, int textViewResourceId, List<String> list) {
        super(context, textViewResourceId, list);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (view == null) {
            layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflator.inflate(R.layout.launcher_custom_list, parent, false);
        }

        String info = list.get(position);

        if (info != null) {
            typeFaceRegular = Typeface.createFromAsset(context.getAssets(), "Roboto/Roboto-Regular.ttf");
            TextView txtvwLauncherListText = (TextView) view.findViewById(R.id.txtvwLauncherListText);
            txtvwLauncherListText.setText(info);
            txtvwLauncherListText.setTypeface(typeFaceRegular);
        }

        return view;
    }
}

1 个答案:

答案 0 :(得分:4)

为列表视图添加项目点击监听器,并在其中执行

{
adapter.SetSelectedPos(position);
adapter.notifyDatasetChanged);
}

adapter.SetSelectedPos(int pos) // pos comes from the above listener
{
 _pos = pos; //_pos define in adapter class
}


// in getView do
if(_pos == position)
{
//Show image
}
else
{
// hide image (this hide is important and required)
}