Android -- Gallety

仍是經過例子學習Gallety的用法.java

mian.xml:(只有一個Gallery)android

<LinearLayout 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"
    >
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
       />
</LinearLayout>

接下來是cell.xml文件:(只有一個ImageView)ide

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >  
    <ImageView
        android:id="@+id/ce_im"
        android:layout_width="match_parent"
        android:layout_height="match_parent"     
        />
</LinearLayout>

接下來是自定義的Adapter:學習

public class MyAdapter extends BaseAdapter {
    private Context context;
    private int[] p_w_picpathids;
    public MyAdapter(Context context, int[] p_w_picpathids) {
        super();
        this.context = context;
        this.p_w_picpathids = p_w_picpathids;
    }
    @Override
    public int getCount() {
        return p_w_picpathids.length;
    }
    @Override
    public Object getItem(int position) {
        return p_w_picpathids[position];
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //如下注釋部分爲動態的添加View(直接在代碼中定義視圖控件。不須要xml文件,可根據我的習慣選擇),
         /*ImageView p_w_picpathView=new ImageView(context);
         p_w_picpathView.setImageResource(p_w_picpathids[position]);
         p_w_picpathView.setLayoutParams(new
         Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
         LayoutParams.WRAP_CONTENT));*/
                                                                                     
        ImageView p_w_picpathView;
        if (convertView == null) {
            //獲取視圖
            convertView = LayoutInflater.from(context).inflate(R.layout.cell,
                    null);
            p_w_picpathView = (ImageView) convertView.findViewById(R.id.ce_im);
            p_w_picpathView.setImageResource(p_w_picpathids[position]);
        }else {
            p_w_picpathView = (ImageView) convertView.findViewById(R.id.ce_im);
            p_w_picpathView.setImageResource(p_w_picpathids[position]);
        }
        return convertView;
    }
}

MainActivity.javathis

public class MainActivity extends Activity {
    private Gallery gallery;
    private MyAdapter myadapter;
    private int[] p_w_picpaths={R.drawable.i1,R.drawable.i2,
            R.drawable.i3,R.drawable.i4};  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery=(Gallery) findViewById(R.id.gallery);      
        myadapter=new MyAdapter(this, p_w_picpaths);
        gallery.setAdapter(myadapter);             
    }
}


運行效果:(經過左右滑動能夠看到不一樣的圖片)spa


完整例子代碼見附件xml

相關文章
相關標籤/搜索