open a new listview from previous listview

戏子无情 提交于 2019-12-05 06:15:03

问题


Hope you guys help me..

I have a listview with 5 rows in it. When i click a row it is opening another activity class. Now what i want is i would like to open a new listview instead of new activity on click. Totally i have 6 listviews. I would like to open my new listview on the rightside of the previous listview so that both the listviews will be visible. my coding for onclick listener is

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
            long paramLong) {
        switch( position )
{
   case 0:  Intent newActivity0 = new Intent(YourActivity.this, second.class);     
            startActivity(newActivity0);
            break;
   case 1:  Intent newActivity1 = new Intent(YourActivity.this, third.class);     
            startActivity(newActivity1);
            break;
   case 2:  Intent newActivity2 = new Intent(YourActivity.this, fourth.class);     
            startActivity(newActivity2);
            break;
   case 3:  Intent newActivity3 = new Intent(YourActivity.this, fifth.class);     
            startActivity(newActivity3);
            break;
   case 4:  Intent newActivity4 = new Intent(YourActivity.this, sixth.class);     
            startActivity(newActivity4);
            break;
}              
    }
});

is my expected output and when i click second row it should open new listview on the rightside of it. Please help me in achieving this. Thanks in advance.

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/19377480/open-a-new-listview-from-previous-listview

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