Skip to content

Commit

Permalink
新增服务器获取按钮控件DynamicSoreView
Browse files Browse the repository at this point in the history
  • Loading branch information
FJ917 committed Jun 2, 2017
1 parent 59ca670 commit 0e65373
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package fj.mtsortbutton.lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

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

import fj.mtsortbutton.lib.Interface.IDynamicSore;
import fj.mtsortbutton.lib.Interface.ViewControl;
import fj.mtsortbutton.lib.adapter.ViewPagerAdapter;

/**
* @author FengTong
* @date 2017/6/1
*/
public class DynamicSoreView<T> extends LinearLayout {
Context mContext;
private ViewPager viewPager;
private LinearLayout llIndicator;

//选中点
private int RadioSelect;
//未选中点
private int RadioUnselected;
//圆点间距
private int distance;
//每页展示几个
private int number;
//展示数据的gridView
private Integer gridView;
//总页数
private int page;
//数据List
private List<T> dataList;

List<View> listSoreView = new ArrayList<>();
View soreView;


//接口
private IDynamicSore iDynamicSore;
//设置接口
public IDynamicSore getiDynamicSore() {
return iDynamicSore;
}

public void setiDynamicSore(IDynamicSore iDynamicSore) {
this.iDynamicSore = iDynamicSore;
}

public DynamicSoreView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater.from(context).inflate(R.layout.anfq_sore_button, this, true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
llIndicator = (LinearLayout) findViewById(R.id.llIndicator);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DynamicSoreView);
if (typedArray != null) {
//选中点
RadioSelect = typedArray.getResourceId(R.styleable.DynamicSoreView_SoreRadioSelect, R.drawable.radio_select);
//未选中点
RadioUnselected = typedArray.getResourceId(R.styleable.DynamicSoreView_SoreRadioUnselected, R.drawable.radio_unselected);
//圆点间距
distance = typedArray.getInteger(R.styleable.DynamicSoreView_SoreDistance,10);
//每页显示几个
number = typedArray.getInteger(R.styleable.DynamicSoreView_SoreNumber,8);
typedArray.recycle();
}

//设置空布局
gridView = R.layout.viewpager_default;
}

//初始化ViewPager
private void initViewPager(){
listSoreView = new ArrayList<>();
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < page; i++) {
//循环拿到传入的View
soreView = layoutInflater.inflate(gridView, null);
//通过接口回掉的形式返回当前的View,实现接口后开源拿到每个View然后进行操作
if (iDynamicSore!=null){
List<T> data;
int total = dataList.size();
if(i == page-1){
//添加按钮
data = new ArrayList<>();
for(int j = i*number;j<total;j++){
data.add(dataList.get(j));
}
}else{
data = new ArrayList<>();
int size;
if(total< number){
size = total;
}else{
size = (i+1)*number;
}
for(int j = i*number;j<size;j++){
data.add(dataList.get(j));
}
}
iDynamicSore.setGridView(soreView,i,data);
}
//将获取到的View添加到List中
listSoreView.add(soreView);
}
//设置viewPager的Adapter
viewPager.setAdapter(new ViewPagerAdapter(listSoreView));
//初始化LinearLayout,加入指示器
initLinearLayout(viewPager, page, llIndicator);
}

/**
* 设置指示器,设置ViewPager滑动事件监听
* @param viewPager --ViewPager
* @param pageSize --View的页数
* @param linearLayout --LinearLayout
*/
private void initLinearLayout(ViewPager viewPager, int pageSize, LinearLayout linearLayout) {
linearLayout.removeAllViews();
//定义数组放置指示器的点,pageSize是View个数
final ImageView[] imageViews = new ImageView[pageSize];
for (int i = 0; i < pageSize; i++) {
//创建ImageView
ImageView image = new ImageView(mContext);
//将ImageView放入数组
imageViews[i] = image;
//默认选中第一个
if (i == 0) {
//选中的点
image.setImageResource(RadioSelect);
} else {
//未选中的点
image.setImageResource(RadioUnselected);
}
//设置宽高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(distance, 0, distance, 0);
//将点添加到LinearLayout中
linearLayout.addView(image, params);
}

//ViewPager的滑动事件
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int arg0) {
//arg0当前ViewPager
for (int i = 0; i < imageViews.length; i++) {
//设置为选中的点
imageViews[arg0].setImageResource(RadioSelect);
//判断当前的点i如果不等于当前页的话就设置为未选中
if (arg0 != i) {
imageViews[i].setImageResource(RadioUnselected);
}
}
}
});
}

