更改listview行的颜色

时间:2014-11-01 12:11:03

标签: android listview

让我简单解释一下。

我有两个碎片:

1)片段A用户输入一些文本。在这里,我还定义了5个不同颜色的按钮。从这里,输入的文本将添加到数据库中。

2)片段B,它有一个列表视图,当用户点击"保存"时,使用customadapter从该数据库填充数据。片段A中的按钮。

一切都很好。正在保存数据,将其加载到Listview和所有数据中。现在还记得5种不同颜色的5个按钮吗?

我想要的是假设在片段A中添加数据时,用户选择的按钮颜色为"橙色"假设,那么将在适配器的getView()方法中膨胀的行也应该具有其背景橙色。 (Google Keep的东西)

这可能吗?

我的适配器类:

public class notesAdapter extends BaseAdapter {

ArrayList<notesSingleRow> notes;
Context context;
View convertView;
private static final String TAG = "SampleAdapter";
private final LayoutInflater mLayoutInflater;
private final Random mRandom;
private SparseBooleanArray mSelectedItemsIds;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

public notesAdapter(Context context, ArrayList<notesSingleRow> notes) {
    this.notes = notes;
    this.context = context;
    this.mLayoutInflater = LayoutInflater.from(context);
    this.mRandom = new Random();
}

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

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

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

@Override
public View getView(final int position, View convertView,
                    final ViewGroup parent) {
    this.convertView = convertView;
    ViewHolder vh;
    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.notes_single_row, parent, false);
        vh = new ViewHolder();
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }
    vh.txtView = detail(convertView, R.id.notes_grid_text, notes.get(position).getNotes());
    vh.notes_title = detail(convertView, R.id.note_title_added, notes.get(position).getNotesTitle());
    int len = vh.txtView.length();
    if (len == 1 || len ==2){
        vh.txtView.setTextSize(100);
    }
    else if (len == 3){
        vh.txtView.setTextSize(80);
    }
    else if (len == 4){
        vh.txtView.setTextSize(60);
    }
    else if (len ==5){
        vh.txtView.setTextSize(50);
    }
    else if (len == 8){
        vh.txtView.setTextSize(60);
    }

    double positionHeight = getPositionRatio(position);

    vh.txtView.setHeightRatio(positionHeight);
    vh.notes_title.setHeightRatio(positionHeight);
    vh.notes_title.setPaintFlags(vh.notes_title.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);

   /* if ((position == 0 || position == 5 || position == 10 || position ==15)) {
        convertView.setBackgroundColor(Color.rgb(255, 112, 67));
    } else if (position == 1 || position == 6 || position == 11 || position ==16) {
        convertView.setBackgroundColor(Color.rgb(29, 233, 182));
    } else if (position == 2 || position == 7 || position == 12 || position ==17) {
        convertView.setBackgroundColor(Color.rgb(121, 134, 203));
    } else if (position == 3 || position == 8 || position == 13 || position ==18) {
        convertView.setBackgroundColor(Color.rgb(205, 220, 57));
    } else if (position == 4 || position == 9 || position == 14 || position ==19) {
        convertView.setBackgroundColor(Color.rgb(224, 64, 251));
    }*/
            return convertView;
}

public void changeColorToOrange() {
    convertView.setBackgroundColor(Color.rgb(255, 112, 67));
}


static class ViewHolder {
    DynamicHeightTextView txtView, notes_title;
}

private double getPositionRatio(final int position) {
    double ratio = sPositionHeightRatios.get(position, 0.0);

    if (ratio == 0) {
        ratio = getRandomHeightRatio();
        sPositionHeightRatios.append(position, ratio);
        Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
    }
    return ratio;
}

