2014-11-01 222 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

相關問題