如何更改android中选定的listview行颜色?

时间:2014-04-27 14:43:48

标签: android listview background simpleadapter

我使用Listview生成了SimpleAdapter。我想将点击的行的背景颜色更改为黑色。当我在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;

          }

1 个答案:

答案 0 :(得分:0)

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

tutorial about custom adapter

您可以基本上将OnClickListenergetView()方法中的每个视图对齐。

OnClickListener:

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