【问题标题】:How to create NewActivity on Child of ExpandableListView?如何在 ExpandableListView 的子级上创建 NewActivity?
【发布时间】:2014-11-13 00:00:26
【问题描述】:

我创建了可扩展列表视图男子和女子运动。在男子和女子运动下,我们可以选择其中一项运动。我让我的孩子在点击时做出回应,让我回应“棒球被点击”。我的问题是我如何在点击我的一项运动后获得 NewActivity(打开新页面)(例如,如果我点击棒球将为我打开新页面,我将能够显示上一场比赛的结果) . Here is my code:

activity_main.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="match_parent"
    android:orientation="vertical"
    android:background="@drawable/mtfinal" 
    >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:dividerHeight="1.5dp" 
        >
    </ExpandableListView>

</LinearLayout>

MainActivity.java:

     package com.example.athletic_project.java;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.Toast;

    public class MainActivity<View> extends ActionBarActivity {

        ExpandableListView exv;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            exv=(ExpandableListView)findViewById(R.id.expandableListView1);
            exv.setAdapter(new MyAdapter(this));

            exv.setOnChildClickListener(new OnChildClickListener(){


            @Override
                public boolean onChildClick(ExpandableListView parent,
                        android.view.View v, int groupPosition, int childPosition,
                        long id) {
                    // TODO Auto-generated method stub
                    String itemclicked=MyAdapter.childList[groupPosition][childPosition];
                    Toast.makeText(MainActivity.this, itemclicked + " is clicked.", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

        }
    }
MyAdapter.java:

package com.example.athletic_project.java;

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

public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
Typeface typeface;

static String []parentList = {"Men's Sports","Women's Sports"};
static String [][]childList = {
    {
        "Baseball","Basketball","Bowling","Cross Country","Golf","Soccer","Track & Field"
    },
    {
        "Baseball","Basketball","Bowling","Cross Country","Golf","Soccer","Track & Field","Volleyball"
    }
};

    public MyAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context=context;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return parentList.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childList[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        typeface=Typeface.createFromAsset(context.getAssets(),"fonts/KGTribecaStamp.ttf");
        TextView tv = new TextView(context);
        tv.setText(parentList[groupPosition]);
        tv.setPadding(45, 10, 10, 10);
        tv.setTextSize(18);
        tv.setTextColor(Color.BLUE);
        tv.setTypeface(typeface);
        return tv;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        typeface=Typeface.createFromAsset(context.getAssets(),"fonts/KGTribecaStamp.ttf");
        TextView tv = new TextView(context);
        tv.setText(childList[groupPosition][childPosition]);
        tv.setPadding(45, 10, 10, 10);
        tv.setTextSize(15);
        tv.setTextColor(Color.WHITE);
        tv.setTypeface(typeface);
        return tv;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

【问题讨论】:

    标签: android xml android-layout android-activity expandablelistview


    【解决方案1】:

    使用子项点击监听器和switch语句来知道哪个被选中

    expListView.setOnChildClickListener(new OnChildClickListener() {
    
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        switch (groupPosition)
        {
        case 0:
                switch (childPosition)
                {
                case 0:
                    Baseball();
                    break;
                case 1:
                    //do something
                    break;
                }
          case 1:
                switch (childPosition)
    
                 {
                case 0:
                    football();
                    break;
                case 1:
                    //do something
                    break;
                }
        }
        return false;
    }
    
    private void baseball() {
        Intent myIntent = new Intent(MainActivity.this, baseball.class);
        startActivity(myIntent);
    
    
    }
    
    private void football() {
        Intent myIntent = new Intent(MainActivity.this, football.class);
        startActivity(myIntent);
    
    }   });   }
    

    现在创建 2 类ballball.java 和 football.java

    import android.app.Activity;
    import android.os.Bundle;
    
    public class football extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.football_activity);
    
    }}
    

     import android.app.Activity;
      import android.os.Bundle;
    
      public class baseball extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baseball_activity);
    
    }}
    

    现在需要 2 个 xml 填充 football_activity.xml 和 balloon_activity.xml 创建这些后,我们需要转到 AndroidManifest

    将这些添加到 AndroidManifest

    <activity
            android:name=".baseball"
            android:parentActivityName=".MainActivity" >
        </activity>
        <activity
            android:name=".football"
            android:parentActivityName=".MainActivity" >
        </activity>
    

    完成! :)

    【讨论】:

    • 我有一个问题,我尝试了这段代码,每次如果我点击棒球,我的应用程序就会停止工作。如果我点击任何其他运动什么都不会发生。你知道出了什么问题吗?
    【解决方案2】:

    以下是您开始新活动的方法:

    Intent myIntent = new Intent(this.getActivity(), NewActivity.class);
    this.getActivity().startActivity(myIntent);
    this.getActivity().finish();
    

    【讨论】:

    • 我应该在 NewActivity 类下创建什么?
    • 您只需写下您希望启动的活动的名称。
    • 我必须把 Intent 部分放在哪里?
    • 您可能不想使用finish(),因为我想您希望能够导航回MainActivity;但是,您将把它放在您的 onChildClick 中。
    猜你喜欢
    • 2012-05-16
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多