我使用 Bauerca 的 DragSortListView ( github.com/bauerca/drag-sort-listview ) 来显示带有两个按钮的频道列表。但是 TextView (textViewChannel) 没有填满可用空间!我究竟做错了什么??
频道列表.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res/com.froggie.xmonitor"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:text="@string/inputs"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.mobeta.android.dslv.DragSortListView
android:id="@+id/listShowChannels"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="3dp"
android:choiceMode="multipleChoice"
android:dividerHeight="1px"
android:padding="3dp"
dslv:collapsed_height="1px"
dslv:drag_enabled="true"
dslv:drag_handle_id="@+id/drag_handle"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onDown"
dslv:float_alpha="0.6"
dslv:remove_enabled="true"
dslv:remove_mode="clickRemove"
dslv:slide_shuffle_speed="0.3" />
</LinearLayout>
频道.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/channel_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="@+id/drag_handle"
android:layout_width="46dp"
android:layout_height="46dp"
android:src="@drawable/drag" />
<TextView
android:id="@+id/textViewChannel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="TextView" />
<ToggleButton
android:id="@+id/toggleButtonMute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/btn_mute_bg"
android:text="MUTE"
android:textOff="MUTE"
android:textOn="MUTE" />
<ToggleButton
android:id="@+id/toggleButtonHide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_mute_bg"
android:checked="false"
android:gravity="center_vertical|center_horizontal"
android:layout_weight="0"
android:text="HIDE"
android:textOff="HIDE"
android:textOn="HIDE" />
</LinearLayout>
频道列表.java:
public class ChannelList extends Activity {
DragSortListView channels;
ChannelAdapter adapter;
private DragSortListView.DropListener onDrop =
new DragSortListView.DropListener() {
@Override
public void drop(int from, int to) {
if (from != to) {
adapter.moveCheckState(from+1, to+1);
channels.invalidateViews();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.channel_list);
channels = (DragSortListView) getListView();
adapter = new ChannelAdapter(this, MainActivity.f);
channels.setAdapter(adapter);
channels.setDropListener(onDrop);
this.setTitle("Channel list");
}
protected DragSortListView getListView() {
// TODO Auto-generated method stub
return (DragSortListView) this.findViewById(R.id.listShowChannels);
}
}
class ChannelAdapter extends BaseAdapter implements OnClickListener {
Fader channel[];
Context c;
public ChannelAdapter(Context context, Fader[] f) {
channel = f;
c = context;
}
public void moveCheckState(int from, int to) {
// TODO Auto-generated method stub
Fader temp;
if (from < to) {
temp = channel[from];
for (int i=from; i<to; i++){
channel[i] = channel[i+1];
channel[i].invalidate();
}
channel[to] = temp;
channel[to].invalidate();
} else {
temp = channel[from];
for (int i=from; i>to; i--){
channel[i] = channel[i-1];
channel[i].invalidate();
}
channel[to] = temp;
channel[to].invalidate();
}
for (int i=1; i<33; i++){
MainActivity.order[Integer.parseInt(MainActivity.f[i].getChannelNumber())] = i;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return channel.length-1;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
String str = String.format("%02d: %s", position+1, channel[position+1].getChannelName());
return str;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void remove(Object item) {
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.channels, null);
}
TextView name = (TextView) v.findViewById(R.id.textViewChannel);
int chanNumber = Integer.parseInt(channel[position+1].getChannelNumber());
String str = String.format("%02d: %s", chanNumber, channel[position+1].getChannelName());
name.setText(str);
ToggleButton tbHide = (ToggleButton) v.findViewById(R.id.toggleButtonHide);
tbHide.setChecked(!channel[position+1].isVisible());
tbHide.setTag(new Integer(position+1));
tbHide.setOnClickListener(this);
ToggleButton tbMute = (ToggleButton) v.findViewById(R.id.toggleButtonMute);
tbMute.setChecked(channel[position+1].isMute());
tbMute.setTag(new Integer(position+1));
tbMute.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ToggleButton togg = (ToggleButton) v;
Integer position = (Integer) togg.getTag();
switch (v.getId()) {
case R.id.toggleButtonHide:
channel[position].setVisible(!togg.isChecked());
MainActivity.f[position].setVisible(!togg.isChecked());
break;
case R.id.toggleButtonMute:
channel[position].setMute(togg.isChecked());
MainActivity.f[position].setMute(togg.isChecked());
break;
}
}
}
我一定遗漏了一些明显的东西!