Disabling and changing style of individual listview items with custom adapter in Android

不想你离开。 提交于 2019-12-25 05:24:09

问题


I have a listview that is being populated by information from a database using a custom adapter to handle the individual views. What I am trying to do is disable certain items in that listview (it is a basic progression system, i.e. user finishes module 1, and then module 2 is unlocked).

I have seen mentions here and there to override areAllItemsEnabled() and isEnabled() methods in my adapter class. The problem I'm having is, areAllItemsEnabled() seems ok, but the isEnabled() method basically enables/disables the entire listview, not the individual item. Any help is appreciated, thanks in advance.

Adapter Class:

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;
}

}

Implementation in activity:

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);
        }
    });

The view description for the individual items:

<?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>

回答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.




回答2:


but the isEnabled() method basically enables/disables the entire listview, not the individual item.

It will gives the status of the current row.

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

but what your trying to do? are you enables/disable by this

(adapter.getView(position, convertView, parent)).setEnabled(false);


来源:https://stackoverflow.com/questions/11489573/disabling-and-changing-style-of-individual-listview-items-with-custom-adapter-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!