最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android DrawLayout結(jié)合ListView用法實例

 更新時間:2020年09月23日 15:03:30   作者:手撕高達(dá)的村長  
這篇文章主要介紹了Android DrawLayout結(jié)合ListView用法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

想做一個APP,設(shè)計中有側(cè)邊欄這個功能,所以現(xiàn)在開始學(xué)習(xí)下側(cè)邊欄的實現(xiàn)。

在官方的UI空間中已經(jīng)給出了DrawerLayout這個側(cè)滑的菜單空間。

因為在使用DrawerLayout的時候遇到了些問題,花了一天是時間才搞定,這里來記錄一下,免得到時候自己在掉坑里。

1.主布局一定要是DrawerLayout。

2.側(cè)欄拉出來時,要點擊空白欄關(guān)閉側(cè)欄的話,一定要把空白欄設(shè)置為FrameLayout

先上個效果圖吧:

好了,上代碼:

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <!-- 內(nèi)容欄-->
  <FrameLayout
    android:id="@+id/ly_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  <!-- 側(cè)滑欄-->
  <ListView
    android:id="@+id/list_left_drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff000"
    android:choiceMode="singleChoice"
    android:divider="#FFFFFF"
    android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>

listView里面的布局 item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
  xmlns:app="http://schemas.android.com/apk/res-auto">


  <!-- 定義一個用于顯示頭像的ImageView -->
  <ImageView
    android:id="@+id/imgtou"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:baselineAlignBottom="true"
    android:paddingLeft="8dp" />

  <!-- 定義一個豎直方向的LinearLayout,把QQ呢稱與說說的文本框設(shè)置出來 -->
  <LinearLayout
    android:id="@+id/new_line"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="8px"
      android:textColor="#1D1D1C"
      android:textSize="20sp" />

    <TextView
      android:id="@+id/says"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="8px"
      android:textColor="#B4B4B9"
      android:textSize="14sp" />

    <TextView
      android:id="@+id/time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="8px"
      android:textColor="#B4B4B9"
      android:textSize="14sp" />

  </LinearLayout>

</LinearLayout>

主程序MainActivity.java

package action.sun.com.testdraw2;

import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

  private String[] names = new String[]{"Tom", "Jack", "Json"};
  private String[] says = new String[]{"111111,2222222", "33333333~", "444444444~"};
  private String[] times = new String[]{"1天前", "3天前~", "2天前~"};
  private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
  private DrawerLayout drawer_layout;

  private ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("1", "onCreate: xxxxxxxxxxxxxxx");
    drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);

    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < names.length; i++) {
      Map<String, Object> showitem = new HashMap<String, Object>();
      showitem.put("touxiang", imgIds[i]);
      showitem.put("name", names[i]);
      showitem.put("says", says[i]);
      showitem.put("time", times[i]);
      listitem.add(showitem);
    }

    //創(chuàng)建一個simpleAdapter
    SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem,
        R.layout.item_list, new String[]{"touxiang", "name", "says","time"},
        new int[]{R.id.imgtou, R.id.name, R.id.says, R.id.time});
    //ListView 容器
    listView = (ListView) findViewById(R.id.list_left_drawer);
    listView.setAdapter(myAdapter);
    listView.setOnItemClickListener(this);

  }

  //點擊Item 顯示在幀頁面選擇的Item值
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(MainActivity.this, "提示的內(nèi)容", Toast.LENGTH_LONG).show();
    //關(guān)閉 側(cè)邊欄
    drawer_layout.closeDrawer(listView);
  }
}

到了現(xiàn)在,代碼完了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot 打包文件名增加編譯時間

    SpringBoot 打包文件名增加編譯時間

    這篇文章主要為大家介紹了SpringBoot 打包編譯時間實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Flutter runApp GestureBinding使用介紹

    Flutter runApp GestureBinding使用介紹

    這篇文章主要為大家介紹了Flutter runApp GestureBinding使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 違章查詢源碼分享

    違章查詢源碼分享

    本文主要分享了違章查詢源碼,特附上源碼下載地址和測試安裝包下載地址。需要的朋友一起來看下吧
    2016-12-12
  • Android中EditText光標(biāo)的顯示與隱藏方法

    Android中EditText光標(biāo)的顯示與隱藏方法

    這篇文章主要給大家介紹了關(guān)于Android中EditText光標(biāo)的顯示與隱藏以及Android之第一次不顯示EditText光標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • kotlin anko頁面跳轉(zhuǎn)實現(xiàn)方式,攜帶參數(shù)或flag

    kotlin anko頁面跳轉(zhuǎn)實現(xiàn)方式,攜帶參數(shù)或flag

    這篇文章主要介紹了kotlin anko頁面跳轉(zhuǎn)實現(xiàn)方式,攜帶參數(shù)或flag,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • android 引導(dǎo)界面的實現(xiàn)方法

    android 引導(dǎo)界面的實現(xiàn)方法

    現(xiàn)在越來越多程序都有引導(dǎo)頁面了。網(wǎng)上資料不全?,F(xiàn)在自己實現(xiàn)下。
    2013-06-06
  • Android開源堆疊滑動控件仿探探效果

    Android開源堆疊滑動控件仿探探效果

    這篇文章主要為大家詳細(xì)介紹了Android開源堆疊滑動控件仿探探效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android 判斷某個Activity 是否在前臺運行的實例

    Android 判斷某個Activity 是否在前臺運行的實例

    下面小編就為大家分享一篇Android 判斷某個Activity 是否在前臺運行的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Flutter給控件實現(xiàn)鉆石般的微光特效

    Flutter給控件實現(xiàn)鉆石般的微光特效

    這篇文章主要給大家介紹了關(guān)于Flutter給控件實現(xiàn)鉆石般的微光特效的相關(guān)資料,實現(xiàn)的效果非常不錯,非常適合大家做開發(fā)的時候參考,需要的朋友可以參考下
    2021-08-08
  • Android Compose remember的使用和原理詳解

    Android Compose remember的使用和原理詳解

    本文詳細(xì)介紹了Android Jetpack Compose中的remember函數(shù),它是優(yōu)化UI組件重組性能的重要工具,remember可以緩存數(shù)據(jù)或?qū)ο?避免每重組時重新計算,保持狀態(tài)不變,通過合理使用`remember`,可以提高應(yīng)用的性能和流暢度,感興趣的朋友一起看看吧
    2025-03-03

最新評論

临夏县| 右玉县| 安康市| 高州市| 紫阳县| 察哈| 峨山| 崇文区| 诸暨市| 兴山县| 齐河县| 获嘉县| 沾化县| 泰州市| 集安市| 马公市| 德昌县| 临夏县| 化州市| 文水县| 富平县| 平原县| 翁牛特旗| 桓仁| 赤壁市| 农安县| 保山市| 溆浦县| 辛集市| 会同县| 教育| 常德市| 新平| 建瓯市| 阿勒泰市| 毕节市| 无极县| 石城县| 麻栗坡县| 浏阳市| 合水县|