所以我已经查看了很多其他答案,但未能让我的应用程序按我想要的方式工作。我基本上希望列表视图包含文本,check mark
到right
,但是左边是一个加法按钮。现在我的列表视图显示但检查图像永远不会改变。
编辑:在有用的评论后,我发现可以使用模拟器上的箭头(向上/向下)选择行,这会突出显示我想要的行。但是,当我单击该行时,它不会像我想要的那样被选中。此外,如果它有助于在对话框中使用列表视图。
选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/accept_on" />
<item
android:drawable="@drawable/accept" />
</selector>
行xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#EEE">
<ImageButton
android:id="@+id/goToMapButton"
android:src="@drawable/go_to_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1" />
<Button
android:id="@+id/checkButton"
android:background="@drawable/item_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
MapAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MapAdapter extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
String data[] = null;
LayoutInflater inflater;
LinearLayout layout;
public MapAdapter(Context context, int layoutResourceId, String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
@Override
public String getItem(int position) {
return data[position];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView == null)
{
convertView = inflater.inflate(R.layout.map_item_row, null);
layout = (LinearLayout)convertView.findViewById(R.id.layout);
holder.map = (ImageButton)convertView.findViewById(R.id.goToMapButton);
holder.name = (TextView)convertView.findViewById(R.id.itemName);
//holder.check = (Button)convertView.findViewById(R.id.checkButton);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
layout.setBackgroundColor(0x00000004);
holder.name.setText(getItem(position));
return convertView;
}
static class ViewHolder
{
ImageButton map;
TextView name;
Button check;
}
}
答案 0 :(得分:0)
尝试将选择器设置为背景而不是src
答案 1 :(得分:0)
试试这个
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
Planet planet = (Planet) this.getItem(position);
// The child views in each row.
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
// Find the child views.
textView = (TextView) convertView.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
// Optimization: Tag the row with it's child views, so we don't
// have to
// call findViewById() later when we reuse the row.
convertView.setTag(new PlanetViewHolder(textView, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call
// findViewById().
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag(planet);
// Display planet data
checkBox.setChecked(planet.isChecked());
textView.setText(planet.getName());
return convertView;
}
请参阅此示例http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php