Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ CSDN:[http://blog.csdn.net/iamdingruihaha/article/details/71422269](http://blog.
   hsMain.setAnRightOffset();//向右移动一个单元

hsMain.getSelectedString();//获得被选中的文本


hsMain.setOnSlideListener();//当左右滑动时,获取当前所选项的指针


```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity implements View.OnClickListener, HorizontalselectedView.OnHorizontalViewSlideListener {

private View leftImageView;
private View rightImageView;
private HorizontalselectedView hsMain;
private Button btMain;
List<String> strings = new ArrayList<String>();
private TextView tvMain;
private TextView tvMainCurrent;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -42,11 +43,12 @@ private void initView() {
rightImageView = findViewById(R.id.iv_right);
btMain = ((Button) findViewById(R.id.bt_main));
tvMain = ((TextView) findViewById(R.id.tv_main));

tvMainCurrent = ((TextView) findViewById(R.id.tv_main_current));

leftImageView.setOnClickListener(this);
rightImageView.setOnClickListener(this);
btMain.setOnClickListener(this);
hsMain.setOnSlideListener(this);
}


Expand All @@ -68,4 +70,9 @@ public void onClick(View view) {
break;
}
}

@Override
public void currentIndex(int index) {
tvMainCurrent.setText("所选项指针:" + index);
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,15 @@
android:layout_marginTop="20dp"
android:background="@color/gray" />

<TextView
android:padding="5dp"
android:gravity="center"
android:id="@+id/tv_main_current"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:background="@color/gray" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class HorizontalselectedView extends View {
private int textWidth = 0;
private int textHeight = 0;
private int centerTextHeight = 0;
private OnHorizontalViewSlideListener horizontalView;


public HorizontalselectedView(Context context) {
Expand Down Expand Up @@ -120,6 +121,10 @@ public boolean onTouchEvent(MotionEvent event) {
anOffset = 0;
n = n - 1;
downX = scrollX;
// 回调
if (null != horizontalView) {
horizontalView.currentIndex(n);
}
}
}
} else {
Expand All @@ -131,6 +136,10 @@ public boolean onTouchEvent(MotionEvent event) {
anOffset = 0;
n = n + 1;
downX = scrollX;
// 回调
if (null != horizontalView) {
horizontalView.currentIndex(n);
}
}
}
}
Expand Down Expand Up @@ -162,6 +171,10 @@ protected void onDraw(Canvas canvas) {
if (n >= 0 && n <= strings.size() - 1) {//加个保护;防止越界

String s = strings.get(n);//得到被选中的文字
// 回调
if (null != horizontalView) {
horizontalView.currentIndex(n);
}
/**
* 得到被选中文字 绘制时所需要的宽高
*/
Expand Down Expand Up @@ -251,4 +264,19 @@ public String getSelectedString() {
}
return null;
}

/**
* 设置回调函数
* @param horizontalView
*/
public void setOnSlideListener(OnHorizontalViewSlideListener horizontalView) {
this.horizontalView = horizontalView;
}

/**
* 回调函数
*/
public interface OnHorizontalViewSlideListener {
void currentIndex(int index);
}
}