Skip to content

Commit

Permalink
增加了CommonPickerPopup通用列表选择器实现
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed May 11, 2021
1 parent 59c9833 commit 18f6d66
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 5 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ popup.setCityPickerListener(new CityPickerListener() {
new XPopup.Builder(MainActivity.this)
.asCustom(popup)
.show();


- 通用列表选择器
```java
CommonPickerPopup popup = new CommonPickerPopup(MainActivity.this);
ArrayList<String> list = new ArrayList<String>();
list.add("小猫");
list.add("小狗");
list.add("小羊");
popup.setPickerData(list)
.setCurrentItem(1);
popup.setCommonPickerListener(new CommonPickerListener() {
@Override
public void onItemSelected(int index, String data) {
Toast.makeText(MainActivity.this, "选中的是 "+ data, Toast.LENGTH_SHORT).show();
}
});
new XPopup.Builder(MainActivity.this)
.asCustom(popup)
.show();
```
25 changes: 25 additions & 0 deletions app/src/main/java/com/lxj/xpopupextdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import android.widget.Toast;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopupext.listener.CityPickerListener;
import com.lxj.xpopupext.listener.CommonPickerListener;
import com.lxj.xpopupext.listener.TimePickerListener;
import com.lxj.xpopupext.popup.CityPickerPopup;
import com.lxj.xpopupext.popup.CommonPickerPopup;
import com.lxj.xpopupext.popup.TimePickerPopup;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

Expand Down Expand Up @@ -73,5 +76,27 @@ public void onCityChange(String province, String city, String area) {
.show();
}
});

findViewById(R.id.btnCommon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommonPickerPopup popup = new CommonPickerPopup(MainActivity.this);
ArrayList<String> list = new ArrayList<String>();
list.add("小猫");
list.add("小狗");
list.add("小羊");
popup.setPickerData(list)
.setCurrentItem(1);
popup.setCommonPickerListener(new CommonPickerListener() {
@Override
public void onItemSelected(int index, String data) {
Toast.makeText(MainActivity.this, "选中的是 "+ data, Toast.LENGTH_SHORT).show();
}
});
new XPopup.Builder(MainActivity.this)
.asCustom(popup)
.show();
}
});
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
android:text="显示城市选择器"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnCommon"
android:text="显示通用列表选择器"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.lxj.xpopupext.adapter;

import android.util.Log;

import com.contrarywind.adapter.WheelAdapter;

import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lxj.xpopupext.listener;

public interface CommonPickerListener {
void onItemSelected(int index, String data);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.lxj.xpopupext.listener;

import android.view.View;

/**
* Created by xiaosong on 2018/3/20.
*/
Expand Down
124 changes: 124 additions & 0 deletions ext/src/main/java/com/lxj/xpopupext/popup/CommonPickerPopup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.lxj.xpopupext.popup;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;

import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopupext.R;
import com.lxj.xpopupext.adapter.ArrayWheelAdapter;
import com.lxj.xpopupext.listener.CommonPickerListener;
import com.lxj.xpopupext.listener.ISelectTimeCallback;
import com.lxj.xpopupext.listener.TimePickerListener;
import com.lxj.xpopupext.view.WheelTime;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class CommonPickerPopup extends BottomPopupView {

private int itemsVisibleCount = 7;
private int itemTextSize = 18;
public int dividerColor = 0xFFd5d5d5; //分割线的颜色
public float lineSpace = 2.4f; // 条目间距倍数 默认2
public int textColorOut = 0xFFa8a8a8; //分割线以外的文字颜色
public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色
private WheelView wheelView;
public CommonPickerPopup(@NonNull Context context) {
super(context);
}

@Override
protected int getImplLayoutId() {
return R.layout._xpopup_ext_common_picker;
}

@Override
protected void onCreate() {
super.onCreate();
wheelView = findViewById(R.id.commonWheel);
findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
TextView btnConfirm = findViewById(R.id.btnConfirm);
btnConfirm.setTextColor(XPopup.getPrimaryColor());
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int index = wheelView.getCurrentItem();
if(commonPickerListener!=null) commonPickerListener.onItemSelected(index, list.get(index));
dismiss();
}
});
initWheelData();
}

private void initWheelData() {
wheelView.setItemsVisibleCount(itemsVisibleCount);
wheelView.setAlphaGradient(true);
wheelView.setTextSize(itemTextSize);
wheelView.setCyclic(false);
wheelView.setDividerColor(dividerColor);
wheelView.setDividerType(WheelView.DividerType.FILL);
wheelView.setLineSpacingMultiplier(lineSpace);
wheelView.setTextColorOut(textColorOut);
wheelView.setTextColorCenter(textColorCenter);
wheelView.isCenterLabel(false);
wheelView.setCurrentItem(currentItem);
wheelView.setAdapter(new ArrayWheelAdapter<>(list));
wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
}
});
}
private CommonPickerListener commonPickerListener;
public CommonPickerPopup setCommonPickerListener(CommonPickerListener commonPickerListener){
this.commonPickerListener = commonPickerListener;
return this;
}
public CommonPickerPopup setItemTextSize(int textSize){
this.itemTextSize = textSize;
return this;
}

public CommonPickerPopup setItemsVisibleCount(int itemsVisibleCount){
this.itemsVisibleCount = itemsVisibleCount;
return this;
}
public CommonPickerPopup setLineSpace(float lineSpace){
this.lineSpace = lineSpace;
return this;
}
List<String> list = new ArrayList<>();
/**
* 设置选项数据
*/
public CommonPickerPopup setPickerData(List<String> list) {
this.list = list;
return this;
}

int currentItem = 0;
/**
* 设置默认选中
*/
public CommonPickerPopup setCurrentItem(int currentItem) {
this.currentItem = currentItem;
return this;
}


}
52 changes: 52 additions & 0 deletions ext/src/main/res/layout/_xpopup_ext_common_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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:orientation="vertical">

<LinearLayout
android:background="#f5f5f5"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">

<TextView
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:text="@string/xpopup_cancel"
android:textAllCaps="false"
android:textColor="@color/_xpopup_dark_color"
android:textSize="16sp" />

<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>

<TextView
android:id="@+id/btnConfirm"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textColor="@color/_xpopup_dark_color"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/xpopup_ok"
android:textAllCaps="false"
android:textSize="16sp" />
</LinearLayout>


<com.contrarywind.view.WheelView
android:id="@+id/commonWheel"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

0 comments on commit 18f6d66

Please sign in to comment.