【问题标题】:How do I permanently change the background color of a row in a Listview?如何永久更改 Listview 中一行的背景颜色?
【发布时间】:2015-06-05 01:02:19
【问题描述】:

我想更改列表视图中一行的背景颜色。现在我正在使用

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


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

        }

这可行 - 但是当我在我的列表视图上向下滚动以使背景颜色更改的行看不见,然后向上滚动时,该行不再具有更改的颜色。

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

【问题讨论】:

  • 您必须实现自己的自定义列表视图适配器,如果您的列表视图类有一个布尔值,如果它被选中,然后在您的自定义列表视图适配器的getView 函数中管理背景颜色。这样,您就可以更好地跟踪选择了哪个元素。
  • 我猜您的 View 在离开可视屏幕后会被回收/状态清除。您需要将状态保存在某种数据模型或所选项目的视图标签中。

标签: android xml android-listview


【解决方案1】:

你要做的是用你自己的row_layout.xml文件创建一个自定义ListView,一个CustomListViewObject有一个boolean来检查元素是否被点击,最后,一个@987654325 @ 检查是否选择了行元素。

你的行布局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();
    }
});

【讨论】:

  • 这是有道理的,而且奏效了!非常感谢!现在我真的可以睡几个小时了。
【解决方案2】:

为你的listView定义一个adapter,将被点击的item的索引值保存在内存中,点击时使用adapter.notifyDataSetChanged()重绘视图,根据索引值设置背景色。

这里有一些示例代码可能对你有帮助。我只是写java代码,没有时间完成xml文件和编写测试数据进行测试。所以如果这段代码有问题,请告诉我。

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;
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多