0

我有一个列表视图,它由数据库中的信息填充,使用自定义适配器来处理各个视图。我想要做的是禁用该列表视图中的某些项目(它是一个基本的进度系统,即用户完成模块 1,然后模块 2 被解锁)。

我已经看到这里和那里提到覆盖我的适配器类中的areAllItemsEnabled()isEnabled()方法。我遇到的问题是,areAllItemsEnabled()似乎没问题,但isEnabled()方法基本上启用/禁用整个列表视图,而不是单个项目。任何帮助表示赞赏,在此先感谢。

适配器类:

public class ModuleAdapter extends ArrayAdapter<Module> {

Context context;
int layoutResourceId;
Module data[];

public ModuleAdapter(Context context, int layoutResourceId,
        Module data[]) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ModuleHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ModuleHolder();
        holder.modJapTitle = (TextView)row.findViewById(R.id.moduleJapTitle);
        holder.modEngTitle = (TextView)row.findViewById(R.id.moduleEngTitle);
        holder.modComp = (TextView)row.findViewById(R.id.moduleCompletion);
        holder.modRating = (RatingBar)row.findViewById(R.id.moduleScore);

        row.setTag(holder);
    }
    else
    {
        holder = (ModuleHolder)row.getTag();
    }

    Module module = data[position];
    holder.modJapTitle.setText(module.moduleJapaneseTitle);
    holder.modEngTitle.setText(module.moduleEnglishTitle);
    holder.modComp.setText(module.moduleCompletionRate);
    holder.modRating.setRating(module.moduleRating);

    return row;
}

static class ModuleHolder
{
    TextView modEngTitle;
    TextView modJapTitle;
    TextView modComp;
    RatingBar modRating;
}


@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return false;
}

}

活动实施:

ModuleAdapter moduleData = new ModuleAdapter(this, R.layout.module_box_item, module_data);
    moduleData.areAllItemsEnabled();
    moduleData.isEnabled(0);
    ListView listView1 = (ListView)findViewById(R.id.moduleListContainer);
    listView1.setClickable(true);
    listView1.setAdapter(moduleData);
    listView1.setCacheColorHint(Color.TRANSPARENT);
    listView1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int temp = position + 1;
            Intent i = new Intent(Story.this, SkitSelect.class);
            i.putExtra("moduleToGet", temp);
            startActivity(i);
        }
    });

单个项目的视图描述:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/moduleBox"
android:id="@+id/moduleBoxSingle"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" >

<TextView
    android:id="@+id/moduleJapTitle"
    style="@style/moduleTitleJap" />
<TextView
    android:id="@+id/moduleCompletion"
    android:layout_above="@+id/moduleSepartor"
    style="@style/moduleCompletion" />
<View
    android:id="@+id/moduleSepartor"
    android:layout_below="@+id/moduleJapTitle"
    style="@style/moduleSeperator" />
<TextView
    android:id="@+id/moduleEngTitle"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleTitleEng" />
<RatingBar
    android:id="@+id/moduleScore"
    android:layout_below="@+id/moduleSepartor"
    style="@style/moduleRating"
    android:layout_alignParentRight="true" />

</RelativeLayout>
4

2 回答 2

1

Ok I figured this out. The method was as simple as this:

public boolean isEnabled(int position) {

    if (position == 2) {
        return false
    }

    return true;  
}

This has to be set in the custom adapter. Not called from the implementation in the activity. This was the problem since I was trying to do something like mycustomadapter.isEnabled(2) which is incorrect.

Hope this helps someone else.

于 2012-07-20T16:24:36.573 回答
0
but the isEnabled() method basically enables/disables the entire listview, not the individual item.

它将给出当前行的状态。

    public boolean isEnabled(int position) {
    return super.isEnabled(position);
};

但你想做什么?您是否通过此启用/禁用

(adapter.getView(position, convertView, parent)).setEnabled(false);
于 2012-07-15T06:38:20.387 回答