/**
* 设置view
* @param gridView
* @return
*/
public DynamicSoreView setGridView(Integer gridView){
this.gridView = gridView;
return this;
}
/**
* 设置view
* @param gridView
* @return
*/
public DynamicSoreView setNumColumns(GridView gridView){
gridView.setNumColumns(number/2);//设置每行GridView个数
return this;
}
/**
* 设置初始化
*/
public DynamicSoreView init(List<T> t){
this.dataList = t;
this.page = (int)Math.ceil((double) t.size()/number);//计算出有几页/这里用了ceil函数凑整,2.1=3
initViewPager();
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fj.mtsortbutton.lib.Interface;

import android.view.View;

import java.util.List;

/**
* @author FengTong
* @date 2017/6/1
*/
public interface IDynamicSore<T> {
void setGridView(View view, int type, List<T> data);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fj.mtsortbutton.lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
Expand All @@ -20,11 +21,11 @@ public class SoreButton extends LinearLayout {
private ViewPager viewPager;
private LinearLayout llIndicator;
//选中图片
private int RadioSelect = R.drawable.radio_select;
private int RadioSelect;
//未选中图片
private int RadioUnselected = R.drawable.radio_unselected;
private int RadioUnselected;
//圆点间距
private int distance = 10;
private int distance;

List<View> listSoreView = new ArrayList<>();
View soreView;
Expand All @@ -47,6 +48,17 @@ public SoreButton(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.anfq_sore_button, this, true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
llIndicator = (LinearLayout) findViewById(R.id.llIndicator);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DynamicSoreView);
if (typedArray != null) {
//选中点
RadioSelect = typedArray.getResourceId(R.styleable.DynamicSoreView_SoreRadioSelect, R.drawable.radio_select);
//未选中点
RadioUnselected = typedArray.getResourceId(R.styleable.DynamicSoreView_SoreRadioUnselected, R.drawable.radio_unselected);
//圆点间距
distance = typedArray.getInteger(R.styleable.DynamicSoreView_SoreDistance,10);
typedArray.recycle();
}
//设置空布局
listView = new ArrayList<>();
listView.add(R.layout.viewpager_default);
Expand Down Expand Up @@ -80,6 +92,7 @@ private void initViewPager(){
* @param linearLayout --LinearLayout
*/
private void initLinearLayout(ViewPager viewPager, int pageSize, LinearLayout linearLayout) {
linearLayout.removeAllViews();
//定义数组放置指示器的点,pageSize是View个数
final ImageView[] imageViews = new ImageView[pageSize];
for (int i = 0; i < pageSize; i++) {
Expand Down Expand Up @@ -129,6 +142,7 @@ public void onPageSelected(int arg0) {
* @param distance --距离
* @return
*/
@Deprecated
public SoreButton setDistance(int distance){
this.distance = distance;
return this;
Expand All @@ -139,6 +153,7 @@ public SoreButton setDistance(int distance){
* @param radioUnselected --未选中图片
* @return
*/
@Deprecated
public SoreButton setIndicator(int radioSelect,int radioUnselected){
//选中图片
RadioSelect = radioSelect;
Expand Down
13 changes: 13 additions & 0 deletions FJMtSortButtonLib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DynamicSoreView">
<!--设置选中点-->
<attr name="SoreRadioSelect" format="reference" />
<!--设置最未选中点-->
<attr name="SoreRadioUnselected" format="reference" />
<!--设置圆点间距-->
<attr name="SoreDistance" format="integer" />
<!--设置每页显示几个-->
<attr name="SoreNumber" format="integer" />
</declare-styleable>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".IndexActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity android:name=".Main2Activity" />
</application>

</manifest>
30 changes: 30 additions & 0 deletions app/src/main/java/fj/mtsortbutton/test/IndexActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fj.mtsortbutton.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IndexActivity extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
startActivity(new Intent(this,MainActivity.class));
break;
case R.id.btn2:
startActivity(new Intent(this,Main2Activity.class));
break;
}
}
}
Loading

0 comments on commit 0e65373

Please sign in to comment.