【问题标题】:Android - ExpandableListView feature not working..Android - ExpandableListView 功能不起作用..
【发布时间】:2014-05-19 11:05:45
【问题描述】:

我想要做的是在每个列表视图项中有一个带有按钮的列表视图项。 当按下按钮时,我希望一个处于消失状态的布局将可见并随着动画向下展开,如果显示布局而不是当同一个按钮被预置时,布局将向上滑动并再次消失。

为了做到这一点,我为列表视图项创建了下一个布局 -

    <?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 之外使用简单的 textview 破解了动画代码并且它可以工作,但是当我尝试在 listview 项目中使用它时,它不起作用。

有什么想法吗?

感谢您的帮助

【问题讨论】:

标签: android android-layout listview android-listview expandablelistview


【解决方案1】:

使用 ExpandableListView。您必须将 BaseExpandableListAdapter 扩展到您的适配器并覆盖方法。在此方法中,重写 getGroupView 以显示可扩展组的名称,并重写 getChildView 方法以膨胀列表的子视图。在为子视图充气后,在该视图上设置您想要的任何动画。

【讨论】:

    【解决方案2】:

    你应该使用可扩展视图...
    基本示例在这里...expandavlelistview example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2012-12-15
      • 1970-01-01
      相关资源
      最近更新 更多