gpt4 book ai didi

android - 从以前的 ListView 打开一个新的 ListView

转载 作者:行者123 更新时间:2023-11-29 21:31:30 27 4
gpt4 key购买 nike

希望大家帮帮我..

我有一个包含 5 行的 ListView 。当我单击一行时,它正在打开另一个 Activity 类。现在我想要的是我想打开一个新的 ListView 而不是点击新的 Activity 。我总共有 6 个 ListView 。我想在前一个 ListView 的右侧打开我的新 ListView ,以便两个 ListView 都可见。我对 onclick 监听器的编码是

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

Here是我的预期输出,当我单击第二行时,它应该在它的右侧打开新的 ListView 。请帮助我实现这一目标。提前致谢。

最佳答案

好的,试试这个

首先,您必须对布局 xml 文件进行一些更改

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>

以及您的 Activity 的其他更改

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

就是这样,希望对你有帮助

对于按下后退按钮,您必须重写 onbackPressed 方法,例如

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

关于android - 从以前的 ListView 打开一个新的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19377480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com