如何永久更改Listview中行的背景颜色?

时间:2015-06-05 01:02:20

标签: android xml android-listview

我想在listview中更改行的背景颜色。现在我用

做这件事
  public void onItemClick(AdapterView<?> parent, final View view,
                                int position, long id) {


        view.setBackgroundColor(Color.parseColor("#ff0000"));//                

        }

哪个有效 - 但是当我向下滚动列表视图以使具有更改的背景颜色的行不在视线中,然后向上滚动时,该行不再具有更改的颜色。

有没有办法可以永久设置行的背景颜色,这样就不会发生?

2 个答案:

答案 0 :(得分:0)

为listView定义适配器,保存在内存中单击的项的索引值,并使用adapter.notifyDataSetChanged()在单击时重绘视图,根据索引值设置背景颜色。

这里有一些示例代码可以帮到你。我只是编写java代码,没有时间完成xml文件并将测试数据写入test.So如果这段代码有问题,请告诉我。

public class TestActivity extends Activity {
  ListView listView;
  ArrayList<String> dataList;
  int selectedIndex=-1;
  TestAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_activity);
    listView=(ListView)findViewById(R.id.listview);

    //init your dataList here
    adapter=new TestAdapter();
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedIndex=position;
            adapter.notifyDataSetChanged();
        }
    });
}




public class TestAdapter extends BaseAdapter{

    class ViewHolder{
        TextView title;
        LinearLayout background;
    }

    @Override
    public int getCount() {
        if(null==dataList){
            return 0;
        }else{
            return dataList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(null==convertView){
            convertView=getLayoutInflater().inflate(R.layout.list_test,null);
            ViewHolder holder=new ViewHolder();
            holder.title=(TextView)convertView.findViewById(R.id.title);
            holder.background=(TextView)convertView.findViewById(R.id.background);
            convertView.setTag(holder);
        }

        ViewHolder holder=(ViewHolder)convertView.getTag();
        String data=dataList.get(position);
        holder.title.setText(data);
        if(position == selectedIndex){
            holder.background.setBackgroundColor(Color.WHITE);
        }else{
            holder.background.setBackgroundColor(Color.BLUE);
        }
        return convertView;
    }
}

}

答案 1 :(得分:0)

您需要做的是使用您自己的ListView文件制作自定义row_layout.xmlCustomListViewObject使用boolean来检查是否点击了该元素或不是,最后是CustomListView来检查行元素是否被选中。

您的行布局row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="12dp"
        android:text="Large Text"
        android:id="@+id/textfield"/>

</LinearLayout>

这就是你的行类应该是这样的:

public class CustomRowClass {

    private String input;
    private boolean selected;

    public BallotEntryClass() {
    }

    public BallotEntryClass(String input, boolean selected) {
        this.input = input;
        this.selected = selected;
    }

    //setters and getters here
}

最后,您的适配器:

public class CustomListViewAdapter extends BaseAdapter {

    private ArrayList<CustomRowClass> listData;
    private LayoutInflater layoutInflater;
    private Context context;

    public CustomListViewAdapterBallot(Context context, ArrayList<CustomRowClass> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {

            convertView = layoutInflater.inflate(R.layout.row_layout, null);
            holder = new ViewHolder();

            holder.input = (TextView) convertView.findViewById(R.id.textfield);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textfield.setText(listData.get(position).getInput());

        //check here if state = 1 which means selected state
        if(listData.get(position).isSelected()){
            convertView.setBackgroundColor(Color.GREEN);
        }
        else{
            convertView.setBackgroundColor(Color.WHITE);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView input;
    }
}

就是这样。要使用它,请这样做:

ListView sampleListView = (ListView) findViewById(R.id.listView);

//create your listView with your custom object
ArrayList<CustomRowClass> data = new ArrayList<>();


for(int i = 0 ; i < 10 ; i ++){
    CustomRowClass entry = new CustomRowClass("Name " + i, false);
    data.add(entry);
}

//create your adapter, use the nameAndAgeList ArrayList
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, data);

//get your listView and use your adapter
listView.setAdapter(sampleListView);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CustomRowClass selection = (CustomRowClass) listView.getItemAtPosition(position);
        selection.setSelected(true);
        adapter.notifyDataSetChanged();
    }
});