替換PopUpWindow(解決焦點獲取問題)實現頂部篩選菜單(頂部不變,底部陰暗)

很多人寫頂部篩選菜單或彈窗都會用到PopUpWindow,但是PopUpWindow也不是萬能的,現在商業App的一些界面功能實現使用PopUpWindow無法達到需要的效果(焦點問題,頂部明亮,底部陰暗問題)因此本人在此寫了一個demo可以實現頂部彈出篩選佈局(不使用PopUpWindow)

核心代碼如下:


package com.example.james;


import java.util.ArrayList;
import java.util.List;


import com.example.james.FlightBargainTicketTopFilterView.OnViewChangeLisener;


import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class MyPopView2 <T extends View> extends LinearLayout{


private LinearLayout ll;
RelativeLayout rl;

OnViewChangeLisener mViewChangeLisener;
MyAdapter myAdapter;
ListView mListView;
EditText mEditText;
TextView textView;
TextView textView2;
View view;
RadioButton mRadioButton;
TextView currentb;
    int currentLayoutId = -1;
Animation dropdown_in, dropdown_out;
private boolean isShowing = false;
private Context context;
private static final int OPEN_STATE = 0;
private static final int CLOSE_STATE = -1;
private int state = CLOSE_STATE;
String[][] str = {{"a","b","100","100","100","100","100","100","100","100","100","100","100"},{"c","d"},{"e","f"}};
private ImageView mImageView;
private TextView mTextView;


public MyPopView2(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
init();
}


public MyPopView2(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
init();
}


private void init() {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.my_listview, this,false);
mEditText = (EditText) view.findViewById(R.id.edt);
textView = (TextView)view.findViewById(R.id.hot);
textView2 = (TextView)view.findViewById(R.id.history);
ll = (LinearLayout)view.findViewById(R.id.list_layout);
ll.setVisibility(View.VISIBLE);
rl = (RelativeLayout)view.findViewById(R.id.bt_layout);
mListView = (ListView) view.findViewById(R.id.list);
addView(view);
initAnimate();
}


private void initAnimate(){
dropdown_in = AnimationUtils.loadAnimation(context,R.anim.dropdown_in);
        dropdown_out = AnimationUtils.loadAnimation(context,R.anim.dropdown_out);
}



private List<View> triggers = new ArrayList<View>();
public void addItem(int index, T trigger, TextView optionTextView,
ImageView optionImageView, String str){
optionTextView.setText(str);
triggers.add(trigger);
trigger.setOnClickListener(new MyClickClass(index,optionImageView,optionTextView));
mImageView = optionImageView;
mTextView = optionTextView;
}
private void showMyPopView(){
show();

}
public void closeMyPopView(){
hide();
setState(mImageView,state,mTextView);
}

private class MyClickClass implements OnClickListener{


int index;
ImageView im;
TextView tv;
MyClickClass(int index,ImageView im,TextView tv){
this.index = index;
this.im = im;
this.tv = tv;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("currentLayoutId",String.valueOf(currentLayoutId));
Log.e("index",String.valueOf(index));
if(state == OPEN_STATE&&currentLayoutId==index){
closeMyPopView();
Log.e("james","close");
}
else {
if(currentLayoutId==-1){
myAdapter = new MyAdapter(getContext(), str[index]);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new MyOnItemClickClass(str[index],tv));
showMyPopView();
}
else{
myAdapter.updateList(str[index]);
}
currentLayoutId = index;
setState(im,state,tv);
Log.e("james","open");
}
}
}
private class MyOnItemClickClass implements OnItemClickListener{


TextView tv;
String[] str;
MyOnItemClickClass(String[] str,TextView tv){
this.str = str;
this.tv =tv;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
tv.setText(str[arg2]);
closeMyPopView();
}

}

private void setState(ImageView im, int isOpen, TextView mtv) {
for (int i = 0; i < triggers.size(); i++) {
TextView tv = (TextView) triggers.get(i).findViewById(
R.id.option_item_title);
ImageView img = (ImageView) triggers.get(i).findViewById(
R.id.top_first_img);
img.setSelected(false);
tv.setSelected(false);
}
if (isOpen == OPEN_STATE) {
im.setSelected(true);
mtv.setSelected(true);
}


}



public void show() {
// TODO Auto-generated method stub
mOnMaskViewListener.onMask(1);
startAnimation(dropdown_in);
setVisibility(View.VISIBLE);
isShowing = true;
state = OPEN_STATE;

}
public void hide() {
// TODO Auto-generated method stub
mOnMaskViewListener.onMask(0);
        startAnimation(dropdown_out);
        setVisibility(View.GONE);
        currentLayoutId = -1;
        isShowing = false;
        state = CLOSE_STATE;
}
public boolean isShowing(){
return state == OPEN_STATE ?true:false;
}

OnMaskViewListener mOnMaskViewListener;


public void setOnMaskViewListener(OnMaskViewListener mOnMaskViewListener) {
this.mOnMaskViewListener = mOnMaskViewListener;
}


public interface OnMaskViewListener {
public void onMask(int flag);
}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章