为什么自定义列表视图会更改位置?

时间:2016-03-07 11:54:42

标签: android listview

我创建了自定义ListView。它有11件商品,从0到10。 列表视图仅显示0到8,然后再次从0开始。 有什么帮助吗? 这是代码:

主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.example.tefa.mycustomlist.MainActivity">

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:divider="#223344"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    />

行项目XML          

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dp"
    android:background="#ffeeee"


    android:padding="3dp">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/listImage"
        android:background="@drawable/ic_launcher" />
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="The Alchemist"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:textSize="14dp"
    android:textStyle="bold"
    android:textColor="#040404"
    android:id="@+id/title"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Paulo Coelho"
    android:textColor="#ffde3a3d"
    android:layout_toRightOf="@+id/thumbnail"
    android:layout_below="@+id/title"
    android:layout_marginTop="2dp"
    android:id="@+id/author"/>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="201 pages"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/thumbnail"
    android:id="@+id/pages"
    android:textStyle="bold"

    />

<ImageView
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/pages"
    android:layout_marginTop="2dp"
    android:background="@drawable/ic_launcher"/>

主要活动

package com.example.tefa.mycustomlist;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
ListView myList;
CustomListViewAdapter myAdapter;
ArrayList<HashMap<String, String>> authorList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final String[] bookTitles = new String[]{
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10"


    };

    final String[] bookPages = new String[]{
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10"
    };

    final String[] authors = new String[]{

            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10"

    };

    authorList = new ArrayList<>();

    for (int i = 0; i < authors.length; i++) {
        HashMap<String, String> data = new HashMap<>();
        data.put("title", bookTitles[i]);
        data.put("pages", bookPages[i]);
        data.put("author", authors[i]);
        authorList.add(data);
    }

    myList = (ListView) findViewById(R.id.list);

    myAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    myList.setAdapter(myAdapter);



     }

   }

CustomListViewAdapter

package com.example.tefa.mycustomlist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class CustomListViewAdapter extends BaseAdapter implements ListAdapter {


private Context mContext;
private ArrayList<HashMap<String, String>> books;
private static LayoutInflater inflater;

public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    books = data;
    mContext = context;
    inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return books.size();
}

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (convertView == null) {
        view = inflater.inflate(R.layout.list_row, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView author = (TextView) view.findViewById(R.id.author);
        TextView pages = (TextView) view.findViewById(R.id.pages);
        ImageView image = (ImageView) view.findViewById(R.id.listImage);

        HashMap<String, String> mBook = books.get(position);

        title.setText(mBook.get("title"));
        author.setText(mBook.get("author"));
        pages.setText(mBook.get("pages"));
        image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));


    }

    return view;
  }
}

1 个答案:

答案 0 :(得分:1)

只需更换适配器

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

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by damodhar.meshram on 3/7/2016.
 */
public class CustomListViewAdapter extends BaseAdapter implements ListAdapter {


    private Context mContext;
    private ArrayList<HashMap<String, String>> books;
    private static LayoutInflater inflater;

    public CustomListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {
        books = data;
        mContext = context;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return books.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder ;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_row, null);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.author = (TextView) convertView.findViewById(R.id.author);
            holder.pages = (TextView) convertView.findViewById(R.id.pages);
            holder.image = (ImageView) convertView.findViewById(R.id.listImage);


            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }



        HashMap<String, String> mBook = books.get(position);

        holder.title.setText(mBook.get("title"));
        holder.author.setText(mBook.get("author"));
        holder.pages.setText(mBook.get("pages"));
        holder.image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));

        return convertView;
    }

    static class ViewHolder {
        private TextView title;
        private TextView pages;
        private TextView author;
        private ImageView image;
    }

}