1

Что я пытаюсь сделать, так это иметь элемент списка с кнопкой в ​​каждом списке viewitem. Когда нажата кнопка Buttton, я хочу, чтобы макет, находящийся в состоянии ожидания, будет виден и расширяться с анимацией вниз, и если макет отображается, чем когда будет сохранена одна и та же кнопка, то макет будет скользить вверх и снова исчезнет.Android - функция ExpandableListView не работает.

Для того, чтобы сделать это, я создал следующий макет для пункта - списке следует

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tilte_info"> 

     <TextView 
      android:id="@+id/textView_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="15dp" 
      android:text="TextView" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button_open" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="25dp" 
      android:text="Open" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#5C83AF" 
     android:visibility="gone" 
     android:id="@+id/extend_info"> 

     <TextView 
      android:id="@+id/textView_more_info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="15dp" 
      android:text="TextView" 
      android:textSize="20sp" /> 



    </LinearLayout> 



</LinearLayout> 

Как вы можете видеть, что есть две раскладки - один является tilte_info, который всегда показанными и есть кнопка в нем , а другой - extend_info, который будет отображаться или скрываться нажатием кнопки.

Теперь я использовал следующий код для того, чтобы попытаться заставить его работать -

public class MainActivity extends Activity { 
ArrayList<Place> placesList = new ArrayList<Place>(); 
placeAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Place place1 = new Place("Place num 1", "some info on place 1"); 
    Place place2 = new Place("Place num 2", "some info on place 2"); 
    Place place3 = new Place("Place num 3", "some info on place 3"); 

    placesList.add(place1); 
    placesList.add(place2); 
    placesList.add(place3); 

    ListView lv = (ListView) findViewById(R.id.listview_1); 

    adapter = new placeAdapter(this); 

    lv.setAdapter(adapter); 
    } 

     class placeAdapter extends ArrayAdapter<Place> implements OnClickListener{ 

     public placeAdapter(Context context) { 
      super(context, -1, placesList); 

     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      if (convertView==null){ 
       // use the LayoutInflater to inflate an XML layout file: 
       convertView=getLayoutInflater().inflate(R.layout.list_item, parent,false); 
      } 

      TextView textTitle = (TextView) convertView.findViewById(R.id.textView_title); 
      TextView textInfo = (TextView) convertView.findViewById(R.id.textView_more_info); 
      Button open = (Button) convertView.findViewById(R.id.button_open); 

      infoLayout = (View) convertView.findViewById(R.id.extend_info); 
      infoLayout.setVisibility(View.GONE); 
      open.setOnClickListener(this); 

      Place place = placesList.get(position); 

      textTitle.setText(place.getTitle()); 
      textInfo.setText(place.getInfo()); 

     return convertView; 
     } 

     @Override 
     public void onClick(View v) { 

      if(infoLayout.isShown()){ 
       slide_up(MainActivity.this, infoLayout); 
       infoLayout.setVisibility(View.GONE); 

      }else{ 
       infoLayout.setVisibility(View.VISIBLE); 
       slide_down(MainActivity.this, infoLayout); 

      } 

     } 

     } 



    public static void slide_down(Context ctx, View v){ 

     Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_down); 
     if(a != null){ 
     a.reset(); 
     if(v != null){ 
      v.clearAnimation(); 
      v.startAnimation(a); 
     } 
     } 
    } 



public static void slide_up(Context ctx, View v){ 

     Animation a = AnimationUtils.loadAnimation(ctx, R.anim.slide_up); 
     if(a != null){ 
     a.reset(); 
     if(v != null){ 
      v.clearAnimation(); 
      v.startAnimation(a); 
     } 
     } 
    } 






    } 

Дело в том, что я пытался отладить код и получать в функции OnClick, но ничего не происходит - макет, который ушел, не был показан.

Я проколол код анимации простым текстовым просмотром вне списка и работал, но когда я пытаюсь использовать его в элементе listview, он не работает.

Любые идеи, почему?

Спасибо за любую помощь

+0

вы должны использовать [ExpandableListView] (http://developer.android.com/reference/android/ виджет/ExpandableListView.html) –

ответ

0

Использование ExpandableListView. Вы должны расширить BaseExpandableListAdapter на свой адаптер и переопределить методы. В этих методах переопределите getGroupView, чтобы отобразить имя expandablegroup и переопределить метод getChildView, чтобы раздуть дочерний вид для списка. После раздувания макета для ребенка установите любую анимацию, которая вам нужна в этом представлении.

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