open a new listview from previous listview

南笙酒味 提交于 2019-12-03 21:27:31

ok, try this

First you have to do some changes on your layout xml file

Layout :

<LinearLayout 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"
android:baselineAligned="false"
android:orientation="horizontal"
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" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#fff123"
     />


<ListView
    android:id="@+id/listView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#ccc987"
    android:visibility="gone" />

<!-- add other list as you like -->

</LinearLayout>

And other changes on your Activity like that

Activity

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ListView listView1;
    ListView listView2;
    String[] items = { "item 1", "item 2", "item 3", "item 4", "item 5" };
    String[] items_list_2 = { "items_list_2", "items_list_2", "items_list_2",
            "items_list_2", "items_list_2", "items_list_2", "items_list_2",
            "items_list_2", "items_list_2", "items_list_2", "items_list_2" };

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

        listView1 = (ListView) findViewById(R.id.listView1);
        listView1.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, items));
        listView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                switch (position) {
                case 0:
                    new loadData().execute();
                    break;

                default:
                    break;
                }
            }
        });
    }

    @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;
    }

    private class loadData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            listView2 = (ListView) findViewById(R.id.listView2);
            listView2.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, items_list_2));
            listView2.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, "item_list_2 : " + arg2,
                            Toast.LENGTH_SHORT).show();
                }
            });

            listView2.setVisibility(View.VISIBLE);
        }
    }
}

that's it, I hope this help you

For back button pressed you have to override onbackPressed method like

@Override
public void onBackPressed() {
//hide the second list here
  listView2.setVisibility(View.GONE);
}

You should use fragment to do this. In your FragmentActivity there will be two fragments. One fragment will contain the listview of 5 elements like you did. And the other fragment will contain a listView. When you click on one of the 5 elements , then you have to update the other fragment that holds the appropriate listview for that element. Probably the Settings app of Android Tablets is a good example of what you are trying to do.

See this link , might be helpful for you. You'll get what you need here.

http://www.vogella.com/articles/AndroidFragments/article.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!