fix dragging mess

This commit is contained in:
Adrian Ulrich 2014-10-17 22:21:01 +02:00
parent 02a5274611
commit 06b5cfdafa
7 changed files with 89 additions and 17 deletions

View File

@ -29,9 +29,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
android:orientation="horizontal">
<View
android:id="@+id/dragger"
android:layout_width="20dip"
android:id="@+id/pmark"
android:layout_width="2dip"
android:layout_height="44dip"
android:visibility="invisible"
android:background="@android:color/holo_blue_dark" />
<TextView
android:id="@+id/text"
@ -48,14 +49,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight="1"/>
<!-- checkbox: off by default -->
<CheckedTextView
android:id="@+id/checkbox"
android:visibility="gone"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:layout_height="match_parent"
android:layout_width="wrap_content"/>
android:layout_width="44dip"/>
<ImageView
android:id="@+id/dragger"
android:scaleType="center"
android:src="@drawable/arrow"
android:layout_width="44dip"
android:layout_height="44dip" />
</LinearLayout>
<View

View File

@ -19,10 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res-auto"
android:id="@+id/list"
android:fastScrollEnabled="true"
android:divider="@null"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="outsideInset"
dslv:drag_enabled="false"
dslv:drag_start_mode="onMove"
dslv:drag_handle_id="@+id/dragger"/>

View File

@ -36,7 +36,8 @@ THE SOFTWARE.
android:layout_weight="1"
android:choiceMode="multipleChoice"
dslv:drag_enabled="true"
dslv:drag_start_mode="onMove" />
dslv:drag_start_mode="onMove"
dslv:drag_handle_id="@+id/dragger" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="fill_parent"

View File

@ -21,6 +21,8 @@ import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckedTextView;
import android.widget.Checkable;
import android.util.Log;
@ -28,16 +30,59 @@ import android.util.Log;
public class DraggableRow extends LinearLayout implements Checkable {
private boolean mShowCheckBox;
private boolean mChecked;
private float mDensity;
private static int sWidth = -1;
private TextView mTextView;
private CheckedTextView mCheckBox;
private View mPmark;
private ImageView mDragger;
// Hardcoded sizes of elements in DPI
// MUST match definition in draggable_row
private final int DPI_PMARK = 2;
private final int DPI_CHECKBOX = 44;
private final int DPI_DRAGGER = 44;
private final int DPI_SLACK = 2; // safety margin
public DraggableRow(Context context, AttributeSet attrs) {
super(context, attrs);
mDensity = context.getResources().getDisplayMetrics().density;
}
@Override
public void onFinishInflate() {
mCheckBox = (CheckedTextView)this.findViewById(R.id.checkbox);
mTextView = (TextView)this.findViewById(R.id.text);
mPmark = (View)this.findViewById(R.id.pmark);
mDragger = (ImageView)this.findViewById(R.id.dragger);
setupTextView(false);
}
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (sWidth != w) {
sWidth = w;
// This view was drawn with an incorrect with
// fix it and force a redraw
setupTextView(true);
}
}
/**
* Resizes our textview to the correct size
* @param redraw invalidates the current view if TRUE
*/
private void setupTextView(boolean redraw) {
int pixelUsed = (int)((DPI_SLACK + DPI_PMARK + DPI_DRAGGER + (mShowCheckBox ? DPI_CHECKBOX : 0)) * mDensity);
int pixelFree = sWidth - pixelUsed;
if (pixelFree > 0)
mTextView.setWidth(pixelFree);
if (redraw == true)
this.invalidate();
}
@Override
@ -56,16 +101,39 @@ public class DraggableRow extends LinearLayout implements Checkable {
setChecked(!mChecked);
}
/**
* Marks a row as highlighted
* @param state Enable or disable highlighting
*/
public void highlightRow(boolean state) {
mPmark.setVisibility( state ? View.VISIBLE : View.INVISIBLE );
}
/**
* Changes the visibility of the checkbox
* @param state controlls if the checkbox is shown or hidden
* @param state show or destroy the checkbox
*/
public void showCheckBox(boolean state) {
if (mShowCheckBox != state) {
mCheckBox.setVisibility( state ? View.VISIBLE : View.GONE);
mShowCheckBox = state;
setupTextView(true);
}
}
/**
* Change visibility of dragger element
* @param state shows or hides the dragger
*/
public void showDragger(boolean state) {
mDragger.setVisibility( state ? View.VISIBLE : View.INVISIBLE );
}
/**
* Returns an instance of our textview
*/
public TextView getTextView() {
return mTextView;
}
}

View File

@ -105,9 +105,9 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback {
@Override
public void bindView(View view, Context context, Cursor cursor)
{
View dragger = ((View)view.findViewById(R.id.dragger));
dragger.setVisibility( mEditable ? View.VISIBLE : View.INVISIBLE );
TextView textView = ((TextView)view.findViewById(R.id.text));
DraggableRow dview = (DraggableRow)view;
dview.showDragger(mEditable);
TextView textView = dview.getTextView();
textView.setText(cursor.getString(1));
textView.setTag(cursor.getLong(3));
}

View File

@ -26,7 +26,6 @@ import android.view.LayoutInflater;
import android.widget.TextView;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.Spannable;
@ -59,20 +58,18 @@ public class ShowQueueAdapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
View row = inflater.inflate(mResource, parent, false);
DraggableRow row = (DraggableRow)inflater.inflate(mResource, parent, false);
Song song = getItem(position);
if (song != null) { // unlikely to fail but seems to happen in the wild.
TextView target = ((TextView)row.findViewById(R.id.text));
SpannableStringBuilder sb = new SpannableStringBuilder(song.title);
sb.append('\n');
sb.append(song.album+", "+song.artist);
sb.setSpan(new ForegroundColorSpan(Color.GRAY), song.title.length() + 1, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
target.setText(sb);
row.getTextView().setText(sb);
}
View dragger = ((View)row.findViewById(R.id.dragger));
dragger.setVisibility( ( position == mHighlightRow ? View.VISIBLE : View.INVISIBLE ));
row.highlightRow(position == mHighlightRow);
return row;
}

View File

@ -93,7 +93,7 @@ public class TabOrderAdapter extends BaseAdapter {
} else {
view = (DraggableRow)convert;
}
((TextView)view.findViewById(R.id.text)).setText(LibraryPagerAdapter.TITLES[mTabIds[position]]);
view.getTextView().setText(LibraryPagerAdapter.TITLES[mTabIds[position]]);
view.showCheckBox(true);
return view;
}