我正在实现一个水平滚动视图,其中的项目包含一个 ImageView 和一个 Textview
这是我的主要活动:
public class MainActivity extends Activity implements OnClickListener{
List elementlist;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elementlist=new ArrayList<Element>();
elementlist.add(new Element("Item 1", "p1"));
elementlist.add(new Element("Item 2", "p2"));
elementlist.add(new Element("Item 3", "p3"));
elementlist.add(new Element("Item 4", "p4"));
elementlist.add(new Element("Item 5", "p5"));
layout=(LinearLayout)findViewById(R.id.lay);
populate_list();
}
private void populate_list()
{
Iterator<Element> i=elementlist.iterator();
while(i.hasNext())
{
Element e= (Element)i.next();
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.list_element, null);
ImageView iw=(ImageView)v.findViewById(R.id.imageView1);
TextView tw=(TextView)v.findViewById(R.id.textView1);
iw.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(e.getImagename(), "drawable", this.getPackageName())));
tw.setText(e.getDisplayname());
layout.addView(v);
}
}
}
元素类:
public class Element {
private String displayname;
private String imagename;
public Element(String displayname, String imagename) {
this.displayname = displayname;
this.imagename = imagename;
}
public String getDisplayname() {
return displayname;
}
public String getImagename() {
return imagename;
}}
活动主.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:id="@+id/lay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
和listelement.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<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" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_centerHorizontal="true"
android:background="#000000"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
现在,我的情况是我有两个要求:
滚动列表的大小应减小(如诺基亚 N72、N95 等图库的情况)
滚动完成后,列表应该自动跳转到最近的项目并且大小再次增加,即该项目的宽度适合屏幕宽度或至少位于中心。
我怎样才能满足我的要求?
我想完全这样做
当用户滚动时,它应该如下所示: