Android listview随机行为

时间:2013-01-24 08:09:32

标签: android android-layout android-widget


我正面临listview i-e的问题我在运行时对ListViewItem上的一些视图进行了膨胀,导致ListView行增加,i-e listview中的某些行具有不同的高度,而其他行具有不同的getView位置。

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView1);
        TestAdapter adapter = new TestAdapter(this);
        listView.setAdapter(adapter);
    }
    public void onClickAction(View v){
         v.findViewById(R.id.l1).setVisibility(View.VISIBLE);
    }
}


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TestAdapter extends BaseAdapter {
    private String[] items = {"a","b","c","d","e","f","g","h","i"};
    private Context context;
    public TestAdapter(Context context) {
        this.context = context;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    ViewHolder holder;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText("value = "+position);
        return convertView;
    }
    static class ViewHolder{
        TextView textView;
    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >


<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView>

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLayout"
    android:onClick="onClickAction"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"        
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:visibility="gone"
        android:background="@android:color/darker_gray"
        android:orientation="vertical" >
    </LinearLayout>
</RelativeLayout>

0 个答案:

没有答案