Android實(shí)現(xiàn)下拉展示條目效果
本文實(shí)例為大家分享了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="請(qǐng)輸入內(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)建一個(gè)listView lv_list = new ListView(this); //給listview設(shè)置背景 lv_list.setBackgroundResource(R.mipmap.listview_background); //給listview設(shè)置適配器 myAdapter = new MyAdapter(); lv_list.setAdapter(myAdapter); //給listview設(shè)置條目點(diǎn)擊事件 lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //當(dāng)點(diǎn)擊到一個(gè)條目的時(shí)候,就把這個(gè)條目的內(nèi)容顯示在輸入框中 et_input.setText(data.get(position)); popupWindow.dismiss(); } }); //給按鈕設(shè)置點(diǎn)擊事件 ib.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //當(dāng)點(diǎn)擊下拉按鈕時(shí),顯示列表選項(xiàng) showPopup(); } }); } //初始化數(shù)據(jù) private void initData() { //創(chuàng)建一個(gè)集合,用來(lái)存儲(chǔ)數(shù)據(jù) data = new ArrayList<>(); //通過(guò)for循環(huán)創(chuàng)建數(shù)據(jù) for (int i = 1;i<20;i++){ data.add(100+i+""); } } //顯示下拉的列表?xiàng)l目 private void showPopup() { //創(chuàng)建PopupWindow對(duì)象 popupWindow = new PopupWindow(lv_list,et_input.getWidth(),500,true); //為了在點(diǎn)擊列表?xiàng)l目之外的區(qū)域能夠讓列表?xiàng)l目隱藏,為列表?xiàng)l目設(shè)置一個(gè)背景 popupWindow.setBackgroundDrawable(new ColorDrawable()); //讓列表?xiàng)l目顯示 并指定在哪個(gè)列表?xiàng)l目下展示 popupWindow.showAsDropDown(et_input); } //創(chuàng)建一個(gè)適配器 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); //設(shè)置點(diǎn)擊事件 iv_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //刪除集合里面的數(shù)據(jù) data.remove(position); //刷新頁(yè)面 myAdapter.notifyDataSetChanged(); } }); //獲取數(shù)據(jù) String data = getItem(position); //設(shè)置數(shù)據(jù) tv_data.setText(data); return convertView; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android RadioGroup多行顯示效果 解決單選問(wèn)題
這篇文章主要為大家詳細(xì)介紹了Android RadioGroup多行顯示效果,解決單選問(wèn)題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android檢測(cè)IBeacon熱點(diǎn)的方法
這篇文章主要介紹了Android檢測(cè)IBeacon熱點(diǎn)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android Studio 3.6 調(diào)試 smali的全過(guò)程
這篇文章主要介紹了Android Studio 3.6 調(diào)試 smali, 目前最新版的 Android Studio 利用附加功能調(diào)試 smali 非常方便,具體操作步驟跟隨小編一起看看吧2020-02-02Android開(kāi)源框架的SlidingFragment的使用示例
今天小編就為大家分享一篇關(guān)于Android開(kāi)源框架的SlidingFragment的使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Android TextView跑馬燈效果實(shí)現(xiàn)方法
這篇文章主要介紹了Android TextView跑馬燈效果實(shí)現(xiàn)方法,涉及Android布局文件中相關(guān)屬性的設(shè)置技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-01-01Android使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法
這篇文章主要介紹了Android使用原生組件WebView加載網(wǎng)頁(yè)和數(shù)據(jù)的方法的相關(guān)資料,需要的朋友可以參考下2016-09-09