2011-08-25 64 views
0

我需要在Android小部件上实现可滚动列表功能。我知道小部件不支持ListView,我想在Scroll View中使用一组按钮,但我不知道如何实现适配器的“回收”功能(使用尽可能多的按钮,可以显示在屏幕并在滚动时“回收”它们,刷新按钮上显示的数据)。谁能帮我这个?谢谢你在前进在Android小部件上实现列表

回答

0

我的理解是,要实现自定义列表适配器:

public class YourAdapterName extends BaseAdapter{ 

private Context mContext; 
private Vector mValuestoShow; 

/** 
* Constructor to be called to initialize adapter with values. 
* @param context 
* @param vector 
*/ 
public YourAdapterName(Context context, Vector vector){ 
    mContext = context; 
    mValuestoShow = vector; 
} 

public int getCount() { 
    if(null != mValuestoShow){ 
     return mValuestoShow.size(); 
    } 
    return 0; 
} 

public Object getItem(int position) { 
    if(position < mValuestoShow.size()) 
     return mValuestoShow.get(position); 
    else 
     return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

/** 
* This method can be override to enable/disable particular list row. 
*/ 
@Override 
public boolean isEnabled(int position) { 
    //Write your code here...... 
    return super.isEnabled(position); 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder ; 
    if(convertView == null){ 
     LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = li.inflate(R.layout.your_layout, null); 
     holder = new ViewHolder(); 
     holder.txt_name = (TextView) convertView.findViewById(R.id.name_txt); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    /** 
* Use of enable method how to set different color for disabled row.... 
* You can also customize your row background color or text color without using enable method 
* in the same way as below is done as per your conditions. 
*/ 
    if(!isEnabled(position)){ 
     holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.color_code)); 
     holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.white)); 
    }else{ 
     holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.color_code)); 
     holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.black)); 
    } 

    holder.txt_name.setText(getItem(position).toString()); 

    return convertView; 
} 

class ViewHolder { 
    TextView txt_name; 
} 

} 

your_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" 
    android:padding = "10dp" > 

<TextView 
    android:id = "@+id/txt_type1" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" /> 

<TextView 
    android:id = "@+id/txt_type2" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" /> 

<Button 
    android:id = "@+id/btn" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" /> 

</RelativeLayout> 

注意:您可以添加更多的景色像Button,ImageButton的, EditText根据您的需要。