2016-10-13 6 views
0

클릭하면 다른 활동으로 이동하기 위해 코드를 추가 할 수 있습니까?Expandablelistview에서 항목을 클릭하면 다른 활동으로 이동할 수 있습니까?

side_screen.java 
package com.example.asus.myapplication; 

import android.app.SearchManager; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.ExploreByTouchHelper; 
import android.support.v7.view.menu.MenuView; 
import android.support.v7.widget.SearchView; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.AdapterView; 
import android.widget.ExpandableListView; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class side_screen extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
    HashMap<String, List<String>> Computer_category; 
    List<String> Computer_list; 
    ExpandableListView Exp_list; 
    ComputerAdapter adapter; 

    int photo_num; 
    ImageView imageView; 
    private GoogleApiClient client; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle("meun"); 
     toolbar.setSubtitle("courses"); 

     setSupportActionBar(toolbar); 

     Exp_list = (ExpandableListView) findViewById(R.id.exp_list); 
     Computer_category = DataProvider.getInfo(); 
     Computer_list = new ArrayList<String>(Computer_category.keySet()); 
     adapter = new ComputerAdapter(this, Computer_category, Computer_list); 
     Exp_list.setAdapter(adapter); 

     Exp_list.setOnItemClickListener(new ExpandableListView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      } 
     }); 
     Intent searchintent = getIntent(); 
     if (Intent.ACTION_SEARCH.equals(searchintent.getAction())) { 
      String query = searchintent.getStringExtra(SearchManager.QUERY); 

      Toast.makeText(side_screen.this, query, Toast.LENGTH_SHORT).show(); 
     } 

     imageView = (ImageView) findViewById(R.id.image_show); 
     final ImageButton but_next = (ImageButton) findViewById(R.id.next); 
     final ImageButton but_back = (ImageButton) findViewById(R.id.back); 
     but_next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (photo_num < 4) { 
        photo_num++; 
        imageView.setImageResource(getResources().getIdentifier("bri_".concat(String.valueOf(photo_num)), "drawable", getPackageName())); 
        but_back.setEnabled(true); 
       } else { 
        but_next.setEnabled(false); 
       } 
      } 
     }); 
     but_back.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (photo_num > 1) { 
        photo_num--; 
        imageView.setImageResource(getResources().getIdentifier("bri_".concat(String.valueOf(photo_num)), "drawable", getPackageName())); 
        but_next.setEnabled(true); 
       } else { 
        but_back.setEnabled(false); 
       } 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.side_screen, menu); 
     SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
     SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     return super.onCreateOptionsMenu(menu); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.item1) { 
      finish(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("menu courses") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 

    public void but_wel(View view) { 
     TextView texwel= (TextView) findViewById(R.id.welcome); 
     Intent intent=new Intent(this,com_under.class); 
     startActivity(intent); 
    } 
} 

ComputerAdapter.java 

package com.example.asus.myapplication; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.TextView; 

import java.lang.reflect.Type; 
import java.util.HashMap; 
import java.util.List; 

/** 
* Created by ASUS on 20/09/16. 
*/ 
public class ComputerAdapter extends BaseExpandableListAdapter { 
    private Context cxt; 
    private HashMap<String,List<String>>Computer_Category; 
    private List<String>Computer_List; 
    public ComputerAdapter(Context cxt,HashMap<String,List<String>>Computer_category,List<String>Computer_list){ 
     this.cxt=cxt; 
     this.Computer_Category=Computer_category; 
     this.Computer_List=Computer_list; 
    } 


    @Override 
    public int getGroupCount() { 
     return Computer_List.size(); 
    } 

    @Override 
    public int getChildrenCount(int arg0) { 
     return Computer_Category.get(Computer_List.get(arg0)).size(); 
    } 

    @Override 
    public Object getGroup(int arg0) { 
     return Computer_List.get(arg0); 
    } 

    @Override 
    public Object getChild(int parent, int child) { 
     return Computer_Category.get(Computer_List.get(parent)).get(child); 
    } 

    @Override 
    public long getGroupId(int arg0) { 
     return arg0; 
    } 

    @Override 
    public long getChildId(int parent, int child) { 
     return child; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentview) { 
     String group_title= (String) getGroup(parent); 
     if (convertView==null){ 
      LayoutInflater inflator= (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView=inflator.inflate(R.layout.parent_layout,parentview,false); 
     } 
     TextView parent_txetview= (TextView) convertView.findViewById(R.id.parent_txt); 
     parent_txetview.setTypeface(null, Typeface.BOLD); 
     parent_txetview.setText(group_title); 
     return convertView; 
    } 

    @Override 
    public View getChildView(int parent, int child, boolean LastChild, View convertView, ViewGroup parentview) { 
     String child_title= (String) getChild(parent,child); 
     if (convertView==null){ 
      LayoutInflater inflater= (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView=inflater.inflate(R.layout.child_layout,parentview,false); 
     } 
     TextView child_textview= (TextView) convertView.findViewById(R.id.chlid_txt); 
     child_textview.setText(child_title); 
     return convertView; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return false; 
    } 
} 

여기에 추가해야하는 코드가 있습니까?

DataProvider.java 

package com.example.asus.myapplication; 

import android.widget.ExpandableListView; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

/** 
* Created by ASUS on 20/09/16. 
*/ 
public class DataProvider { 
    public static HashMap<String,List<String>>getInfo() 
    { 
     HashMap<String,List<String>>ComputerDetails=new HashMap<String, List<String>>(); 
       List<String>Computer_Engineering=new ArrayList<>(); 
     Computer_Engineering.add("UNDERGRUATE"); 
     Computer_Engineering.add("GRUATE"); 
     List<String>Software_Engineering=new ArrayList<>(); 
     Software_Engineering.add("UNDERGRUATE"); 
     Software_Engineering.add("GRUATE"); 
     List<String>Information_Engineering=new ArrayList<>(); 
     Information_Engineering.add("UNDERGRUATE"); 
     Information_Engineering.add("GRUATE"); 
     ComputerDetails.put("Computer Engineering",Computer_Engineering); 
     ComputerDetails.put("Software Engineering",Software_Engineering); 
     ComputerDetails.put("Information Engineering",Information_Engineering); 
     return ComputerDetails; 



    } 
} 

참고 : 활동의 끝에서 expandablelistview :

side_screen.Xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.asus.myapplication.side_screen" 
    android:background="#d6d7d7"> 
    <include layout="@layout/toolbar" 
     android:id="@+id/toolbar"/> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:background="@drawable/cover" 
     android:id="@+id/imageView4" 

     android:orientation="vertical" 
     android:layout_below="@+id/toolbar" 
     android:layout_alignParentStart="true"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Welcome To Labib Academey " 
      android:textSize="25dp" 
      android:layout_marginTop="22dp" 
      android:id="@+id/welcome" 
      android:onClick="but_wel" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
      android:textColor="#025e1e"/> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Now,choose your cours from " 
      android:textSize="23dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/welcome2" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
      android:textColor="#025e1e"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text=" computer sciences below." 
      android:textSize="23dp" 
      android:layout_marginTop="10dp" 
      android:id="@+id/welcome3" 
      android:layout_gravity="center" 
      android:textStyle="bold" 
      android:textColor="#025e1e"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:orientation="vertical" 
     android:background="#0a5951" 

     android:layout_marginTop="200dp" 
     android:id="@+id/linearLayout3"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="The best websites in computer science" 
      android:layout_gravity="center" 
android:paddingBottom="15dp" 
      android:textColor="#ffffffff" 
      android:textSize="19dp" 
      android:textStyle="bold" 
      android:id="@+id/textView7" 
      android:layout_marginTop="20dp" 
      android:layout_below="@+id/imageView4" 
      android:layout_centerHorizontal="true" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="20dp" 
     android:orientation="horizontal" 
     android:background="#ffffff" 
     android:gravity="center" 
     android:layout_marginTop="375dp" 
     android:id="@+id/linearLayout2" 
     android:layout_centerHorizontal="true"> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:background="#0a5951" 
     android:orientation="vertical" 
     android:layout_marginTop="390dp" > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Computer courses" 
      android:textColor="#ffffff" 
      android:textStyle="bold" 
      android:textSize="20dp" 
      android:padding="15dp" 
      android:gravity="center"/> 
    </LinearLayout> 



    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#f7f3f3" 
     android:layout_marginTop="260dp" 
     android:layout_alignBottom="@+id/linearLayout2" 
     android:layout_alignParentStart="true"> 
     <ImageView 
      android:layout_width="270dp" 
      android:layout_height="140dp" 
      android:id="@+id/image_show" 
      android:src="@drawable/bri_4" 
      android:layout_gravity="center_horizontal" 
      android:layout_below="@+id/linearLayout3" 
      android:layout_centerHorizontal="true" /> 
     <ImageButton 
      android:layout_width="50dp" 
      android:layout_height="80dp" 
      android:id="@+id/next" 
      android:src="@mipmap/ic_next1" 
      android:background="@color/color0" 
      android:layout_marginTop="25dp" 
      android:layout_below="@+id/linearLayout3" 
      android:layout_alignParentEnd="true" /> 

     <ImageButton 
      android:layout_width="50dp" 
      android:layout_height="80dp" 
      android:id="@+id/back" 
      android:background="@color/color0" 
      android:layout_marginTop="25dp" 
      android:src="@mipmap/ic_back1" 
      android:layout_alignBottom="@+id/next" 
      android:layout_alignParentStart="true" /> 
    </RelativeLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/scroll" 
      android:background="#f9f7f7" 
      android:layout_marginTop="450dp"> 


      <ExpandableListView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/exp_list" 
       android:divider="#A4C739" 
       android:paddingLeft="15dp" 
       android:paddingRight="15dp" 
       android:dividerHeight="1dp" 
       android:indicatorRight="?android:attr/expandableListPreferredChildIndicatorRight" 
       android:nestedScrollingEnabled="true"/> 
     </LinearLayout> 



</RelativeLayout> 

parent_screen.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"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/parent_txt" 
    android:textColor="#1a912c" 
    android:textSize="20dp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp"/> 
</LinearLayout> 

내 가능한 한 빨리 도와주세요.

+0

귀하의 질문은 내가 바로 다음 질문을 가지고 있다면 당신은 그냥 Exp_list'에서 활동을 시작해야 .. 매우 나쁘다 .setOnItemClickListener'. 너무 많은 코드를 작성한 사람이라면이 작은 것을 알기가 어려워서는 안됩니다 ... 내가 당신의 질문을 잘못하면 당신이 원하는 것에 대해 자세히 써야합니다 .. – San

답변

0

당신은 당신의 ExpandableListView에 리스너를 설정해야하고 그것에서 활동을 엽니 다

listView.setOnItemClickListener(new OnItemClickListener(onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // Launch the activity 
    Intent intent = new Intent(context, ActivityToOpen.class); 
    context.startActivity(intent); 
}); 
+0

예를 들어 제가 가고 싶습니다. 첫 번째 항목을 두 번째 주소 아래에 클릭 할 때 다른 활동 ... 어떻게 쓸 수 있습니까, 제발 –

+0

나는 당신이하고 싶은 것을 정확히 이해하지 못합니다. 목록의 첫 번째 항목을 클릭 할 때만 액티비티를 여는 것을 의미하는 경우 클릭 리스너 내에서 position이 0인지 확인할 수 있습니다. 그렇다면 활동을 엽니 다. –

+0

도움을 주셔서 감사합니다 –

관련 문제