0
我創建了自定義ListView
。它有從0到10的11個項目。 列表視圖僅顯示從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;
}
}
使用視圖持有人在實施的ListView – Jas
@Jas你能告訴我怎麼樣嗎? – Tefa
@Selvin我之前試過搜索過,但我的英文沒有幫我,謝謝你的回覆 – Tefa