gpt4 book ai didi

java - 使用 Android ExpandableLayout 库

转载 作者:行者123 更新时间:2023-11-30 11:09:53 25 4
gpt4 key购买 nike

<分区>

我正在尝试在我的项目中使用 android 可扩展列表

我有一个问题,我的 ListView 中的所有行都有相同的项目,即一个 Edittext 和一个按钮,但我想每一行都有特定的项目,主要问题是如何为我的 ListView 中的每一行设置特定内容?

如果需要,这里是代码:

主要 Activity .java:

package com.andexert.expandablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.andexert.expandablelayout.library.ExpandableLayoutListView;


public class MainActivity extends Activity {

private final String[] array = {"World", "Awesome", "Android", "is", "Awesome", "World", "Android", "is", "Awesome", "World", "Android", "is", "Awesome", "World", "Android", "is", "Awesome"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.view_row, R.id.header_text, array);
final ExpandableLayoutListView expandableLayoutListView = (ExpandableLayoutListView) findViewById(R.id.listview);

expandableLayoutListView.setAdapter(arrayAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:expandable="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<com.andexert.expandablelayout.library.ExpandableLayout
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e74c3c"
android:gravity="right"
expandable:contentLayout="@layout/view_content"
expandable:headerLayout="@layout/view_header" />

<com.andexert.expandablelayout.library.ExpandableLayoutListView
android:id="@+id/listview"
android:layout_below="@+id/first"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="match_parent"/>

</RelativeLayout>

view_content.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="match_parent">

<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:hint="Hello World !"
android:textColor="@android:color/white"
android:background="#c0392b"/>

<Button
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click ME !"/>

</RelativeLayout>

view_header.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="match_parent"
android:gravity="right" >

<TextView
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:padding="10dp"
android:textColor="@android:color/white"
android:text="ExpandableLayout Header!"
android:gravity="center"/>

</RelativeLayout>

view_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:expandable="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right" >

<com.andexert.expandablelayout.library.ExpandableLayoutItem
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e74c3c"
android:gravity="center_vertical"
expandable:contentLayout="@layout/view_content"
expandable:headerLayout="@layout/view_header" />


</RelativeLayout>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com