希望你们帮帮我..
我有一个包含5行的列表视图。当我点击一行时,它正在打开另一个活动类。现在我想要的是我想打开一个新的listview而不是点击新的活动。总共我有6个列表视图。 我想在上一个列表视图的右侧打开我的新列表视图,以便列表视图都可见。 我对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;
}
}
});
是我的预期输出,当我点击第二行时,它应该在它的右侧打开新的列表视图。 请帮助我实现这一目标。提前谢谢。
答案 0 :(得分:1)
您应该使用片段来执行此操作。在FragmentActivity中会有两个片段。一个片段将包含与您一样的5个元素的列表视图。另一个片段将包含一个listView。当您单击5个元素之一时,您必须更新包含该元素的相应列表视图的其他片段。 Android平板电脑的设置应用程序可能是您尝试做的一个很好的例子。
查看此链接,可能对您有所帮助。你会得到你需要的东西。
http://www.vogella.com/articles/AndroidFragments/article.html
答案 1 :(得分:1)
好的,试试这个
首先,您必须对布局xml文件进行一些更改
布局:
<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>
您的活动的其他更改就像那样
活动
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);
}