2

im Trying to use android Expandable List in My Project

I have a problem, All Rows in My ListView Have the same Items, which is a Edittext and an button, But I want to have specific Items for each row, the main problem is how to set a specific content for each row in my list view?

here is the Codes if its needed:

MainActivity.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>
Alireza Pir
  • 878
  • 1
  • 16
  • 41

1 Answers1

1

for having different layout for each row you have to write a Custom Listview Adapter and override getViewTypeCount() method in adapter and return how many types of view you are having.

Then you also need to override getItemViewType(int position) method in adapter and provide a value ranging from 0..n-1 where n is value returned from getViewTypeCount() previously.

Then finally you need to override getView(int position, View convertView, ViewGroup parent) and based on type you can decide which view you need for a given position.

There inside getView() inflate different layouts having different implementation of xml tag ExpandableLayoutItem having their implementation for

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

for the most part you should be familiar with inflating Multiple Layouts in list view other things are simple.

EDIT

Here is also a famous answer I have just came across . A good example is also given here

Njoy!

Community
  • 1
  • 1
Shubhang Malviya
  • 1,525
  • 11
  • 17