私はAndroidプログラミングの初心者です
アクティビティに2つのリストビューと2つのボタンがあります。最初のボタンを押すと、1つのリストビューが作成されてアクティビティに表示され、2番目のボタンをクリックすると、2番目のリストビューが作成され、最初のリストビューを閉じる必要があります。
誰かが私にこれを行う方法を提案できますか?
前もって感謝します..
簡単なオプションは、ボタンクリック時に実行時にリストのリストアダプタを変更することです。
お気に入り
on first button click
list.setAdapter(adapter1);
on second button click
list.setAdapter(adapter2);
また、タブを使用して2つのリストを表示することもできます。
http://joshclemm.com/blog/?p=59
と
http://www.edumobile.org/android/android-beginner-tutorials/tab-control/
それはタブインターフェースのように聞こえます。だからこれは簡単なはずです
http://developer.android.com/reference/android/app/ActionBar.html#newTab()
そこにあるドキュメントには、フラグメントを使用した完全な実装例があります。これまでフラグメントを使用したことがないかもしれませんが、恐れることはありません。基本的には単なるビューです。リストビューを飛び回らせる前に、例を機能させてください。
お役に立てれば!
フラグメントの使用を検討する必要があります。メインアクティビティでフレームレイアウトを使用します。次に、ユーザーの選択に応じて、フラグメントの1つを動的にロードします。
case (R.id.button1):
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.fragment, frag1);
fragmentTransaction.commit();
break;
http://developer.android.com/guide/components/fragments.html&&http://android-developers.blogspot.com/2011/02/android-30-fragments-api.htmlを参照してください
これを試して、
2番目のボタンを押している間
firstlistView.setVisibility(1);(1->Invisible state)first listview goes invisible state
secondlistview.setVisiblity(0);(0->Visible)Ur second listview shown by this code
2つの異なるボタンに2つのリストを表示する場合は、2つの異なるArrayAdapterと変更リストを含む1つのリストのみを使用できます。
サンプルコード...
このコードをxmlファイルに入れます
<RelativeLayout 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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="List1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:text="List2" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
</RelativeLayout>
このコードをアクティビティに入れてください
public class MainActivity extends Activity {
ListView listView1;
Button list1Button,list2Button;
ArrayAdapter<String> adapter1,adapter2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView)findViewById(R.id.listView1);
list1Button = (Button)findViewById(R.id.button1);
list2Button = (Button)findViewById(R.id.button2);
String[] names1 = {"Android","Iphone","Titenium"};
String[] names2 = {"java",".net","php"};
adapter1 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names1);
adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names2);
listView1.setAdapter(adapter1);
list1Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
listView1.setAdapter(null);
listView1.setAdapter(adapter1);
}
});
list2Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
listView1.setAdapter(null);
listView1.setAdapter(adapter2);
}
});
}
}
これがお役に立てば幸いです。