I would like to know if it is possible to open a new Activity by clicking on a child element of a ExpandableListView
, keeping in mind that for each child item element it opens a specific Activity?
My layout consists of a Tablayout with Expandable List so I would like to know how to do in this layout scheme where you use fragments.
Follow my project
Java Code = Package / Screens / Adapter
Adapter class
package com.marcos.programador.projetobus.Telas;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.marcos.programador.projetobus.Cursos;
import com.marcos.programador.projetobus.R;
import java.util.HashMap;
import java.util.List;
/**
* Created by Marcos on 23/02/2017.
*/
class Adaptador extends BaseExpandableListAdapter {
private List<String> listaGrupos;
private HashMap<String, List<Cursos>> listaItensGrupos;
private Context context;
public Adaptador(Context context, List<String> grupos, HashMap<String, List<Cursos>> itensGrupos) {
this.context = context;
listaGrupos = grupos;
listaItensGrupos = itensGrupos;
}
@Override
public int getGroupCount() {
return listaGrupos.size();
}
@Override
public int getChildrenCount(int grupoPosicao)
{
return listaItensGrupos.get(getGroup(grupoPosicao)).size();
}
@Override
public Object getGroup(int grupoPsicao)
{
return listaGrupos.get(grupoPsicao);
}
@Override
public Object getChild(int grupoPosicao, int inicioPosicao)
{
return listaItensGrupos.get(getGroup(grupoPosicao)).get(inicioPosicao);
}
@Override
public long getGroupId(int grupoPosicao)
{
return grupoPosicao;
}
@Override
public long getChildId(int grupoPosicao, int inicioPosicao)
{
return inicioPosicao;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public View getGroupView(int grupoPosicao, boolean isExpanded, View converteVisualizacao, ViewGroup parent) {
if(converteVisualizacao == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
converteVisualizacao = layoutInflater.inflate(R.layout.grupo, null);
}
TextView tvGrupo = (TextView) converteVisualizacao.findViewById(R.id.tvGrupo);
TextView tvQtde = (TextView) converteVisualizacao.findViewById(R.id.tvQtde);
tvGrupo.setText((String)getGroup(grupoPosicao));
tvQtde.setText(String.valueOf(getChildrenCount(grupoPosicao)));
return converteVisualizacao;
}
@Override
public View getChildView(int grupoPosicao, int inicioPosicao, boolean finalPosicao, View converteVisualizacao, ViewGroup parent) {
if(converteVisualizacao == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
converteVisualizacao = layoutInflater.inflate(R.layout.item_grupo,null);
}
TextView tvItem = (TextView) converteVisualizacao.findViewById(R.id.tvItem);
TextView tvValor = (TextView) converteVisualizacao.findViewById(R.id.tvValor);
Cursos cursos = (Cursos) getChild(grupoPosicao,inicioPosicao);
tvItem.setText(cursos.getNome());
tvValor.setText(String.valueOf(cursos.getValor()));
return converteVisualizacao;
}
@Override
public boolean isChildSelectable(int grupoPosicao, int inicioPosicao)
{
return false;
}
}
Data Class
package com.marcos.programador.projetobus.Telas;
import com.marcos.programador.projetobus.Cursos;
import com.marcos.programador.projetobus.MainActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Marcos on 23/02/2017.
*/
public class Dados extends MainActivity {
static List<String> listaGrupos = new ArrayList<String>();
static List<Cursos> listaItem1 = new ArrayList<Cursos>();
static List<Cursos> listaItem2 = new ArrayList<Cursos>();
static List<Cursos> listaItem3 = new ArrayList<Cursos>();
static HashMap<String, List<Cursos>> listaItensGrupo = new HashMap<>();
public static List<String> carregaGrupo(String nome){
listaGrupos.add(nome);
return listaGrupos;
}
public static List<Cursos> carregaItem1(String nome, Double numero){
listaItem1.add(new Cursos(nome, numero));
return listaItem1;
}
public static List<Cursos> carregaItem2(String nome, Double numero){
listaItem2.add(new Cursos(nome, numero));
return listaItem2;
}
public static List<Cursos> carregaItem3(String nome, Double numero){
listaItem3.add(new Cursos(nome, numero));
return listaItem3;
}
public static HashMap<String, List<Cursos>> carregaRelacionamentos(){
listaItensGrupo.put(listaGrupos.get(0), listaItem1);
listaItensGrupo.put(listaGrupos.get(1), listaItem2);
listaItensGrupo.put(listaGrupos.get(2), listaItem3);
return listaItensGrupo;
}
}
Screen class2
package com.marcos.programador.projetobus.Telas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import com.marcos.programador.projetobus.R;
/**
* Created by Marcos on 28/02/2017.
*/
public class tela2 extends android.support.v4.app.Fragment {
ExpandableListView lista;
ArrayAdapter<String> adapter;
public static tela2 novaInstancia() {return new tela2();}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View tela=inflater.inflate(R.layout.tela2,null);
inicializaObjetos(tela);
carregaLista();
return tela;
}
// Inicializando Objetos
private void inicializaObjetos(View tela)
{
lista=(ExpandableListView) tela.findViewById(R.id.listaCursos);
}
//Carrega Lista
private void carregaLista()
{
//Criando os grupos
Dados.carregaGrupo("Android Studio");
Dados.carregaGrupo("Java Netbeans");
Dados.carregaGrupo("PHP");
//Criando os itens do grupo Android Studio
Dados.carregaItem1("Manhã", 0.0);
Dados.carregaItem1("Tarde", 0.0);
Dados.carregaItem1("Noite", 0.0);
//Criando os itens do grupo Java Netbeans
Dados.carregaItem2("Manhã", 0.0);
Dados.carregaItem2("Tarde", 0.0);
Dados.carregaItem2("Noite", 0.0);
//Criando os itens do grupo PHP
Dados.carregaItem3("Manhã", 0.0);
Dados.carregaItem3("Tarde", 0.0);
Dados.carregaItem3("Noite", 0.0);
//Criando Relacionamento dos grupos com seus itens
Dados.carregaRelacionamentos();
// Criando um adaptador com os dados acima
Adaptador adaptador = new Adaptador(getActivity(), Dados.listaGrupos, Dados.listaItensGrupo);
//adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, armazena);
//Definindo o adaptador
lista.setAdapter(adaptador);
}
@Override
public String toString(){ return "Horarios";}
}
MainActivity class
package com.marcos.programador.projetobus;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import com.marcos.programador.projetobus.Adaptador.QuadroAdaptador;
import com.marcos.programador.projetobus.Telas.tela1;
import com.marcos.programador.projetobus.Telas.tela2;
import com.marcos.programador.projetobus.Telas.tela3;
public class MainActivity extends AppCompatActivity implements
TabLayout.OnTabSelectedListener {
private TabLayout Guias;
private ViewPager abreTela;
int atualPosicao=0;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.Ferramenta);
setSupportActionBar(toolbar);
//Abre páginas da guia
abreTela = (ViewPager) findViewById(R.id.Paginas);
carregaPaginas();
//Configuração Guia
Guias = (TabLayout) findViewById(R.id.Guias);
Guias.setTabGravity(TabLayout.GRAVITY_FILL);
Guias.setupWithViewPager(abreTela);
Guias.addOnTabSelectedListener(this);
}
//Preencher páginas
private void carregaPaginas()
{
QuadroAdaptador quadro=new QuadroAdaptador(getSupportFragmentManager());
quadro.addPage(tela1.novaInstancia());
quadro.addPage(tela2.novaInstancia());
quadro.addPage(tela3.novaInstancia());
abreTela.setAdapter(quadro);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
abreTela.setCurrentItem(atualPosicao=tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.marcos.programador.projetobus.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:id="@+id/Ferramenta">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/Guias"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/Paginas"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
grupo.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.marcos.programador.projetobus.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:id="@+id/Ferramenta">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/Guias"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/Paginas"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
item_group.xml
<?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:background="@color/cardview_shadow_start_color"
android:orientation="horizontal">
<TextView
android:id="@+id/tvItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="7"
android:text="Item"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tvValor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_weight="3"
android:text="Valor"
android:textColor="@android:color/black" />
</LinearLayout>
screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/listaCursos"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>