Portál AbcLinuxu, 25. dubna 2024 04:15
public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder = new ViewHolder(); if (convertView == null) { //need to be fixed if (data[position][4] != "") { vi = inflater.inflate(R.layout.processed_rows, null); holder.tvCode = (TextView) vi.findViewById(R.id.prCode); holder.tvDesc = (TextView) vi.findViewById(R.id.prDesc); holder.tvCurrloc = (TextView) vi.findViewById(R.id.prCurrLoc); holder.tvDestination = (TextView) vi.findViewById(R.id.prTargetLoc); } else { vi = inflater.inflate(R.layout.rows, null); holder.tvCode = (TextView) vi.findViewById(R.id.rCode); holder.tvDesc = (TextView) vi.findViewById(R.id.rDesc); holder.tvCurrloc = (TextView) vi.findViewById(R.id.rCurrLoc); holder.tvDestination = (TextView) vi.findViewById(R.id.rTargetLoc); } .......Toto ovšem funguje pouze, pokud position souhlasí s indexem v poli dat. Jakmile začnu scrolovat, vzniká zmatek v layoutech řádků (jsou přiřazeny špatně). Mohl by někdo poradit, jak zajistit, aby se layouty přiřazovaly správně i během scrollování?
getView
přepínat jejich viditelnost. Ale v tomto případě mi přijde, že samotný přístup, že máte dva různé layouty pro velmi podobná data, je chybně navržený.
are data changed due scrolling?
try:
public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder = (vi == null) ? null : (ViewHolder) vi.getTag(); if (holder == null) { //todo move to viewholder constructor vi = inflater.inflate(((data[position][4] != "") ? R.layout.processed_rows : R.layout.rows ), null); // holder constructor can have parameters, // example: new ViewHolder(inflater, vi, data[position]) holder = new ViewHolder(); //todo move to viewholder constructor vi.setTag(holder); // todo this can be in holder too holder.tvCode = (TextView) vi.findViewById(R.id.prCode); holder.tvDesc = (TextView) vi.findViewById(R.id.prDesc); holder.tvCurrloc = (TextView) vi.findViewById(R.id.prCurrLoc); holder.tvDestination = (TextView) vi.findViewById(R.id.prTargetLoc); } // todo: holder function to set data holder.setData(data[position]); return vi; }
Tiskni Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.