日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android-下拉更多列表

發布時間:2024/1/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android-下拉更多列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在很多的登錄界面都有記住賬號的的這個功能,在點擊輸入賬號的時候會出現一個列表供用戶選擇。點擊其中的條目后會自動把信息填充到文本框當中。

然后我用PopupWindow組件實現了一個點擊更多按鈕實現更多列表的一個功能,這個下拉更多列表的效果如下:

接下來我們就開始一步一步的實現下拉更多列表功能。

1、首先新建一個對話框,布局文件login_layout.xml如下:`

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/background_light"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp" ><TextViewandroid:id="@+id/xygame_login_title_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="登錄"android:textSize="20sp" /></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="0.1dp"android:background="@android:color/darker_gray" /><TextViewandroid:id="@+id/xygame_login_accountPrompt_tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:paddingLeft="10dp"android:text="請登錄"android:textSize="15sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:paddingLeft="10dp"android:paddingRight="10dp" ><LinearLayoutandroid:id="@+id/login_account_ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><EditTextandroid:id="@+id/login_account_et"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="0.1dp"android:layout_weight="1"android:hint="用戶名/手機號"android:singleLine="true"android:textSize="15sp" /><RelativeLayoutandroid:id="@+id/login_down_more_rl"android:layout_width="50dp"android:layout_height="match_parent" ><ImageViewandroid:id="@+id/login_down_more_iv"android:background="@drawable/down_more_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true" /></RelativeLayout></LinearLayout><EditTextandroid:id="@+id/login_password_et"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="0.1dp"android:gravity="center_vertical"android:hint="請輸入密碼"android:inputType="textPassword"android:singleLine="true"android:textSize="15dp" /><Buttonandroid:id="@+id/login_in_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="登錄"android:textColor="#ffffff"android:textSize="15sp" /></LinearLayout></LinearLayout>
2、繼承自DialogFragment實現一個對話框,并把 布局文件加進去

<pre name="code" class="java">import java.util.ArrayList;import android.annotation.SuppressLint; import android.app.Activity; import android.app.DialogFragment; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams; import android.widget.PopupWindow.OnDismissListener;@SuppressLint("NewApi") public class LoginDialog extends DialogFragment implements OnClickListener{private View mDialog;private ImageView mMoreAccountIV;//更多列表的圖標private RelativeLayout mMoreAccountRL;//更多賬號的RelativeLayoutprivate LinearLayout moreAccountParentView; private LinearLayout mMoreAccountLL;private int mAccountPopWidth; //賬號列表的長度private ListView mAccountListView; //更多列表的listviewprivate PopupWindow mAccountPopWindow;private AccountAdapter mAdapter;private Button mLoginBtn;private EditText mAccountET,mPasswordET;private ArrayList<String> mAccountData; @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);mDialog = inflater.inflate(R.layout.login_layout, null);initView();initData();return mDialog;}/*** 初始化數據*/private void initData() {//當以前有登錄過的話則顯示更多按鈕mAccountData = new ArrayList<String>();mAccountData.add("123456");mAccountData.add("12345");mAccountData.add("1234");mAccountData.add("123");if (mAccountData.size() > 0) {mMoreAccountRL.setVisibility(View.VISIBLE);}else {mMoreAccountRL.setVisibility(View.GONE);}}private void initView() {mLoginBtn = (Button) mDialog.findViewById(R.id.login_in_btn);mMoreAccountRL = (RelativeLayout) mDialog.findViewById(R.id.login_down_more_rl);mMoreAccountLL = (LinearLayout) mDialog.findViewById(R.id.login_account_ll);mMoreAccountIV = (ImageView) mDialog.findViewById(R.id.login_down_more_iv);mAccountET = (EditText) mDialog.findViewById(R.id.login_account_et);mPasswordET = (EditText) mDialog.findViewById(R.id.login_password_et);mLoginBtn.setOnClickListener(this);mMoreAccountRL.setOnClickListener(this);}/*** 初始化更多列表*/private void initWidget() {//沒有在initData有賬號的時候就初始化更多列表是因為那個時候無法獲取賬號列表的長度if (mAdapter == null) {mAdapter = new AccountAdapter(getActivity(),mAccountData);}//設置在更多列表里的ListviewmAccountListView = new ListView(getActivity());LayoutParams moreParam = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);mAccountListView.setLayoutParams(moreParam);moreAccountParentView = new LinearLayout(getActivity());LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);moreAccountParentView.setLayoutParams(params);moreAccountParentView.addView(mAccountListView);mAccountListView.setAdapter(mAdapter);if (mAccountPopWindow == null){mAccountPopWidth = mMoreAccountLL.getWidth();mAccountPopWindow = new PopupWindow(moreAccountParentView, mAccountPopWidth, ViewGroup.LayoutParams.WRAP_CONTENT,true);}initPopupWindowHeight();mAccountPopWindow.setOutsideTouchable(false);mAccountPopWindow.setBackgroundDrawable(new BitmapDrawable());}/*** 設置顯示下拉高度 為三個item高度*/private void initPopupWindowHeight() {// 獲取ListView對應的AdapterListAdapter listAdapter = mAccountListView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;ViewGroup.LayoutParams params = mAccountListView.getLayoutParams();View listItem = listAdapter.getView(0, null, mAccountListView);listItem.measure(0, 0); // 計算子項View 的寬高totalHeight += (listItem.getMeasuredHeight()) * 3; // 3個item的總高度params.height = totalHeight+ (mAccountListView.getDividerHeight() * (listAdapter.getCount() - 1));// listView.getDividerHeight()獲取子項間分隔符占用的高度// params.height最后得到整個ListView完整顯示需要的高度mAccountListView.setLayoutParams(params);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.login_down_more_rl: //點擊下拉更多按鈕initWidget();showList();mMoreAccountIV.setBackgroundResource(R.drawable.pull_icon);break;case R.id.login_in_btn: //點擊登錄break;}}/*** 顯示列表*/private void showList() {mAccountPopWindow.showAsDropDown(mMoreAccountLL, 0, 0);mAccountPopWindow.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss() {mMoreAccountIV.setBackgroundResource(R.drawable.down_more_icon);}});}/*** dp轉化成px*/private int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}private class AccountAdapter extends BaseAdapter{private ArrayList<String> mMoreAccountList;private Activity mActivity;public AccountAdapter(Activity activity, ArrayList<String> moreAccount) {mActivity = activity;mMoreAccountList = moreAccount;}@Overridepublic int getCount() {return mMoreAccountList.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {MoreAccountHolder mHolder;if (convertView == null) {mHolder = new MoreAccountHolder();LinearLayout mainLinearLayout = new LinearLayout(mActivity);// mainLinearLayout.setBackgroundResource();LinearLayout mainLayout = new LinearLayout(mActivity);LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);mainLayout.setBackgroundResource(android.R.color.white);//account textTextView itemText = new TextView(mActivity);LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, 1);textParams.setMargins(dip2px(getActivity(),6) ,dip2px(getActivity(),6), 0,dip2px(getActivity(),6));itemText.setTextSize(12);itemText.setPadding(dip2px(getActivity(),10) , 0, 0, 0);mainLayout.addView(itemText, textParams);//delete iconImageView deleteIcon = new ImageView(mActivity);LinearLayout.LayoutParams iconParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);iconParams.gravity = Gravity.CENTER;iconParams.setMargins(0, 0, dip2px(getActivity(),30), 0);deleteIcon.setBackgroundResource(R.drawable.delete_icon);//設置刪除圖標mainLayout.addView(deleteIcon, iconParams);mainLinearLayout.addView(mainLayout, mainParams);convertView = mainLinearLayout;mHolder.popupAccountContent = itemText;mHolder.popupAccountDelete = deleteIcon;convertView.setTag(mHolder);}else{mHolder = (MoreAccountHolder) convertView.getTag();}mHolder.popupAccountContent.setText(mMoreAccountList.get(position));mHolder.popupAccountContent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mAccountET.setText(mMoreAccountList.get(position));mPasswordET.setText("123456");mAccountPopWindow.dismiss();}});//點擊刪除icon的時候操作 這個時候最好是顯示一個刪除的對話框 mHolder.popupAccountDelete.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mMoreAccountList.remove(position);}});return convertView;}class MoreAccountHolder {TextView popupAccountContent;ImageView popupAccountDelete;}}}

3、MainActivity如下:

import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MainActivity extends Activity {private Button showDialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showDialog = (Button) findViewById(R.id.showDialog);showDialog.setOnClickListener(new OnClickListener() {@SuppressLint("NewApi") @Overridepublic void onClick(View v) {LoginDialog dialog = new LoginDialog();dialog.show(getFragmentManager(), "LoginDialog");}});}}

這里是demo






總結

以上是生活随笔為你收集整理的android-下拉更多列表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。