Skip to content

Commit

Permalink
Add Click Listener to views
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-4444 committed Jul 19, 2018
1 parent be2df31 commit 7c8c1ee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import mishmarot.guy.com.timelineunit.CallBack_StepViewClick;
import mishmarot.guy.com.timelineunit.StepLineLayout;
import mishmarot.guy.com.timelineunit.StepLineView;

Expand Down Expand Up @@ -33,7 +35,12 @@ protected void onCreate(Bundle savedInstanceState) {
stepLayout_2.setStepLines(this, StepLineView.LineOrientation.VERTICAL, 12, Color.argb(123, 0, 0, 255), Color.argb(123, 0, 255, 0), Color.argb(123, 0, 255, 0), 3, 20, 10);
stepLayout_3.setStepLines(this, StepLineView.LineOrientation.VERTICAL, 5, R.color.orange, R.color.yellow, R.color.orange);
stepLayout_4.setStepLines(this, StepLineView.LineOrientation.VERTICAL, 6, Color.argb(200, 255, 0, 0), Color.argb(100, 255, 0, 0), Color.argb(123, 200, 200, 0), 4, 25, 0);
stepLayout_5.setStepLines(this, StepLineView.LineOrientation.VERTICAL, 40, R.color.colorPrimary, android.R.color.darker_gray, Color.argb(123, 200, 200, 0), 4, 25, 0);
stepLayout_5.setStepLines(this, StepLineView.LineOrientation.VERTICAL, 40, R.color.colorPrimary, android.R.color.darker_gray, Color.argb(123, 200, 200, 0), 4, 25, 0, new CallBack_StepViewClick() {
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this, position + " Clicked!!", Toast.LENGTH_SHORT).show();
}
});



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mishmarot.guy.com.timelineunit;

public interface CallBack_StepViewClick {
public void onClick(int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ private void init(Context context) {
}

public void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor, int _lineSize, int _markerRadius, int _linePadding){
setStepLines(context, lineOrientation, _numOfItems, _mainColor, _secondColor, _lineColor, _lineSize, _markerRadius, _linePadding, null);
}

public void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor, int _lineSize, int _markerRadius, int _linePadding, CallBack_StepViewClick _callBack_stepViewClick){
this.lineSize = _lineSize;
this.markerRadius = _markerRadius;
this.linePadding = _linePadding;
setStepLines(context, _lineOrientation, _numOfItems, _mainColor, _secondColor, _lineColor);
setStepLines(context, _lineOrientation, _numOfItems, _mainColor, _secondColor, _lineColor, _callBack_stepViewClick);
}

public void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor){
setStepLines(context, _lineOrientation, _numOfItems, _mainColor, _secondColor, _lineColor, null);
}

public void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int _numOfItems, int _mainColor, int _secondColor, int _lineColor, CallBack_StepViewClick callBack_stepViewClick){

// If the user send resource int instead of Color int..
try {
Expand All @@ -64,10 +72,10 @@ public void setStepLines(Context context, StepLineView.LineOrientation _lineOrie
this.mainColor = _mainColor;
this.lineColor = _lineColor;
this.secondColor = _secondColor;
setStepLines(context, _lineOrientation, _numOfItems);
setStepLines(context, _lineOrientation, _numOfItems, callBack_stepViewClick);
}

private void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int numOfItems) {
private void setStepLines(Context context, StepLineView.LineOrientation _lineOrientation, int numOfItems, CallBack_StepViewClick _callBack_stepViewClick) {
stepLineViews = new ArrayList<>();

this.lineOrientation = _lineOrientation;
Expand All @@ -80,20 +88,20 @@ private void setStepLines(Context context, StepLineView.LineOrientation _lineOri
return;
}
else if (numOfItems == 1) {
StepLineView stepLineView4 = getStepLineUnit(context, StepLineView.LineType.ONLY_ONE, StepLineView.OrderStatus.ACTIVE);
StepLineView stepLineView4 = getStepLineUnit(context, StepLineView.LineType.ONLY_ONE, StepLineView.OrderStatus.ACTIVE, stepLineViews.size(), _callBack_stepViewClick);
stepLineViews.add(stepLineView4);
}
else {
// numOfItems > 1
StepLineView stepLineView0 = getStepLineUnit(context, StepLineView.LineType.BEGIN, StepLineView.OrderStatus.ACTIVE);
StepLineView stepLineView0 = getStepLineUnit(context, StepLineView.LineType.BEGIN, StepLineView.OrderStatus.ACTIVE, stepLineViews.size(), _callBack_stepViewClick);
stepLineViews.add(stepLineView0);

for (int i = 1; i < numOfItems-1; i++) {
StepLineView stepLineView1 = getStepLineUnit(context, StepLineView.LineType.NORMAL, StepLineView.OrderStatus.ACTIVE);
StepLineView stepLineView1 = getStepLineUnit(context, StepLineView.LineType.NORMAL, StepLineView.OrderStatus.ACTIVE, stepLineViews.size(), _callBack_stepViewClick);
stepLineViews.add(stepLineView1);
}

StepLineView stepLineView2 = getStepLineUnit(context, StepLineView.LineType.END, StepLineView.OrderStatus.ACTIVE);
StepLineView stepLineView2 = getStepLineUnit(context, StepLineView.LineType.END, StepLineView.OrderStatus.ACTIVE, stepLineViews.size(), _callBack_stepViewClick);
stepLineViews.add(stepLineView2);
}

Expand Down Expand Up @@ -121,7 +129,7 @@ public void setUnitCompleted(int completedIndex) {
stepLineViews.get(completedIndex).setOrderStatus(StepLineView.OrderStatus.COMPLETED);
}

public StepLineView getStepLineUnit(Context context, StepLineView.LineType lineType, StepLineView.OrderStatus orderStatus) {
public StepLineView getStepLineUnit(Context context, StepLineView.LineType lineType, StepLineView.OrderStatus orderStatus, int _position, CallBack_StepViewClick _callBack_stepViewClick) {
// markerSize - default=20
LinearLayout.LayoutParams linearParams;
if (lineOrientation == StepLineView.LineOrientation.HORIZONTAL)
Expand All @@ -144,6 +152,8 @@ public StepLineView getStepLineUnit(Context context, StepLineView.LineType lineT
stepLineView.setLinePadding(linePadding);
stepLineView.setDrawables();
stepLineView.setOrderStatus(orderStatus);
stepLineView.setPosition(_position);
stepLineView.setCallBack_stepViewClick(_callBack_stepViewClick);

return stepLineView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class StepLineView extends View {
private Rect mBounds;
private Context mContext;

private int position = -1;
private CallBack_StepViewClick callBack_stepViewClick;

Drawable a1, a2, a3;

public StepLineView(Context context, AttributeSet attrs) {
Expand Down Expand Up @@ -378,6 +381,25 @@ public void setOrderStatus(OrderStatus orderStatus) {
invalidate();
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

public void setCallBack_stepViewClick(final CallBack_StepViewClick callBack_stepViewClick) {
this.callBack_stepViewClick = callBack_stepViewClick;

this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
callBack_stepViewClick.onClick(position);
}
});
}

public int getMainColor() {
return mainColor;
}
Expand Down

0 comments on commit 7c8c1ee

Please sign in to comment.