java - 使用 Android ExpandableLayout 库

标签 java android xml

<分区>

我正在尝试在我的项目中使用 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>

最佳答案

为了让每一行都有不同的布局,你必须编写一个自定义 ListView 适配器并覆盖 getViewTypeCount()适配器中的方法并返回您拥有的 View 类型。

然后你还需要覆盖getItemViewType(int position)适配器中的方法并提供一个范围从 0..n-1 的值,其中 n 是从 getViewTypeCount() 返回的值以前。

最后你需要覆盖 getView(int position, View convertView, ViewGroup parent)根据类型,您可以决定给定职位所需的 View 。

里面有getView()膨胀具有不同 xml 标记实现的不同布局 ExpandableLayoutItem 实现

expandable:headerLayout="@layout/view_header"
expandable:contentLayout="@layout/view_content"

在大多数情况下,您应该熟悉膨胀 Multiple Layouts in list view其他的就简单了。

编辑

这里也是著名的answer我刚遇到。还给出了一个很好的例子here

很高兴!

关于java - 使用 Android ExpandableLayout 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27979208/

相关文章:

java - android.support.v4 库错误

java - 在单个 HashMap 中存储多种数据类型

xml - 如何对 XSLT/XPATH 中的一组节点进行不区分大小写的查找?

xml - 使用 Groovy 的 CSV 到 XML 转换器

java - DOCX4J:如何向 docx 模板中的现有表添加行?

java - 您能否将 Selenium WebElement 转换为字节数组或图像对象(使用 Java)?

java - 将 HTML 时间转换为 Java 时间对象

java - 限制 Firebase 数据库仅接受 Android 上的唯一电子邮件 ID

android - Visual Studio Android 模拟器没有互联网连接

c# - 使用 XmlSerializer 忽略外部元素