Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android小部件上实现列表_Android_Listview_Widget - Fatal编程技术网

在Android小部件上实现列表

在Android小部件上实现列表,android,listview,widget,Android,Listview,Widget,我需要在Android小部件上实现可滚动列表功能。我知道小部件不支持ListView,我曾想过在滚动视图中使用一组按钮,但我不知道如何实现适配器的“回收”功能(使用屏幕上显示的尽可能多的按钮,并在滚动时“回收”它们,刷新按钮上显示的数据)。有人能帮我吗?提前感谢据我所知,您希望实现自定义列表适配器: public class YourAdapterName extends BaseAdapter{ private Context mContext; private Vector mValues

我需要在Android小部件上实现可滚动列表功能。我知道小部件不支持ListView,我曾想过在滚动视图中使用一组按钮,但我不知道如何实现适配器的“回收”功能(使用屏幕上显示的尽可能多的按钮,并在滚动时“回收”它们,刷新按钮上显示的数据)。有人能帮我吗?提前感谢

据我所知,您希望实现自定义列表适配器:

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

}
公共类YourAdapterName扩展BaseAdapter{
私有上下文;
私人向量机;
/**
*要调用的构造函数以使用值初始化适配器。
*@param上下文
*@param向量
*/
公共YourAdapterName(上下文,向量){
mContext=上下文;
mValuestoShow=向量;
}
public int getCount(){
if(null!=mValuestoShow){
返回mValuestoShow.size();
}
返回0;
}
公共对象getItem(int位置){
if(位置
您的_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>

注:您可以根据需要添加更多视图,如按钮、图像按钮、编辑文本