2015-06-03 2 views
0

Я заполняю список с помощью настраиваемого адаптера, но теперь я хочу изменить макет строки, если идентификатор содержит в массиве.Как я могу изменить компоновку в адаптере?

Это мой код:

public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     CalcHolder holder = new CalcHolder(); 
     PojoCalc f = calcList.get(position); 

     Integer[] calcs_lite = {6, 8, 18, 37, 51, 75}; 

     boolean exists = containsValue(calcs_lite, f.get_id()); 

     // First let's verify the convertView is not null 
     // Value exist, so, we enable layout 
     if (convertView == null) { 
      // This a new view we inflate the new layout 
      if(exists){ 
       Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::AVAILABLE"); 
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = inflater.inflate(R.layout.calc_row_adapter, null); 
      }else{ 
       Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::UNAVAILABLE"); 
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = inflater.inflate(R.layout.calc_row_adapter_disabled, null); 
      } 

      // Now we can fill the layout with the right values 
      TextView tv_name = (TextView) v.findViewById(R.id.name); 
      TextView tv_subtitle = (TextView) v.findViewById(R.id.subtitle); 


      holder.calcNameView = tv_name; 
      holder.calcSubtitleView = tv_subtitle; 

      v.setTag(holder); 
     } 
     else{ 
      holder = (CalcHolder) v.getTag(); 
     } 

     holder.calcNameView.setText(f.getName()); 
     holder.calcSubtitleView.setText(f.getSubtitle()); 


     return v; 
    } 

Первый раз, когда я получаю список, он работает, но когда я тяну вниз, чтобы получить больше элементов, он выходит из строя, это не меняет вид справа. Я думаю, что в «другом» есть проблема с держателем, но я не уверен.

+1

Проблема в том, что 'если (convertView == NULL)' условие будет ложным при прокрутке, как вид уже существует и что угодно вы Вы уже завыли в первый раз, когда создали свое представление. RecyclerView обрабатывает эту ситуацию намного лучше, чем ListView, но если вам нужно использовать listview, посмотрите на 'getItemViewType' (http://developer.android.com/reference/android/widget/Adapter.html) и посмотрите, как вы можете используй это. Это хороший пример, который объясняет концепции лучше https://guides.codepath.com/android/Implementing-a-Heterogenous-ListView – kha

ответ

1

1.override getItemViewType и getViewTypeCount в вас пользовательский адаптер
2.Use getItemViewType в GetView

1

Это происходит потому, что вид списка повторного использования каждой строки, и вы пытаетесь установить другую раскладку для каждой строки может вы пожалуйста, как расположение в один файл и переключить его т.е. (скрыть/видимый), как вы хотите

if (convertView == null) { 
     LayoutInflater vi = (LayoutInflater) _context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = vi.inflate(R.layout.grid_list_item, null); 

     holder = new ViewHolder(); 

     holder.btn_delete_photo = (Button) convertView 
       .findViewById(R.id.btn_delete_photo); 
     holder.im_photo = (ImageView) convertView 
       .findViewById(R.id.im_photo); 
     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    DataHolderClassName item = itemList.get(position); 

    holder.btn_delete_photo.setTag(item); 
    holder.im_photo.setTag(item); 

    if(exist) 
    { 
    hide(); 
    }else{ 
    visible();} 
1

вы все в порядке ... просто передать ViewGroup методу надуйте() и добавить третий параметр как ложь точно так же, как

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    CalcHolder holder = new CalcHolder(); 
    PojoCalc f = calcList.get(position); 

    Integer[] calcs_lite = {6, 8, 18, 37, 51, 75}; 

    boolean exists = containsValue(calcs_lite, f.get_id()); 

    // First let's verify the convertView is not null 
    // Value exist, so, we enable layout 
    if (convertView == null) { 
     // This a new view we inflate the new layout 
     if(exists){ 
      Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::AVAILABLE"); 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.calc_row_adapter, parent, false); //changes in this line 
     }else{ 
      Log.e("AdapterCalc1", "VALUE:::" + exists + ":::ID:::" + f.get_id() + ":::NAME:::" + f.getName() + ":::UNAVAILABLE"); 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.calc_row_adapter_disabled, parent, false); //changes in this line 
     } 

     // Now we can fill the layout with the right values 
     TextView tv_name = (TextView) v.findViewById(R.id.name); 
     TextView tv_subtitle = (TextView) v.findViewById(R.id.subtitle); 


     holder.calcNameView = tv_name; 
     holder.calcSubtitleView = tv_subtitle; 

     v.setTag(holder); 
    } 
    else{ 
     holder = (CalcHolder) v.getTag(); 
    } 

    holder.calcNameView.setText(f.getName()); 
    holder.calcSubtitleView.setText(f.getSubtitle()); 


    return v; 
} 
0

Ответ - это сочетание ваших ответов. Я меняю линию надувателя, и я динамически меняю макет, внося изменения в цвета.

Конечный код это:

public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     LinearLayout ll_row; 
     TextView tv_name, tv_subtitle; 

     CalcHolder holder = new CalcHolder(); 
     PojoCalc f = calcList.get(position); 

     Integer[] calcs_lite = {6, 8, 18, 37, 51, 75}; 

     boolean exists = containsValue(calcs_lite, f.get_id()); 

     // First let's verify the convertView is not null 
     // Value exist, so, we enable layout 
     if (convertView == null) { 
      // This a new view we inflate the new layout 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.calc_row_adapter, parent, false); //changes in this line 

      // Now we can fill the layout with the right values 
      ll_row = (LinearLayout) v.findViewById(R.id.ll_row); 
      tv_name = (TextView) v.findViewById(R.id.name); 
      tv_subtitle = (TextView) v.findViewById(R.id.subtitle); 


      if(exists){ 
       ll_row.setBackgroundColor(Color.WHITE); 
       tv_name.setTextColor(Color.BLACK); 
       tv_subtitle.setTextColor(Color.BLACK); 
      }else{ 
       ll_row.setBackgroundColor(Color.LTGRAY); 
       tv_name.setTextColor(Color.GRAY); 
       tv_subtitle.setTextColor(Color.GRAY); 
      } 

      holder.calcNameView = tv_name; 
      holder.calcSubtitleView = tv_subtitle; 

      v.setTag(holder); 
     } 
     else{ 
      // Now we can fill the layout with the right values 
      ll_row = (LinearLayout) v.findViewById(R.id.ll_row); 
      tv_name = (TextView) v.findViewById(R.id.name); 
      tv_subtitle = (TextView) v.findViewById(R.id.subtitle); 

      if(exists){ 
       ll_row.setBackgroundColor(Color.WHITE); 
       tv_name.setTextColor(Color.BLACK); 
       tv_subtitle.setTextColor(Color.BLACK); 
      }else{ 
       ll_row.setBackgroundColor(Color.LTGRAY); 
       tv_name.setTextColor(Color.GRAY); 
       tv_subtitle.setTextColor(Color.GRAY); 
      } 


      holder = (CalcHolder) v.getTag(); 
     } 

     holder.calcNameView.setText(f.getName()); 
     holder.calcSubtitleView.setText(f.getSubtitle()); 


     return v; 
    } 

Благодаря

Смежные вопросы