我想在列表视图中为每个应用程序获取已安装应用程序的列表,其中有两个toggleButton是第一个要添加的,第二个是要保护(如下图所示)。我想第一次在列表后添加一个按钮,试图使用没有结果的滚动视图。大列表的问题我找不到我的按钮。
我的两个布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal"
>
<TextView
android:id="@+id/maTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="180dip"
android:gravity="center_vertical|center_horizontal"
android:text="Ajouter Bloquer" />
</LinearLayout>
<ListView
android:id="@+id/lv_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linear"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
tools:context=".MainActivity" />
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/lv_countries"
android:orientation="horizontal"
>
<Button
android:id="@+id/button25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider" >
</Button>
</LinearLayout>
</RelativeLayout>
和:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight = "160dip">
<ToggleButton
android:id="@+id/tgl_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:layout_marginRight="80dip" />
<ToggleButton
android:id="@+id/tgl_status1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false" />
</LinearLayout>
</RelativeLayout>
我想第二次添加带有应用程序名称的图标。所以我试着更换我的适配器
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.lv_layout, from, to);
我尝试创建一个适配器,使用名称为我的适配器的应用程序图标:
ublic class AppListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<App> mApps;
/** a map which maps the package name of an app to its icon drawable */
private Map<String, Drawable> mIcons;
private Drawable mStdImg;
/**
* Constructor.
*
* @param context the application context which is needed for the layout inflater
*/
public AppListAdapter(Context context, List<HashMap<String, Object>> aList, int r, String[] from, int[] to) {
// cache the LayoutInflater to avoid asking for a new one each time
mInflater = LayoutInflater.from(context);
// set the default icon until the actual icon is loaded for an app
mStdImg = context.getResources().getDrawable(R.drawable.icon);
}
@Override
public int getCount() {
return mApps.size();
}
@Override
public Object getItem(int position) {
return mApps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
// creates a ViewHolder and stores a reference to the children view we want to bind data to
holder = new AppViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.apptitle);
holder.mIcon = (ImageView) convertView.findViewById(R.id.appicon);
convertView.setTag(holder);
} else {
// reuse/overwrite the view passed assuming(!) that it is castable!
holder = (AppViewHolder) convertView.getTag();
}
App app = mApps.get(position);
holder.setTitle(app.getTitle());
if (mIcons == null || mIcons.get(app.getPackageName()) == null) {
holder.setIcon(mStdImg);
} else {
holder.setIcon(mIcons.get(app.getPackageName()));
}
return convertView;
}
/**
* Sets the list of apps to be displayed.
*
* @param list the list of apps to be displayed
*/
public void setListItems(List<App> list) {
mApps = list;
}
/**
* Sets the map containing the icons for each displayed app.
*
* @param icons the map which maps the app's package name to its icon
*/
public void setIcons(Map<String, Drawable> icons) {
this.mIcons = icons;
}
/**
* Returns the map containing the icons for each displayed app.
*
* @return a map which contains a mapping of package names to icon drawable for all displayed apps
*/
public Map<String, Drawable> getIcons() {
return mIcons;
}
/**
* A view holder which is used to re/use views inside a list.
*/
public class AppViewHolder {
private TextView mTitle;
private ImageView mIcon;
/**
* Sets the text to be shown as the app's title
*
* @param title the text to be shown inside the list row
*/
public void setTitle(String title) {
mTitle.setText(title);
}
/**
* Sets the icon to be shown next to the app's title
*
* @param img the icon drawable to be displayed
*/
public void setIcon(Drawable img) {
if (img != null) {
mIcon.setImageDrawable(img);
}
}
}
}
我用它来称呼它:
mAdapter = new AppListAdapter(getApplicationContext(), aList, R.layout.lv_layout, from, to);
mAdapter.setListItems(mApps);
答案 0 :(得分:1)
只需将页脚添加到列表视图
即可View footer = getLayoutInflater().inflate(R.layout.footer, null);
页脚视图可以是按钮
listView.addFooterView(footer);
答案 1 :(得分:0)
将您的父布局设为ScrollView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="318dp"
android:layout_height="495dp"
>
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal"
>
<TextView
android:id="@+id/maTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="180dip"
android:gravity="center_vertical|center_horizontal"
android:text="Ajouter Bloquer" />
</LinearLayout>
<ListView
android:id="@+id/lv_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linear"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
tools:context=".MainActivity" />
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/lv_countries"
android:orientation="horizontal"
>
<Button
android:id="@+id/button25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider" >
</Button>
</LinearLayout>
</RelativeLayout>