ListPopupWindow spinner
程序员文章站
2022-06-24 10:13:08
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/spinner">
<TextView
android:id="@+id/et_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_12"
android:layout_marginRight="@dimen/dp_12"
android:gravity="center|start" />
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="@dimen/dp_50"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginLeft="@dimen/dp_100"
android:padding="@dimen/dp_15"
android:src="@drawable/arrow_down" />
</RelativeLayout>
package com.pdxx.xxpt.activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ListPopupWindow;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.pdxx.xxpt.R;
import com.pdxx.xxpt.utill.TimeUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class SearchHighActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.et_list)
TextView etList;
@BindView(R.id.iv_arrow)
ImageView ivArrow;
private ListPopupWindow mPopup;
private String[] mGoodArray = {"pencil", "potato", "peanut", "carrot", "cabbage", "cat"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_high);
ButterKnife.bind(this);
etList.setOnClickListener(this);
mPopup = new ListPopupWindow(this);
mPopup.setWidth(LayoutParams.WRAP_CONTENT);
mPopup.setHeight(LayoutParams.WRAP_CONTENT);
mPopup.setModal(true);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.et_list) {
setListPopu(v, mGoodArray, ivArrow, etList);
}
}
private void setListPopu(View v, String[] strings, ImageView iv, TextView tv) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mPopup.setDropDownGravity(Gravity.END);
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strings);
mPopup.setAdapter(adapter);
mPopup.setAnchorView(v);
mPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tv.setText(strings[position]);
mPopup.dismiss();
}
});
mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
iv.setImageResource(R.drawable.arrow_down);
}
});
mPopup.show();
ivArrow.setImageResource(R.drawable.arrow_up);
}
}
本文地址:https://blog.csdn.net/qq_26500807/article/details/110632933