android 32 Gallery:橫著滾動的列表

來源:互聯網
上載者:User

標籤:

Gallery:橫著滾動的列表

 mainActivity.java

package com.sxt.day05_01;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.GridView;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.sxt.day05_01.entity.GeneralBean;public class MainActivity extends Activity {    Gallery mGallery;    List<GeneralBean> mGenerals;//代表十個軍事家的集合    GeneralAdapter mAdapter;    int[] resid={        R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,        R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,        R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,        R.drawable.zhuyuanzhang    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initData();//初始化資料        initView();        setListener();    }        private void setListener() {        setOnItemClickListener();        setOnItemLongClickListener();            }    private void setOnItemLongClickListener() {        mGallery.setOnItemLongClickListener(new OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> parent, View view,                    int position, long id) {                Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被長按", 2000).show();                return true;            }        });    }    private void setOnItemClickListener() {        mGallery.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                Toast.makeText(MainActivity.this, mGenerals.get(position).getName()+"被短按", 2000).show();            }        });    }    private void initView() {        mGallery=(Gallery) findViewById(R.id.galleryGeneral);        mAdapter=new GeneralAdapter();//建立適配器        mGallery.setAdapter(mAdapter);//關聯適配器    }    private void initData() {        //將資源中的字串組數轉換為Java數組        String[] names=getResources().getStringArray(R.array.generals);        mGenerals=new ArrayList<GeneralBean>();        for (int i = 0; i < names.length; i++) {            GeneralBean bean=new GeneralBean(resid[i], names[i]);            mGenerals.add(bean);        }    }    //適配器    class GeneralAdapter extends BaseAdapter{        @Override        public int getCount() {            return Integer.MAX_VALUE;//設定Gallery的長度為21億,使得水平滾動的列表到最後的時候又從第一個開始        }        @Override        public GeneralBean getItem(int position) {            return mGenerals.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        //滾動的時候顯示Gallery中第position位置的單個布局,position從0到21億,使得水平滾動的列表到最後的時候又從第一個開始        public View getView(int position, View convertView, ViewGroup parent) {            //拿到ListViewItem的布局,轉換為View類型的對象            View layout=View.inflate(MainActivity.this, R.layout.item_generals, null);            ImageView ivThumb=(ImageView) layout.findViewById(R.id.ivThumb);            TextView tvName=(TextView) layout.findViewById(R.id.tvName);                        //position%mGenerals.size()使得水平滾動的列表到最後的時候又從第一個開始            GeneralBean bean=mGenerals.get(position%mGenerals.size());            ivThumb.setImageResource(bean.getResid());            tvName.setText(bean.getName());            return layout;//返回第position位置的單個布局,也就是item_generals.xml的LinearLayout        }            }}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Gallery        android:id="@+id/galleryGeneral"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:spacing="2dp"/></RelativeLayout>
item_generals.xml
<?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="wrap_content"    android:orientation="vertical" >    <ImageView         android:id="@+id/ivThumb"        android:layout_width="80dp"        下字        android:layout_height="80dp"        android:scaleType="fitXY"        android:src="@drawable/baiqi"/>    <TextView         android:id="@+id/tvName"        android:layout_width="80dp"        android:layout_height="wrap_content"        android:text="白起"        android:textSize="20sp"        android:gravity="center_horizontal"/></LinearLayout>

GeneralBean.java

public class GeneralBean {    private int resid;//圖片的id值    private String name;//軍事家的姓名    public int getResid() {        return resid;    }

 

android 32 Gallery:橫著滾動的列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.