【问题标题】:expanding listview custom component with button click使用按钮单击扩展列表视图自定义组件
【发布时间】:2010-11-05 20:03:14
【问题描述】:

我正在开发一个小型应用程序,其中包含一个填充了复合组件的列表视图。该组件内部有两个文本视图和一个按钮。其中一个文本视图是不可见的,当单击该按钮时,它应该会出现。我可以显示列表,但单击按钮时无法使文本视图可见。 这是组件的xml:

<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
   android:layout_height="wrap_content">
 <TextView 
  android:text="@+id/TextView01" 
  android:id="@+id/placeNamwView" 
  android:layout_height="wrap_content" 
  android:textSize="30dp" 
  android:layout_width="wrap_content" 
  android:maxWidth="250dp">
  </TextView>
 <Button 
  android:layout_height="wrap_content" 
  android:layout_toRightOf="@+id/placeNamwView" 
  android:layout_width="wrap_content" 
  android:id="@+id/Button01" 
  android:text="More">
  </Button>
 <EditText 
  android:layout_below="@+id/placeNamwView" 
  android:layout_height="wrap_content" 
  android:layout_width="wrap_content" 
  android:text="@+id/TextView01" 
  android:id="@+id/placeAddressView" 
  android:textSize="20dp" 
  android:textColor="#FFFF0000" 
  android:maxWidth="250dp">
  </EditText>
</RelativeLayout>

这是填充列表的 ArrayAdapter:

   public class AddressAdapter extends ArrayAdapter<Item> {

   int resource;
   RelativeLayout placeView;
   EditText addressText;



   public AddressAdapter(Context _context, int _resource, List<Item> _items) {
       super(_context, _resource, _items);
       resource = _resource;
   }



   private OnClickListener buttonClick = new OnClickListener() {
     public void onClick (View v) {
       int i = placeView.findViewById(R.id.stub_import).getVisibility();
       visibility(i);
     }
   }


   private void visibility(int i) {
    //

   TODO Auto-generated method stub
     switch(i) {
       case(View.GONE): 
         addressText.setVisibility(View.VISIBLE);
         break;
       case(View.VISIBLE): 
         addressText.setVisibility(View.GONE);
         break;
   }



   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
     Item item = getItem(position);

     String name = item.getName();
     String address = item.getAddress();

     if (convertView == null) {
       placeView = new RelativeLayout(getContext());
       String inflater = Context.LAYOUT_INFLATER_SERVICE;
       LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
       vi.inflate(resource, placeView, true);
     } 
     else 
     {
       placeView = (RelativeLayout) convertView;
     }

     TextView nameText = (TextView)placeView.findViewById(R.id.placeNamwView);
     Button button = (Button)placeView.findViewById(R.id.Button01);
     addressText = (EditText)placeView.findViewById(R.id.placeAddressText);

     button.setOnClickListener(buttonClick);
     nameText.setText(name);
     addressText.setText(address);

     return placeView;
  }
}

【问题讨论】:

    标签: android listview custom-component


    【解决方案1】:

    您对每一行都使用相同的placeView 数据成员。因此,您的按钮单击将更改 getView() 处理的最后一行中的 TextView 可见性,而不是包含用户单击的按钮的行。

    Here is a sample project 来自我的一本书,展示了在 ListView 中使用交互式小部件(在本例中为 RatingBar)。就我而言,我使用了getTag()setTag()。在您的情况下,您只需在 Button 上调用 getParent() 即可开始四处导航以找到正确的 TextView

    【讨论】:

      【解决方案2】:

      我认为你应该在条件中使用以下内容

       INVISIBLE 
      

      而不是

        GONE 
      

      试试吧!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多