只今、アンドロイドの開発をしているのですが
壁にぶち当たりましたので質問させて下さい。
イメージとしましては、リストビューがひょうじされていまして、
リストの内のテキストをタッチすると画面遷移するといったものです。
現時点での、問題点が画面遷移はでき新しくフラグメントが表示されるのですが、
下のlistViewが透けてイベントが発生して、どんどん次から次へとフラグメントが生成されてしまいます。
リストが選択された時の処理に問題があると思うのですが、修正の仕方が検討もつきません。
理解されている方がいらっしゃいましたら、ご教授をお願いいたします。
説明やコードが不足しておりましたら、教えてください。
android
1package com.example.user.sample; 2 3import android.os.Bundle; 4import android.support.v4.app.FragmentActivity; 5import android.support.v4.view.ViewPager; 6 7public class MainActivity extends FragmentActivity { 8 9 ViewPager viewPager; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 viewPager = (ViewPager) findViewById(R.id.pager); 16 viewPager.setAdapter( 17 new Adapter( 18 getSupportFragmentManager())); 19 } 20 21}
android
1package com.example.user.sample; 2 3import android.os.Bundle; 4import android.support.annotation.Nullable; 5import android.support.v4.app.Fragment; 6import android.support.v4.app.FragmentManager; 7import android.support.v4.app.FragmentTransaction; 8import android.view.LayoutInflater; 9import android.view.View; 10import android.view.ViewGroup; 11import android.widget.AdapterView; 12import android.widget.ListView; 13 14import java.util.ArrayList; 15 16import static com.example.user.sample.R.id.listView; 17 18public class Fragment1 extends Fragment { 19 20 21 @Override 22 public View onCreateView(LayoutInflater inflater, 23 ViewGroup container, 24 Bundle savedInstanceState) { 25 return inflater.inflate(R.layout.fragment_fragment1, null); 26 } 27 //listをlistViewに結び付ける 28 ListView lv = (ListView)view.findViewById(listView); 29 30 //Adapterのインスタンス化 31 //第3引数にlabelListを渡す 32 CustomAdapter mAdapter = new CustomAdapter(getActivity(),0,labelList); 33 34 //リストにAdapterをセット 35 lv.setAdapter(mAdapter); 36 37 38 39 //リストが選択された時の処理 40 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 41 42 @Override 43 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 44 45 46 Fragment3 fg3 = new Fragment3(); 47 Bundle bundle = new Bundle(); 48 bundle.putInt("selected", position); 49 fg3.setArguments(bundle); 50 51 FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 52 FragmentTransaction transaction = fragmentManager.beginTransaction(); 53 transaction.replace(R.id.main_fragment, fg3); 54 55 transaction.addToBackStack(null); 56 transaction.commit(); 57 58 } 59 }); 60 61
android
1package com.example.user.sample; 2 3import android.os.Bundle; 4import android.support.v4.app.Fragment; 5import android.view.LayoutInflater; 6import android.view.View; 7import android.view.ViewGroup; 8import android.widget.TextView; 9 10public class Fragment3 extends Fragment { 11 12 @Override 13 public View onCreateView(LayoutInflater inflater, 14 ViewGroup container, 15 Bundle savedInstanceState) { 16 Bundle args = getArguments(); 17 int selected = args.getInt("selected"); 18 19 View view = inflater.inflate(R.layout.fragment_fragment3, container, false); 20 TextView tv = (TextView)view.findViewById(R.id.textView3); 21 tv.setText(String.valueOf(selected + 1) + "番が選択されました。"); 22 23 return view; 24 } 25 26}
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/fragment3" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.user.sample.Fragment3"> 7 8 <!-- TODO: Update blank fragment layout --> 9 <TextView 10 android:id="@+id/textView3" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:layout_gravity="center" 14 android:background="@color/colorAccent" 15 android:gravity="center_vertical" 16 android:textAlignment="center" 17 android:textSize="30sp" /> 18 19 20</FrameLayout> 21
回答1件
あなたの回答
tips
プレビュー