2016-08-21 45 views
1

如標題所示,我有一個LinearLayout,我將其作爲ListView的一部分使用。當我點擊物品時,我需要將其LinearLayout的可見性從View.GONE更改爲View.VISIBLE(反之亦然)。重複使用時保持LinearLayout不見視圖用於ListView

我的問題是,當我點擊第一項(所以現在它的佈局是可見的),而不是滾動列表下來,一個可重用的第一個項目的項目似乎有其佈局的可見性設置爲View.VISIBLE也完全一樣第一個項目,雖然它應該是View.GONE。其餘顯示的項目不重用第一個按預期工作。

我清楚地明白爲什麼會發生的事情,我想我需要爲每一個項目存儲在LinearLayout的可視性我ListView(不僅是當前顯示的),然後使用它在我getView()方法。你們有什麼想法我該怎麼做?

從我的活動有關的某些代碼:

public class MainActivity extends AppCompatActivity { 
     ListView alarmClocksListView; 
     ArrayList<AlarmClock> alarmClocksList; 

     protected void onCreate(Bundle savedInstanceState) { 
      alarmClocksList = new ArrayList<>(); 
      alarmsArrayAdapter = new AlarmsArrayAdapter(this, alarmClocksList); 
      alarmClocksListView.setAdapter(alarmsArrayAdapter); 
     } 

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      //some code 
      alarmClocksList.add(new AlarmClock()); 
      alarmsArrayAdapter.notifyDataSetChanged(); 
     } 
} 
+0

你能張貼您的適配器代碼和活動代碼呢? –

回答

0

好的球員,我找到了解決辦法。我基本上做的是向存儲數據的類添加一個布爾值,這些數據顯示在我的ListView中(因爲每個項目都有自己的數據對象,因此佈局的可見性似乎是個好主意)。在我的getView()方法中,我只是檢索該值並將其用作佈局的可見性。當點擊一個View我相應地處理它,改變適當對象內部的可見性。我還使用一個按鈕來將View.VISIBLE的可見性更改爲View.GONE(對於那些對此按鈕感興趣的用戶)。

這裏是我的適配器:

public class AlarmsArrayAdapter extends ArrayAdapter { 

    ArrayList<AlarmClock> alarmClockArrayList; 
    private Context context; 

    static class ViewHolder { 
     TextView whenAlarmRings; 
     RelativeLayout additionalPanel; 
     Button doneButton; 
    } 

    public AlarmsArrayAdapter(Context context, ArrayList<AlarmClock> alarmClocks) { 
     super(context, -1, alarmClocks); 
     alarmClockArrayList = alarmClocks; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     AlarmClock alarmClock = alarmClockArrayList.get(position); 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.alarm_clock_item, parent, false); 

      holder = new ViewHolder(); 
      holder.whenAlarmRings = (TextView) convertView.findViewById(R.id.whenAlarmRings); 
      holder.additionalPanel = (RelativeLayout) convertView.findViewById(R.id.additionalPanel); 
      holder.doneButton = (Button) convertView.findViewById(R.id.doneButton); 

      convertView.setTag(holder); 
     } else 
      holder = (ViewHolder) convertView.getTag(); 


     if (alarmClock.isViewExpanded == true) { 
      holder.additionalPanel.setVisibility(View.VISIBLE); 
      holder.whenAlarmRings.setVisibility(View.GONE); 
     } else { 
      holder.additionalPanel.setVisibility(View.GONE); 
      holder.whenAlarmRings.setVisibility(View.VISIBLE); 
     } 

     setListeners(holder, alarmClock, convertView); 
     holder.whenAlarmRings.setText(alarmClock.whenAlarmRings); 

     for (int i = 0; i < AlarmClock.DAYS_IN_WEEK; i++) 
      holder.days[i].setChecked(alarmClock.weekDaysState[i]); 
     return convertView; 
    } 

    private void setListeners(final ViewHolder holder, final AlarmClock alarmClock, final View convertView) { 
     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (holder.whenAlarmRings.getVisibility() == View.VISIBLE) { 
        holder.whenAlarmRings.setVisibility(View.GONE); 
        holder.additionalPanel.setVisibility(View.VISIBLE); 
        alarmClock.isViewExpanded = true; 
        //convertView.setClickable(false); 
       } 
      } 
     }); 

     holder.doneButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (holder.whenAlarmRings.getVisibility() == View.GONE) { 
        holder.whenAlarmRings.setVisibility(View.VISIBLE); 
        holder.additionalPanel.setVisibility(View.GONE); 
        alarmClock.isViewExpanded = false; 
        //convertView.setClickable(true); 
       } 
      } 
     }); 
    } 

這裏是我的類存儲數據:

public class AlarmClock { 
    public static final boolean LIST_ITEM_IS_EXPANDED_BY_DEFAULT = true; 
    public String whenAlarmRings; 
    public boolean isViewExpanded; 

    public AlarmClock() { 
     this.isViewExpanded = LIST_ITEM_IS_EXPANDED_BY_DEFAULT; 
     this.whenAlarmRings = "SomeDay"; 
    } 
}