Android 列表视图底部的按钮
Buttons at the bottom of the list view in Android
我正在开发 Android 应用程序,我想在其中修复列表底部的按钮,所以如果列表被刷新并且更多项目添加到列表中,那么按钮应该仍然存在在最后一个。
我使用了下面的代码,但问题是它显示的按钮像这个图像。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/newlistselector"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/newlistselector"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.ziqitza.views.CircularImageView
android:id="@+id/imgNotificationProfile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="3dp"
android:padding="5dp"
android:scaleType="fitXY"
android:src="@drawable/profile_" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgSearchContact"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ll_indicator"
android:src="@drawable/arrowright" />
<LinearLayout
android:id="@+id/ll_indicator"
android:layout_width="4dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/roundcorner_topright_downright"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="@+id/txtNotificationDesc"
android:layout_width="210dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:maxLines="2"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:textSize="12sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status: "
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000"
android:textSize="9sp" />
<TextView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/imgSearchContact"
android:layout_marginRight="10dp"
android:gravity="center_vertical|right"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtNotificationTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:text="time"
android:textColor="#000"
android:textSize="8sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
适配器和活页夹代码:
private AdapterDataBinder<ParseObject> mNotificationsBinder = new AdapterDataBinder<ParseObject>() {
@Override
public void bind(ParseObject data, View view, int position) {
if (isLoadingFirstTime) {
mHolder.mTxtDesciption = (TextView) view.findViewById(R.id.txtNotificationDesc);
mHolder.mTxtTime = (TextView) view.findViewById(R.id.txtNotificationTime);
mHolder.mImageProfile = (CircularImageView) view.findViewById(R.id.imgNotificationProfile);
mHolder.mll_State = (LinearLayout) view.findViewById(R.id.ll_indicator);
mHolder.mTxtDesciption.setText(data.getString("description"));
mHolder.txtStatus = (TextView) view.findViewById(R.id.txtStatus);
// mHolder.btnRequest = (Button) view.findViewById(R.id.button1);
mHolder.txtStatus.setVisibility(View.VISIBLE);
if (!data.getString("type").equals("TrustedNetwork")) {
showStatus(data);
} else
mHolder.txtStatus.setVisibility(View.GONE);
mHolder.mTxtTime.setText(mSDF.format(data.getCreatedAt()));
try {
if (data.getBoolean("seen")) {
mHolder.mll_State.setVisibility(View.INVISIBLE);
} else
mHolder.mll_State.setVisibility(View.VISIBLE);
ParseFile userimage = data.getParseObject("sender").getParseFile("profileImage");
if (userimage == null) {
Utils.loadProfileImage(NotificationsActivity.this, null, mHolder.mImageProfile);
} else
Utils.loadProfileImage(NotificationsActivity.this, null, mHolder.mImageProfile);
Picasso.with(view.getContext()).load(userimage.getUrl()).into(mHolder.mImageProfile);
} catch (Exception ex) {
ex.printStackTrace();
}
view.setTag(mHolder);
}else{
mHolder = (ViewHolder) view.getTag();
}
}
@Override
public void bind(ParseObject data, View view) {
}
};
持有人Class:
ViewHolder mHolder = new ViewHolder();
static class ViewHolder{
private TextView mTxtDesciption, mTxtTime;
private CircularImageView mImageProfile;
Button btn;// = (Button) findView(R.id.refreshBtn);
private LinearLayout mll_State;
TextView txtStatus;
Button btnRequest;
}
这样做
main_list.xml
<?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="wrap_content" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linTop"
android:layout_marginBottom="10dp" >
</ListView>
<LinearLayout
android:id="@+id/linTop"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Done"
android:textSize="15sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel"
android:textSize="15sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Select All"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
如果你想把按钮放在列表项的底部,那么你可以使用ListView
的addFooterView()
方法。例如,这将适用于在列表页脚中添加的一个按钮。
Button btn = new Button(this);
btn.setText("Done");
ListView lst = (ListView) findViewById(R.id.yourlist);
lst.addFooterView(btn);
// * your view containing the 3 buttons *//
这会将包含您在图像中提到的 3 个按钮的视图放在 List
中最后一项之后。
继续我的评论..
- 添加页脚 -
的回答
count
解决方法
- 使用
BaseAdapter
膨胀ListView
中的数据
更新 getCount()
函数以发送回增加的长度。
@Override
public int getCount() {
return data.length + 1;
}
现在,更新 getView()
方法以根据位置膨胀不同的视图。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position != getCount() - 1)
convertView= layoutInflater.inflate(R.layout.item, null);
else
convertView= layoutInflater.inflate(R.layout.button, null);
return convertView;
}
- 不要忘记在此代码中添加
ViewHolder
模式。
要使用 AdapterDataBinder
,我不知道这里的最大计数是如何决定的,所以你必须自己寻找在 getCount()
中完成的调整,但对于代码从 getView()
方法应该放在 onBind()
里面
if (isLoadingFirstTime || position == last - 1) {
if (position == last - 1) {
// Inflate the buttons layout
} else {
// Do as you already did...
}
}
最后我得到了解决方案,我现在在 LayoutInflater
中传递我的 ViewGroup
,其中包含我的页脚布局,然后在我的 ViewGroup
中传递我的按钮最后,我在列表视图中添加了页脚视图。
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer_view, mListView, false);
mListView.addFooterView(footer, null, false);
mListView.setAdapter(mAdapter);
final Button footerRefreshBtn = (Button) footer.findViewById(R.id.footerRefreshBtn);
footerRefreshBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
footerRefreshBtn.setVisibility(View.GONE);
populateNotifications(10);
}
});
我正在开发 Android 应用程序,我想在其中修复列表底部的按钮,所以如果列表被刷新并且更多项目添加到列表中,那么按钮应该仍然存在在最后一个。
我使用了下面的代码,但问题是它显示的按钮像这个图像。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/newlistselector"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/newlistselector"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal" >
<com.ziqitza.views.CircularImageView
android:id="@+id/imgNotificationProfile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="3dp"
android:padding="5dp"
android:scaleType="fitXY"
android:src="@drawable/profile_" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgSearchContact"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ll_indicator"
android:src="@drawable/arrowright" />
<LinearLayout
android:id="@+id/ll_indicator"
android:layout_width="4dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/roundcorner_topright_downright"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="@+id/txtNotificationDesc"
android:layout_width="210dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:maxLines="2"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000"
android:textSize="12sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status: "
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000"
android:textSize="9sp" />
<TextView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/imgSearchContact"
android:layout_marginRight="10dp"
android:gravity="center_vertical|right"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtNotificationTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:text="time"
android:textColor="#000"
android:textSize="8sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
适配器和活页夹代码:
private AdapterDataBinder<ParseObject> mNotificationsBinder = new AdapterDataBinder<ParseObject>() {
@Override
public void bind(ParseObject data, View view, int position) {
if (isLoadingFirstTime) {
mHolder.mTxtDesciption = (TextView) view.findViewById(R.id.txtNotificationDesc);
mHolder.mTxtTime = (TextView) view.findViewById(R.id.txtNotificationTime);
mHolder.mImageProfile = (CircularImageView) view.findViewById(R.id.imgNotificationProfile);
mHolder.mll_State = (LinearLayout) view.findViewById(R.id.ll_indicator);
mHolder.mTxtDesciption.setText(data.getString("description"));
mHolder.txtStatus = (TextView) view.findViewById(R.id.txtStatus);
// mHolder.btnRequest = (Button) view.findViewById(R.id.button1);
mHolder.txtStatus.setVisibility(View.VISIBLE);
if (!data.getString("type").equals("TrustedNetwork")) {
showStatus(data);
} else
mHolder.txtStatus.setVisibility(View.GONE);
mHolder.mTxtTime.setText(mSDF.format(data.getCreatedAt()));
try {
if (data.getBoolean("seen")) {
mHolder.mll_State.setVisibility(View.INVISIBLE);
} else
mHolder.mll_State.setVisibility(View.VISIBLE);
ParseFile userimage = data.getParseObject("sender").getParseFile("profileImage");
if (userimage == null) {
Utils.loadProfileImage(NotificationsActivity.this, null, mHolder.mImageProfile);
} else
Utils.loadProfileImage(NotificationsActivity.this, null, mHolder.mImageProfile);
Picasso.with(view.getContext()).load(userimage.getUrl()).into(mHolder.mImageProfile);
} catch (Exception ex) {
ex.printStackTrace();
}
view.setTag(mHolder);
}else{
mHolder = (ViewHolder) view.getTag();
}
}
@Override
public void bind(ParseObject data, View view) {
}
};
持有人Class:
ViewHolder mHolder = new ViewHolder();
static class ViewHolder{
private TextView mTxtDesciption, mTxtTime;
private CircularImageView mImageProfile;
Button btn;// = (Button) findView(R.id.refreshBtn);
private LinearLayout mll_State;
TextView txtStatus;
Button btnRequest;
}
这样做
main_list.xml
<?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="wrap_content" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linTop"
android:layout_marginBottom="10dp" >
</ListView>
<LinearLayout
android:id="@+id/linTop"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Done"
android:textSize="15sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel"
android:textSize="15sp" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Select All"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
如果你想把按钮放在列表项的底部,那么你可以使用ListView
的addFooterView()
方法。例如,这将适用于在列表页脚中添加的一个按钮。
Button btn = new Button(this);
btn.setText("Done");
ListView lst = (ListView) findViewById(R.id.yourlist);
lst.addFooterView(btn);
// * your view containing the 3 buttons *//
这会将包含您在图像中提到的 3 个按钮的视图放在 List
中最后一项之后。
继续我的评论..
- 添加页脚 -
count
解决方法- 使用
BaseAdapter
膨胀ListView
中的数据
更新
getCount()
函数以发送回增加的长度。@Override public int getCount() { return data.length + 1; }
现在,更新
getView()
方法以根据位置膨胀不同的视图。@Override public View getView(int position, View convertView, ViewGroup parent) { if(position != getCount() - 1) convertView= layoutInflater.inflate(R.layout.item, null); else convertView= layoutInflater.inflate(R.layout.button, null); return convertView; }
- 不要忘记在此代码中添加
ViewHolder
模式。
- 使用
要使用 AdapterDataBinder
,我不知道这里的最大计数是如何决定的,所以你必须自己寻找在 getCount()
中完成的调整,但对于代码从 getView()
方法应该放在 onBind()
if (isLoadingFirstTime || position == last - 1) {
if (position == last - 1) {
// Inflate the buttons layout
} else {
// Do as you already did...
}
}
最后我得到了解决方案,我现在在 LayoutInflater
中传递我的 ViewGroup
,其中包含我的页脚布局,然后在我的 ViewGroup
中传递我的按钮最后,我在列表视图中添加了页脚视图。
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer_view, mListView, false);
mListView.addFooterView(footer, null, false);
mListView.setAdapter(mAdapter);
final Button footerRefreshBtn = (Button) footer.findViewById(R.id.footerRefreshBtn);
footerRefreshBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
footerRefreshBtn.setVisibility(View.GONE);
populateNotifications(10);
}
});