private double getRandomHeightRatio() {
    return (mRandom.nextDouble() / 2.4) + 0.8;
}
private DynamicHeightTextView detail(View v, int resId, String text) {
    DynamicHeightTextView tv = (DynamicHeightTextView) v.findViewById(resId);
    tv.setText(text);
    return tv;
}
public void toggleSelection(int position) {
    selectView(position, !mSelectedItemsIds.get(position));
}
public void selectView(int position, boolean value) {
    if (value)
        mSelectedItemsIds.put(position, value);
    else
        mSelectedItemsIds.delete(position);

    notifyDataSetChanged();
}

用户使用不同颜色的按钮添加此片段中的文本:

public class add_note_frag extends Fragment implements View.OnClickListener {
EditText note, noteTitle;
String user_note, user_note_title;
Button svenote;
ImageView ora, vio, yel, pin;
RelativeLayout rel;
ActionBar ab;
notesAdapter adapter;
private ArrayList<notesSingleRow> notes = new ArrayList<notesSingleRow>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.add_notes_fragment, container, false);
    return view;
}

@Override
public void onResume() {
    super.onResume();

    ab = getActivity().getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    rel = (RelativeLayout) getActivity().findViewById(R.id.notes_rel_lay);
    note = (EditText) getActivity().findViewById(R.id.note);
    noteTitle = (EditText) getActivity().findViewById(R.id.note_title);
    svenote = (Button) getActivity().findViewById(R.id.savenote);
    adapter = new notesAdapter(getActivity(), notes);
    ora = (ImageView) getActivity().findViewById(R.id.orange);
    vio = (ImageView) getActivity().findViewById(R.id.violet);
    yel = (ImageView) getActivity().findViewById(R.id.yellow);
    pin = (ImageView) getActivity().findViewById(R.id.pink);
    ora.setOnClickListener(this);
    vio.setOnClickListener(this);
    yel.setOnClickListener(this);
    pin.setOnClickListener(this);
    svenote.setOnClickListener(this);
}

public void saveNote() {
    tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
    user_note = note.getText().toString();
    user_note_title = noteTitle.getText().toString();
    long id1 = tasksDatabaseOperations.insertNote(user_note, user_note_title);
    if (id1 < 0) {
        Log.e("HirakDebug", "add_task_frag failed insertData operation");
    } else {
        Log.d("HirakDebug", "Data sent to be inserted");
    }
}

@Override
public void onClick(View v) {
    if (v == svenote) {
        saveNote();
        goBackToNoteFrag();
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar)));
    }
    if (v == ora) {
        rel.setBackgroundColor(getResources().getColor(R.color.orange));
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.orange)));
        adapter.changeColorToOrange();
    }
    if (v == vio) {
        rel.setBackgroundColor(getResources().getColor(R.color.violet));
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.violet)));
    }
    if (v == yel) {
        rel.setBackgroundColor(getResources().getColor(R.color.yellow));
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.yellow)));
    }
    if (v == pin) {
        rel.setBackgroundColor(getResources().getColor(R.color.pinkk));
        ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.pinkk)));
    }
}

public void goBackToNoteFrag() {
    notesListFrag nLF = new notesListFrag();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.slide_up, R.anim.slide_down);
    ft.remove(this);
    ft.replace(R.id.dynamic_content, nLF, "nLF");
    ft.commit();
}

@Override
public void onDetach() {
    super.onDetach();
    ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar)));
}

2 个答案:

答案 0 :(得分:2)

有可能。

  • 点击按钮时将选定的颜色传递给片段B.
  • 将该颜色作为参数传递给片段B中的适配器构造函数。
  • 现在您知道适配器内部的颜色。
  • 在适配器的getView()功能中,根据颜色更改膨胀视图的背景颜色。使用:view.setBackgroundColor(color);

请参阅此处的示例:https://github.com/vishalvijay/SampleColorListView

答案 1 :(得分:1)

据我所知,您所要做的就是将颜色十六进制代码与文本一起保存在数据库中。它更简单,可扩展。当您从数据库中获取文本时,您还应该在自定义对象的相同ArrayList中获取颜色,然后在适配器的getView方法中,只需将相应的Hex颜色代码应用于convertView。