欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)下拉展示條目效果

 更新時間:2019年01月28日 11:44:16   作者:常利兵  
這篇文章主要為大家詳細介紹了Android實現(xiàn)下拉展示條目效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android下拉展示條目的具體代碼,供大家參考,具體內(nèi)容如下

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000"
tools:context="com.example.a2_.MainActivity">

<EditText
  android:id="@+id/et_input"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textColor="#fff"
  android:text="請輸入內(nèi)容" />

<ImageButton
  android:padding="6dp"
  android:background="@null"
  android:layout_alignRight="@id/et_input"
  android:id="@+id/ib"
  android:src="@mipmap/down_arrow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</RelativeLayout>

條目布局文件

<?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:padding="5dp"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">

<ImageView

  android:src="@mipmap/user"
  android:id="@+id/iv_user"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

<TextView
  android:layout_weight="1"
  android:id="@+id/tv_data"
  android:text="100"
  android:textColor="#000000"
  android:gravity="center_horizontal"
  android:layout_width="0dp"
  android:layout_height="wrap_content" />

<ImageButton
  android:background="@null"
  android:src="@mipmap/delete"
  android:id="@+id/iv_delete"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>

核心代碼

package com.example.a2_;

import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private EditText et_input;
private PopupWindow popupWindow;
private ListView lv_list;
private ArrayList<String> data;
private MyAdapter myAdapter;

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

  //初始化控件
  et_input = (EditText) findViewById(R.id.et_input);
  ImageButton ib = (ImageButton) findViewById(R.id.ib);

  //初始化數(shù)據(jù)源
  initData();
  //創(chuàng)建一個listView
  lv_list = new ListView(this);
  //給listview設置背景
  lv_list.setBackgroundResource(R.mipmap.listview_background);
  //給listview設置適配器
  myAdapter = new MyAdapter();
  lv_list.setAdapter(myAdapter);
  //給listview設置條目點擊事件
  lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      //當點擊到一個條目的時候,就把這個條目的內(nèi)容顯示在輸入框中
      et_input.setText(data.get(position));
      popupWindow.dismiss();
    }
  });

  //給按鈕設置點擊事件
  ib.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      //當點擊下拉按鈕時,顯示列表選項
      showPopup();
    }
  });

}

//初始化數(shù)據(jù)
private void initData() {
  //創(chuàng)建一個集合,用來存儲數(shù)據(jù)
  data = new ArrayList<>();
  //通過for循環(huán)創(chuàng)建數(shù)據(jù)
  for (int i = 1;i<20;i++){
    data.add(100+i+"");
  }
}

//顯示下拉的列表條目
private void showPopup() {
  //創(chuàng)建PopupWindow對象
  popupWindow = new PopupWindow(lv_list,et_input.getWidth(),500,true);
  //為了在點擊列表條目之外的區(qū)域能夠讓列表條目隱藏,為列表條目設置一個背景
  popupWindow.setBackgroundDrawable(new ColorDrawable());
  //讓列表條目顯示 并指定在哪個列表條目下展示
  popupWindow.showAsDropDown(et_input);
}

//創(chuàng)建一個適配器
class MyAdapter extends BaseAdapter{

  @Override
  public int getCount() {
    return data.size();
  }

  @Override
  public String getItem(int position) {
    return data.get(position);
  }

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

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    //判斷convertView是否為空
    if (convertView==null){
      convertView = View.inflate(MainActivity.this,R.layout.lv_item,null);

    }
    //找到控件
    TextView tv_data = (TextView) convertView.findViewById(R.id.tv_data);
    ImageView iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);

    //設置點擊事件
    iv_delete.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //刪除集合里面的數(shù)據(jù)
        data.remove(position);
        //刷新頁面
        myAdapter.notifyDataSetChanged();
      }
    });
    //獲取數(shù)據(jù)
    String data = getItem(position);
    //設置數(shù)據(jù)
    tv_data.setText(data);
    return convertView;
  }
}

}

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

相關文章

  • Android編程中的四大基本組件與生命周期詳解

    Android編程中的四大基本組件與生命周期詳解

    這篇文章主要介紹了Android編程中的四大基本組件與生命周期,結(jié)合實例形式較為詳細的分析了Android四大組件及生命周期的相關概念與使用技巧,需要的朋友可以參考下
    2015-12-12
  • 如何給Flutter界面切換實現(xiàn)點特效

    如何給Flutter界面切換實現(xiàn)點特效

    這篇文章主要給大家介紹了關于如何給Flutter界面切換實現(xiàn)點特效的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • 詳解Flutter網(wǎng)絡圖片本地緩存的實現(xiàn)

    詳解Flutter網(wǎng)絡圖片本地緩存的實現(xiàn)

    這篇文章主要為大家介紹了詳解Flutter網(wǎng)絡圖片本地緩存的實現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Android RadioGroup多行顯示效果 解決單選問題

    Android RadioGroup多行顯示效果 解決單選問題

    這篇文章主要為大家詳細介紹了Android RadioGroup多行顯示效果,解決單選問題,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android檢測IBeacon熱點的方法

    Android檢測IBeacon熱點的方法

    這篇文章主要介紹了Android檢測IBeacon熱點的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android Studio 3.6 調(diào)試 smali的全過程

    Android Studio 3.6 調(diào)試 smali的全過程

    這篇文章主要介紹了Android Studio 3.6 調(diào)試 smali, 目前最新版的 Android Studio 利用附加功能調(diào)試 smali 非常方便,具體操作步驟跟隨小編一起看看吧
    2020-02-02
  • Android開源框架的SlidingFragment的使用示例

    Android開源框架的SlidingFragment的使用示例

    今天小編就為大家分享一篇關于Android開源框架的SlidingFragment的使用示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android 攔截返回鍵事件的實例詳解

    Android 攔截返回鍵事件的實例詳解

    這篇文章主要介紹了Android 攔截返回鍵事件的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Android TextView跑馬燈效果實現(xiàn)方法

    Android TextView跑馬燈效果實現(xiàn)方法

    這篇文章主要介紹了Android TextView跑馬燈效果實現(xiàn)方法,涉及Android布局文件中相關屬性的設置技巧,非常簡單實用,需要的朋友可以參考下
    2016-01-01
  • Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法

    Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法

    這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁和數(shù)據(jù)的方法的相關資料,需要的朋友可以參考下
    2016-09-09

最新評論