0

更新 正如建议的那样,我已将数据视图持有者添加到我的代码中,但我仍然面临同样的问题。我已经编辑了我的问题,我的代码请看一下。

我想根据行号在我的列表视图中显示 2 个视图。我想在第一行显示右手火车引擎,在第二行显示左手火车引擎,反之亦然。

反之亦然行视图更改我放置 if 条件并通过 mod%2==0 检查位置,而不是奇怪的.. 但是当我启动应用程序时,我得到准确的左右视图但是当我向下滚动时,我得到了两个右手火车引擎一排排。

这是代码

public LazyAdapter(Activity a, ArrayList<String> songsList) {
    activity = a;
    data=songsList;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
    Log.d("Listview","Count is"+data.size());
    return data.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    Log.d("position is - ", "position is "+position);
    if(position%2==0)
    {
    return 0;
    }
    else
    {
    return 1;
    }

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    int type = getItemViewType(position);
    System.out.println("getView " + position + " " + convertView + " type = " + type);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
            case 0:
                convertView = inflater.inflate(R.layout.list_row1, null);
                holder.caption = holder.caption = ((ImageView)convertView.findViewById(R.id.train).findViewById(R.id.boogi1).findViewById(R.id.imageView1));
                break;
            case 1:
                convertView = inflater.inflate(R.layout.list_row2, null);
                holder.caption = holder.caption = ((ImageView)convertView.findViewById(R.id.train2).findViewById(R.id.boogi2).findViewById(R.id.imageView1)); 
                break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    return convertView;
}

static class ViewHolder {
    ImageView caption;

    }

List_row1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
 <include android:id="@+id/mainview" layout="@layout/activity_main" />

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <include android:id="@+id/train" layout="@layout/train" />
</HorizontalScrollView>

火车.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
    <include android:id="@+id/boogi1" layout="@layout/boogi" />
    <include android:id="@+id/boogi2" layout="@layout/boogi" />
     <include android:id="@+id/engine" layout="@layout/engine" />

在 Train2.xml 中包含标记顺序已更改。

4

3 回答 3

1

你没有View Holder用来保持视图,因为当我们滚动列表持有者应该在那里保持inflated视图......

膨胀你的视图后初始化视图持有者,然后在里面做你的东西。

于 2013-04-15T13:46:59.440 回答
0

正如 mahaveermuttha 和 paresh mayani 建议使用 viewholder 和 adilsoomra 指出我忘记添加 getViewTypeCount(); 它解决了我的问题

于 2013-04-16T08:22:48.633 回答
0

if(convertView==null)可能会在这里给您带来问题。

目前,只有在 convertView = null 时才会检查您检查要使用哪个视图的代码。在您的情况下, convertView 可能不为空,因此它多次重复使用同一视图,直到 convertView 再次为空(可能是因为滚动)。

试着留下那部分,然后再试一次。

于 2013-04-15T13:46:20.140 回答