Skip to content

Instantly share code, notes, and snippets.

@marinoluck
Created April 22, 2015 22:53
Show Gist options
  • Save marinoluck/9c58c21be9bafd1e40b1 to your computer and use it in GitHub Desktop.
Save marinoluck/9c58c21be9bafd1e40b1 to your computer and use it in GitHub Desktop.
unscrambling input class
package vc.zl.quizchat.view.component.chat;
import java.util.ArrayList;
import java.util.List;
import vc.zl.quizchat.R;
import vc.zl.quizchat.bussiness.GameManager;
import vc.zl.quizchat.domain.quiz.UnscramblingSentencePrompt;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class UnscramblingInput extends LinearLayout {
@SuppressWarnings("unused")
private static final String TAG = "UnscramblingInput";
private UnscramblingSentencePrompt model;
public CharSequence dragData;
private LinearLayout ll;
private LinearLayout lltarget;
private List<View> views;
private List<View> viewsTarget;
private Button button;
public UnscramblingInput(Context context, AttributeSet attr) {
super(context, attr);
initializeLayoutBasics(context);
}
public UnscramblingInput(Context context) {
super(context);
initializeLayoutBasics(context);
}
@SuppressLint("NewApi")
public void setModel(final UnscramblingSentencePrompt model) {
views = new ArrayList<View>();
viewsTarget = new ArrayList<View>();
//for (int i = 0; i <= 8; i++) {
for (int i : model.getOptions().keySet()) {
UnscramblingWord t = new UnscramblingWord(getContext());
t.setText(model.getOptions().get(i));
t.setOnTouchListener(new ChoiceTouchListener());
views.add(t);
}
for (int i : model.getCorrect().keySet()) {
UnscramblingWordTarget rl = new UnscramblingWordTarget(getContext());
// rl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.WRAP_CONTENT));
rl.setOnDragListener(new ChoiceDragListener2());
rl.setPosition(i);
rl.setText("________ ");
viewsTarget.add(rl);
}
ll = (LinearLayout) findViewById(R.id.holder);
lltarget = (LinearLayout) findViewById(R.id.holder_target);
popuplatePro(ll, views, getContext());
popuplatePro(lltarget, viewsTarget, getContext());
}
private void initializeLayoutBasics(Context context) {
setOrientation(HORIZONTAL);
final LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.input_unscrambling, this);
button = (Button) findViewById(R.id.unscrambling_ready);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ready();
}
});
}
public UnscramblingSentencePrompt getModel() {
return model;
}
/**
* ChoiceTouchListener will handle touch events on draggable views
*
*/
private final class ChoiceTouchListener implements OnTouchListener {
@SuppressLint("NewApi")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior - clip data could
* be set to pass data as part of drag - shadow can be tailored
*/
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
// start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
@SuppressLint("NewApi")
private class ChoiceDragListener2 implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
// no action necessary
break;
case DragEvent.ACTION_DRAG_EXITED:
// no action necessary
break;
case DragEvent.ACTION_DROP:
// handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
// view dragged item is being dropped on
UnscramblingWordTarget dropTarget = (UnscramblingWordTarget) v;
if (dropTarget.isOcupped())
{
break;
}
// view being dragged and dropped
UnscramblingWord dropped = (UnscramblingWord) view;
dropTarget.onDropped(dropped.getText());
ViewGroup parent = (ViewGroup) dropped.getParent();
if (parent instanceof UnscramblingWordTarget)
{
((UnscramblingWordTarget)parent).onUndropped();
}
parent.removeView(dropped);
//dropTarget.removeAllViews();
dropTarget.addView(dropped);
//popuplatePro(lltarget, viewsTarget, getContext());
//popuplatePro(ll, views, getContext());
break;
case DragEvent.ACTION_DRAG_ENDED:
// no action necessary
break;
default:
break;
}
return true;
}
}
private void populateViews(LinearLayout linearLayout, List<View> views,
Context context) {
for (View v : views)
{
linearLayout.addView(v);
}
}
/**
* Copyright 2011 Sherif Updated by Karim Varela to handle LinearLayouts
* with other views on either side.
*
* @param linearLayout
* @param views
* : The views to wrap within LinearLayout
* @param context
* @param extraView
* : An extra view that may be to the right or left of your
* LinearLayout.
* @author Karim Varela
**/
private void populateViews2(LinearLayout linearLayout, List<View> views,
Context context) {
// kv : May need to replace 'getSherlockActivity()' with 'this' or
// 'getActivity()'
linearLayout.removeAllViews();
// int maxWidth = display.getWidth() - extraView.getMeasuredWidth() -
// 20;
int maxWidth = Resources.getSystem().getDisplayMetrics().widthPixels - 50;
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.size(); i++) {
LinearLayout LL = new LinearLayout(context);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views.get(i).measure(0, 0);
params = new LinearLayout.LayoutParams(views.get(i)
.getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
LL.addView(views.get(i), params);
LL.measure(0, 0);
widthSoFar += views.get(i).getMeasuredWidth();
if (widthSoFar >= maxWidth) {
linearLayout.addView(newLL);
newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(),
LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
} else {
newLL.addView(LL);
}
}
linearLayout.addView(newLL);
}
private void rePopulateViews2(LinearLayout linearLayout, List<View> views,
Context context) {
// kv : May need to replace 'getSherlockActivity()' with 'this' or
// 'getActivity()'
linearLayout.removeAllViews();
// int maxWidth = display.getWidth() - extraView.getMeasuredWidth() -
// 20;
int maxWidth = Resources.getSystem().getDisplayMetrics().widthPixels - 50;
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);
int widthSoFar = 0;
for (int i = 0; i < views.size(); i++) {
LinearLayout LL = new LinearLayout(context);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views.get(i).measure(0, 0);
params = new LinearLayout.LayoutParams(views.get(i)
.getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 5, 0);
ViewGroup parent = (ViewGroup) views.get(i).getParent();
parent.removeView(views.get(i));
LL.addView(views.get(i), params);
LL.measure(0, 0);
widthSoFar += views.get(i).getMeasuredWidth();
if (widthSoFar >= maxWidth) {
linearLayout.addView(newLL);
newLL = new LinearLayout(context);
newLL.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(),
LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
} else {
newLL.addView(LL);
}
}
linearLayout.addView(newLL);
}
public void ready()
{
String value = "";
for (View t : viewsTarget)
{
UnscramblingWordTarget target = (UnscramblingWordTarget) t;
value = value + " " + target.getSelectedText();
}
GameManager.getInstance().onChoiseSelected(100, 12, value);
//Toast.makeText(getContext(), value, Toast.LENGTH_LONG).show();
}
public void popuplatePro(LinearLayout linearLayout, List<View> views, Context context){
linearLayout.removeAllViews();
// int maxWidth = display.getWidth() - extraView.getMeasuredWidth() -
// 20;
int maxWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
maxWidth = (int) (maxWidth * 0.7f);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout currentLL = null;
int widthSoFar = 0;
for (int i = 0; i < views.size(); i++) {
if (views.get(i).getParent() != null)
{
ViewGroup parent = (ViewGroup) views.get(i).getParent();
parent.removeView(views.get(i));
}
if (currentLL != null && (widthSoFar + views.get(i).getMeasuredWidth()) <= maxWidth)
{
currentLL.addView(views.get(i));
views.get(i).measure(0, 0);
widthSoFar += views.get(i).getMeasuredWidth();
Log.d(TAG, "NO add new ll, widthSoFar: "+ widthSoFar + "current view size: "+views.get(i).getMeasuredWidth() );
}
else
{
Log.d(TAG, "Add new ll, widthSoFar: "+ widthSoFar);
currentLL = null;
currentLL = new LinearLayout(context);
currentLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
currentLL.setGravity(Gravity.CENTER);
currentLL.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(currentLL);
widthSoFar = views.get(i).getMeasuredWidth();
currentLL.addView(views.get(i));
}
}
}
private void rePopulatePro(LinearLayout lltarget,
List<View> viewsTarget, Context context) {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment