如何在单击列表视图中展开行的高度?

时间:2011-08-03 08:56:13

标签: android listview

我希望在单击列表视图时增加行的高度(匹配父项是特定的),但其他行的高度不应受到影响,只有被点击的行应该展开。

这是我修改代码后所做的。现在我想要点击一个项目,列表应该开始引用另一个XML以不同的方式显示行

public class ExpandableListActivity extends ListActivity 
{

Context context;
int selectedPosition = 0;


String [] mTitles;
String [] mDialogues; 

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    mTitles = getResources().getStringArray(R.array.state_name);
    mDialogues = getResources().getStringArray(R.array.capital_name);
    // Use our own list adapter
    setListAdapter(new ListAdapter(this));
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{    

}


private class ListAdapter extends BaseAdapter {
    public ListAdapter(Context context)
    {

    }



    public int getCount() {
        return mTitles.length;
    }

    public Object getItem(int position) {
        return position;
    }


    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         LayoutInflater inflater = ((Activity) context).getLayoutInflater();
         if(convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.listlayout, parent, false);

                holder.text = (TextView)convertView.findViewById(R.id.textView1);
                convertView.setTag(holder);
            } 

         else 
         {
             holder = (ViewHolder) convertView.getTag(); 
         }
         holder.text.setText(mTitles[position]);

         return convertView;
    }
}
}
   class ViewHolder { 
    TextView text; 
    }

1 个答案:

答案 0 :(得分:1)

您应该使用自定义适配器并在getView(在CursorAdapter的情况下为bindView)更改 LayoutParams为“match_parent”,如果position == clickedPosition或“你的默认值”,否则

static class ViewHolder { 
    TextView text; 
}

private final LayoutParams MATCH_PARENT = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
private final LayoutParams WRAP_CONTENT = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if(convertView == null) {
        holder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.my_layout, parent, false);

        holder.text = (TextView)convertView.findViewById(R.id.text);
        convertView.setTag(holder);
    } else {
         holder = (ViewHolder) convertView.getTag(); 
    }

    holder.text.setText("SomeText"));

    if(position == selectedPosition) {  
        convertView.setLayoutParams(MATCH_PARENT);
    } else {
        convertView.setLayoutParams(WRAP_CONTENT);
    }

    return convertView;
}