繁体   English   中英

在Android小部件上实现列表

[英]Implementing a List on an Android widget

我需要在Android小部件上实现可滚动列表功能。 我知道小部件不支持ListView,我想过在Scroll View中使用set Buttons,但是我不知道如何实现Adapter“回收”功能(要使用的按钮数量尽可能多,屏幕并滚动“回收”它们,刷新按钮上显示的数据)。 谁能帮我这个? 先感谢您

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

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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM