Gallery畫廊,SlidingDrawer側(cè)滑

Gallery控件用于靈活展示圖片)過期

java:

package com.example.gallerytext;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;

public class MainActivity extends Activity {
    Gallery gallery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery=(Gallery) findViewById(R.id.gallery);
        GalleryAdapter galleryAdapter = new GalleryAdapter(MainActivity.this);
        gallery.setAdapter(galleryAdapter);
        //相應(yīng)的點(diǎn)擊事件
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "您點(diǎn)擊的是" + i, Toast.LENGTH_LONG).show();
            }
        });
    }


        
    }

xml里面寫Gallery屬性

<Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

繼承BaseAdapter適配器

package com.example.gallerytext;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Created by ZWH on 2016/5/16.
 */
public class GalleryAdapter extends BaseAdapter {
    private Context mContext;
    //設(shè)置要展示的圖片資源
    int[] images = {R.drawable.ic_launcher,R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4,R.drawable.f5,R.drawable.f6,R.drawable.f7,R.drawable.f8,R.drawable.f9,R.drawable.f10,};

    public GalleryAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //在此最好判斷一下view是否為空
        ImageView image = new ImageView(mContext);
        image.setImageResource(images[i]);
        image.setAdjustViewBounds(true);
        //設(shè)置寬高
        image.setLayoutParams(new Gallery.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return image;
    }
}

效果圖

image.png

安卓中1.5后加入了SlidingDrawer【隱藏式抽屜】惠猿,設(shè)計(jì)原理在你的UI布局有限的情況下悬而,放不下太多的控件的時(shí)候雳刺,可以考慮用這個(gè)隱藏式抽屜繁仁。用SlidingDrawer注意兩點(diǎn),一個(gè)是android:handle(委托要展開的圖片加載Layout配置) 和android:content(要展開的Layout Content):

主java里打

package com.example.slid;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
  
public class MainActivity extends Activity {  
  
    private GridView gridView;  
    private SlidingDrawer slidingDrawer;  
    private ImageView imageView;  
    private int[] icons={  
        R.drawable.widget01, R.drawable.widget02,  
        R.drawable.widget03, R.drawable.widget04,  
        R.drawable.widget05, R.drawable.widget06,
        R.drawable.widget07, R.drawable.widget08,
        R.drawable.widget09,
    };  
      
    private String[] items={"家園衛(wèi)士","安裝包","游戲盒子",
   "鏈接電腦","記事本","垃圾箱","流量管理","軟件修復(fù)","系統(tǒng)設(shè)置"
    };  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        gridView = (GridView)findViewById(R.id.mycontent);  
        slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer);//初始化  
        imageView = (ImageView)findViewById(R.id.my_image);//拉菜單的小箭頭  
        MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);//繼承了BaseAdapter基礎(chǔ)適配器  
        gridView.setAdapter(adapter);//設(shè)置適配  
        slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {  
            //設(shè)置打開抽屜的監(jiān)聽
            public void onDrawerOpened() {  
                //設(shè)置小箭頭的方向
                imageView.setImageResource(R.drawable.right1);  
            }  
        });  
        slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {  
              //設(shè)置關(guān)閉抽屜監(jiān)聽
            public void onDrawerClosed() {  
                imageView.setImageResource(R.drawable.left1);  
            }  
        });  
    }  
  
   
}

基礎(chǔ)適配器

package com.example.slid;

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.TextView;

public class MyGridViewAdapter extends BaseAdapter{  
  
    private Context context;  
    private String[] items;  
    private int[] icons;  
      
    public MyGridViewAdapter(Context context, String[] items, int[] icons){  
        this.context = context;  
        this.items = items;  
        this.icons = icons;  
    }  
      
    public int getCount() {  
        return items.length;  
    }  
  
    public Object getItem(int arg0) {  
        return items[arg0];  
    }  
  
    public long getItemId(int position) {  
        return position;  
    }  
  
