【问题标题】:How to change selected listview row color in android?如何在android中更改选定的listview行颜色?
【发布时间】:2014-04-27 14:43:47
【问题描述】:

我使用SimpleAdapter 生成了一个Listview。我想在单击时将行的背景颜色更改为黑色。当我在 Listview 中只有一行时,背景颜色设置为黑色。但是当我的 Listview 中有不止一行时,即使我单击第一行,最后一行的颜色也会变为黑色。任何人都可以提出任何解决方案吗? 我的代码如下:

    public class mycontacts extends Fragment {
     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.mycontacts_activity, container,
            false);
       SimpleAdapter k=new SimpleAdapter(getActivity(),val,R.layout.contact2,new String[]{"name","path","id"},new int[]{R.id.example_itemname,R.id.path,R.id.contactid})
    {

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

                final View v = super.getView(position, convertView, parent);
                final RelativeLayout l=(RelativeLayout)v.findViewById(R.id.front);
               swipy.setSwipeListViewListener(new BaseSwipeListViewListener(){

          @Override
        public void onClickFrontView(int position) {
        // TODO Auto-generated method stub
        super.onClickFrontView(position);
        l.setBackgroundColor(Color.BLACK);
            }
           });


                return v;
            }
    };
            swipy.setAdapter(k);


    return view;

          }

【问题讨论】:

    标签: android listview background simpleadapter


    【解决方案1】:

    在您的情况下,我会为 ListView 使用自定义适配器。您可以通过扩展 BaseAdapter 类来制作一个。看看这个教程

    tutorial about custom adapter

    您基本上可以将OnClickListener 分配给getView() 方法中的每个视图。

    OnClickListener:

    private class ItemClick implements OnClickListener{
      public void onClick(View v){
         v.setBackgroundColor(Color.BLACK);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多