【问题标题】:change color of listview row更改列表视图行的颜色
【发布时间】:2014-12-28 15:07:11
【问题描述】:

让我简单解释一下。

我有 2 个片段:

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

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

一切正常。数据正在保存,加载到 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)));
}

【问题讨论】:

  • 是的,请发布您的适配器。我们必须“玩”按钮的 onClick 和 getView
  • 我更新了问题。忽略适配器类中的那些注释行。我用它们根据该行的位置更改背景颜色。但现在我想根据用户的选择来改变背景颜色

标签: android listview


【解决方案1】:

有可能。

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

在此处查看示例:https://github.com/vishalvijay/SampleColorListView

【讨论】:

  • 片段 b 只有网格视图。我不能直接从片段 A 传递它吗?
  • 我可能会扭扭捏捏,但我的理解是数据是这样的片段 A ---> 适配器 -- 连续膨胀 --> 片段 B。一旦行膨胀,这是怎么回事上班
  • 它应该是:片段 A ---(需要选择颜色和其他信息)---> 片段 B(从 db 获取数据)---> 将数据填充到网格视图的适配器
  • 哦..谢谢你..我会试试你的方法
  • 你能帮我解决你的问题吗?我的意思是,在我的情况下,关于如何准确实现它的几行代码将不胜感激
【解决方案2】:

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

【讨论】:

  • 很高兴为您提供帮助.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多