我有一个 main.xml 有一个 listview 。 listview 的一行在 row.xml 中定义如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center"
android:textColor="#FF0000"
android:text="sdfasdf" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:background="#FFDA35"
android:layout_height="70dp" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
如您所见,每个listview行都有一个水平滚动视图,当listview生成行时,我想在其中动态加载scrollview子项。 Scrollview子项在 scrollview_child.xml :
中定义<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@null" >
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFF" >
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@string/app_name"
android:gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/img"
android:layout_alignRight="@+id/img"
android:layout_below="@+id/img"
android:gravity="center" />
</RelativeLayout>
我的java代码: 的 MainActivity.java
public class MainActivity extends ActionBarActivity {
ListView list;
private ArrayList<String> people;
private ArrayList<String> school;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.list);
createLists();
ListViewAdapter listAdapter = new ListViewAdapter(this, people, school);
list.setAdapter(listAdapter);
}
public void createLists() {
people = new ArrayList<String>();
people.add("Bob");
people.add("Peter");
people.add("Anna");
people.add("Jane");
school = new ArrayList<String>();
school.add("A");
school.add("B");
school.add("C");
school.add("D");
school.add("E");
}
}
ListViewAdapter.java :
public class ListViewAdapter extends BaseAdapter{
private ArrayList<String> people;
private ArrayList<String> school;
MainActivity activity;
public ListViewAdapter(MainActivity activity, ArrayList<String> people, ArrayList<String> school) {
this.people = people;
this.school = school;
this.activity = activity;
}
private static class ViewHolder {
private TextView cateName;
private ImageView cateImage;
private LinearLayout ll;
private ViewHolder (View v) {
cateImage = (ImageView)v.findViewById(R.id.image);
cateName = (TextView)v.findViewById(R.id.text);
ll = (LinearLayout)v.findViewById(R.id.ll);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return people.size();
}
@Override
public Object getItem(int position) {
return people.get(position);
}
@Override
public long getItemId(int position) {
return people.indexOf(getItem(position));
}
@SuppressLint("InlinedApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("run", "run");
ViewHolder holder = null;
LayoutInflater infllater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = infllater.inflate(R.layout.home_page_list_odd_item, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
// 1 row element
holder.cateImage.setImageResource(R.drawable.defaultavatar);
holder.cateName.setText(people.get(position));
Log.i("size: ", "school: " + school.size() + "people: " + people.size());
// set data for scroll view
for (int i = 0; i<school.size(); i++) {
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.home_page_scroll_list_item, parent, false);
TextView schoolName = (TextView) v.findViewById(R.id.name);
ImageView schoolImage = (ImageView)v.findViewById(R.id.img);
RelativeLayout rl = (RelativeLayout)v.findViewById(R.id.rl);
schoolName.setText(school.get(i));
schoolImage.setImageResource(R.drawable.ic_launcher);
rl.setBackgroundResource(R.drawable.bg_deal);
holder.ll.addView(v);
}
return convertView;
}
}
我的问题是水平滚动视图中的子项是重复的,因为方法getView()正在多次调用。 我的结果如下:http://i.imgur.com/70tte3I.png
请帮忙!对不起,我的英语很糟糕!