1

我有一个 ListView 只显示一项而不是两项。那是我的主要活动:

public class MainActivity extends Activity {
ImageView Add;
ImageView Shadow;
ProgressBar PB;

ListView listView;
String[] valuesUp = { "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "Sed non justo eros. Praesent a nisl dui" };
String[] valuesDown = { "Quisque ut ante eu arcu sagittis porta eu a dolor...",
        "In orci augue facilisis eget fermentum consequat ac..." };
int[] imgValues = {R.drawable.mobileapp_16, R.drawable.mobileapp_19};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ArrayAdapter<String> adapter = new MyAdapter(this, valuesUp, valuesDown, imgValues);
    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(adapter);
}

那就是我的适配器:

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] valuesUp;
private final String[] valuesDown;
private final int[] ImageValue;
private TextView textViewUp;
private TextView textViewDown;
private ImageView image;

public MyAdapter(Context context, String[] valuesUP, String[] valuesDown, int[] imageValue) {
    super(context, R.layout.rowlayout, valuesUP);
    this.context = context;
    this.valuesUp = valuesUP;
    this.valuesDown = valuesDown;
    this.ImageValue = imageValue;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView;

    rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    textViewUp = (TextView) rowView.findViewById(R.id.row_text_view_up);
    textViewDown = (TextView) rowView.findViewById(R.id.row_text_view_down);
    image = (ImageView) rowView.findViewById(R.id.image);
    textViewUp.setText(valuesUp[position]);
    textViewDown.setText(valuesDown[position]);
    image.setImageDrawable(context.getResources().getDrawable(ImageValue[position]));

    return rowView;
}}

当我调试时,我看到 getView 被调用了很多次(大约 12 次)并且“位置”始终为 0。为什么会这样?谢谢!

我现在发现了我的问题!有点奇怪。列表的高度表现得很奇怪。当我在做“wrap_content 或 match_parent”时,它给了我一个项目,但是当我放置 400dp 时它可以。为什么会这样?!那是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/topbar_bg" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/topbar_leftcircle" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/imageView2"
    android:src="@drawable/topbar_logo" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="5dp"
    android:scaleType="fitStart"
    android:src="@drawable/topbar_menu" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentTop="true"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/imageView4"
    android:src="@drawable/topbar_key" />

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView1"
            android:layout_centerHorizontal="true"
            android:background="#00000000"
            android:src="@drawable/mobileapp_14" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageButton1"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY"
            android:src="@drawable/transparent_bg" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/imageView6"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:maxLines="2"
            android:text="Sed at vestibulum elit. Nunc quam erat, gravida ac consequat"
            android:textColor="@color/RedText"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageButton1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textView1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:maxLines="2"
            android:text="Nullam sed erat erat, ut faucibus nunc. Etiam et mi di ante lacinia suscipit. Mauris gravida turpis eget lectus ullam..."
            android:textColor="@color/WhiteText"
            android:textSize="12dp" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerHorizontal="true"
            android:background="#00000000"
            android:scaleType="centerCrop"
            android:src="@drawable/mobileapp_15" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            android:layout_below="@id/imageView7" >

        </ListView>

    </RelativeLayout>
</ScrollView>
4

6 回答 6

2

我发现你的问题。

您的问题出在 XML 中。

从 xml 中删除ScrollView 。问题会解决的。

于 2012-11-21T13:39:48.750 回答
2

@roiberg 您的代码在我的模拟器中运行良好。检查您的 XML 代码

于 2012-11-21T13:22:46.180 回答
1

您不应该将 listView 的高度保持为 wrap_content ,因为这非常昂贵。将其设为 fill_parent/match_parent 并尝试一下。

给出 wrap_content 会让系统做很多运算来计算 listView 的高度。这将对位置 0 处的项目进行多次调用。

您不应该在滚动视图中提供 ListView。这永远行不通!

于 2012-11-21T13:13:56.120 回答
0

我找到了解决方法。我将第一张图片放在一个独立的布局中,并将此布局设置为列表中的第一个单元格。所以,现在我将图像和列表本身作为一个大的可滚动列表。如果有人看到这有什么大问题,我想听听。

于 2012-11-21T14:26:58.780 回答
0

像这样改变你的活动

公共类 MainActivity 扩展 Activity { ImageView 添加;ImageView 阴影;进度条 PB;

ListView listView;
String[] valuesUp = { "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
"Sed non justo eros. Praesent a nisl dui" };
String[] valuesDown = { "Quisque ut ante eu arcu sagittis porta eu a dolor...",
"In orci augue facilisis eget fermentum consequat ac..." };
int[] imgValues = {R.drawable.mobileapp_16, R.drawable.mobileapp_19};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(new MyAdapter(this,valuesUp,valuesDown,imgValues));
}

像这样更改适配器

public class MyAdapter extends BaseAdapter {
private final Context context;
private final String[] valuesUp;
private final String[] valuesDown;
private final int[] ImageValue;
private TextView textViewUp;
private TextView textViewDown;
private ImageView image;

public MyAdapter(Context context, String[] valuesUP, String[] valuesDown, int[] imageValue) {
    super(context, R.layout.rowlayout, valuesUP);
    this.context = context;
    this.valuesUp = valuesUP;
    this.valuesDown = valuesDown;
    this.ImageValue = imageValue;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView;

    rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    textViewUp = (TextView) rowView.findViewById(R.id.row_text_view_up);
    textViewDown = (TextView) rowView.findViewById(R.id.row_text_view_down);
    image = (ImageView) rowView.findViewById(R.id.image);
    textViewUp.setText(valuesUp[position]);
    textViewDown.setText(valuesDown[position]);
    image.setImageDrawable(context.getResources().getDrawable(ImageValue[position]));

    return rowView;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return valuesUp.length;
}

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

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}}
于 2012-11-21T12:59:13.760 回答
0

就我而言,问题是因为我使用的是NestedScrollView所以解决方案是将 android:fillViewport="true"添加到我的 NestedScrollView

这是我的代码:

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.widget.NestedScrollView>
于 2015-09-29T20:49:11.783 回答