在自定义ListView下方添加内容可以通过以下步骤实现:
以下是一个示例代码:
布局文件(activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bottomContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下方内容" />
</LinearLayout>
适配器(CustomAdapter.java):
public class CustomAdapter extends BaseAdapter {
private List<String> data;
public CustomAdapter(List<String> data) {
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(data.get(position));
return convertView;
}
}
Activity中的代码:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView bottomContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
bottomContent = findViewById(R.id.bottomContent);
List<String> data = new ArrayList<>();
data.add("Item 1");
data.add("Item 2");
data.add("Item 3");
CustomAdapter adapter = new CustomAdapter(data);
listView.setAdapter(adapter);
}
}
这样,你就可以在自定义的ListView下方添加内容了。在示例中,我们使用了一个简单的TextView作为下方内容,你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云