2014-11-01 111 views
0

让我简单解释一下。更改列表视图行的颜色

我有2个片段:

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

2)片段B,其具有从该数据库使用customadapter当用户在片段A.点击“保存”按钮

一切正常填充数据列表视图。数据正在保存,加载到Listview和全部。现在记得5种不同颜色的按钮吗?

我理想是,虽然在片段A添加数据假设,用户选择与颜色按钮“橙色”假设,那么将在getView被充气的行()适配器的方法也应该有它的背景橙色。 (Something ike 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))); 
} 
+0

是的,请张贴您的适配器。我们必须“玩”按钮的onClick和getView – 2014-11-01 12:16:10

+0

我更新了问题。 nignore适配器类中的注释行。我用它们根据该行的位置改变背景颜色。但现在我想根据用户选择 – 2014-11-01 12:21:10

回答

2

这是可能的。

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

在这里看到的样品:https://github.com/vishalvijay/SampleColorListView

+0

片段b只是有gridview更改背景颜色。不能直接从片段A传递它? – 2014-11-01 12:24:35

+0

我可能是绞线,但我知道的是,数据是这样的方式片段A --->适配器 - 连续膨胀 - >片段B.一旦行膨胀,这是如何工作 – 2014-11-01 12:26:17

+0

它应该是:片段A ---(带有选定的颜色和其他所需的信息)--->片段B(从数据库获取数据)--->将数据填充到网格视图的适配器 – 2014-11-01 12:28:29

1

至于我认为,所有你需要做的是保存颜色的十六进制代码还你的数据库非常久远的文本。它更简单和可扩展。当您从数据库中获取文本时,还应该获取自定义对象的相同ArrayList中的颜色,然后在适配器的getView方法中,将相应的十六进制颜色代码应用于convertView。

+0

它看起来很简单。让我检查一下这是否有效。 – 2014-11-01 12:38:29

+0

肯定继续.. :) – 2014-11-01 12:38:56

+0

这工作。非常感谢你。 – 2014-11-01 13:51:35

相关问题