    public View getView(int position, View convertView, ViewGroup parent) {  
        Shuju shuju=null;
        if(convertView==null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);  
            convertView = (View)layoutInflater .inflate(R.layout.wode, null);
            shuju=new Shuju();
            shuju.imageView = (ImageView)convertView.findViewById(R.id.image_view);  
            shuju.textview = (TextView)convertView.findViewById(R.id.text_view);
            convertView.setTag(shuju);
        }else{
            shuju= (Shuju) convertView.getTag();
        }
        //第二種加載布局方式
        shuju.imageView.setImageResource(icons[position]);  
        shuju.textview.setText(items[position]);  
        return convertView;  
    }  
 class Shuju{
     TextView textview;
     ImageView imageView;
    }
  
}  

主布局

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="抽屜效果"
        android:gravity="center"
        android:textSize="20sp" />

 <SlidingDrawer  
        android:id="@+id/sliding_drawer"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:handle="@+id/layout1"  
        android:content="@+id/mycontent"  
        android:orientation="horizontal"  
    >  
        <LinearLayout  
           android:id="@id/layout1"  
           android:layout_width="35dp"  
           android:layout_height="fill_parent"  
           android:gravity="center"  
           android:background="#00000000"  
        >  
           <ImageView  
               android:id="@+id/my_image"  
               android:layout_width="wrap_content"  
               android:layout_height="wrap_content"  
               android:src="@drawable/left1"  
           />  
        </LinearLayout>  
        <GridView  
            android:id="@id/mycontent"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingTop="20dip"  
            android:numColumns="3"  
            android:gravity="center"  
            android:background="#ff000000"  
        />  
    </SlidingDrawer>  
</RelativeLayout>  

內(nèi)容控件xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"  
    >  
    <ImageView
        android:id="@+id/image_view"  
        android:layout_width="80dp"  
        android:layout_height="80dp"  
        android:layout_gravity="center"
        android:layout_marginBottom="5dip"
          
    />  
    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="15dip" 
        android:layout_gravity="center"
        android:textColor="#fff" 
    />  
</LinearLayout>  

效果圖

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子硫豆,更是在濱河造成了極大的恐慌,老刑警劉巖笼呆,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熊响,死亡現(xiàn)場離奇詭異,居然都是意外死亡诗赌,警方通過查閱死者的電腦和手機(jī)汗茄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铭若,“玉大人洪碳,你說我怎么就攤上這事〉鹜溃” “怎么了瞳腌?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長环鲤。 經(jīng)常有香客問我纯趋,道長,這世上最難降的妖魔是什么冷离? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任吵冒,我火速辦了婚禮,結(jié)果婚禮上西剥,老公的妹妹穿的比我還像新娘痹栖。我一直安慰自己,他們只是感情好瞭空,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布揪阿。 她就那樣靜靜地躺著疗我,像睡著了一般。 火紅的嫁衣襯著肌膚如雪南捂。 梳的紋絲不亂的頭發(fā)上吴裤,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機(jī)與錄音溺健,去河邊找鬼麦牺。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鞭缭,可吹牛的內(nèi)容都是我干的剖膳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼岭辣,長吁一口氣:“原來是場噩夢啊……” “哼吱晒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起沦童,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤仑濒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后偷遗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躏精,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年鹦肿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辅柴。...
    茶點(diǎn)故事閱讀 40,488評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡箩溃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出碌嘀,到底是詐尸還是另有隱情涣旨,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布股冗,位于F島的核電站霹陡,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏止状。R本人自食惡果不足惜烹棉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怯疤。 院中可真熱鬧浆洗,春花似錦、人聲如沸集峦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摘昌,卻和暖如春速妖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背聪黎。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工罕容, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人挺举。 一個(gè)月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓杀赢,卻偏偏與公主長得像,于是被迫代替她去往敵國和親湘纵。 傳聞我的和親對象是個(gè)殘疾皇子脂崔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容