0


我面临 listview 的问题,即我在运行时对 ListViewItem 的某些视图进行膨胀,这导致 ListView 行的增加,即 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>
4

0 回答 0