2 つのフラグメントがあり、両方のフラグメントにリストビューがあるアプリを作成しました。fragment1 の最初のリストビューがスクロールされ、項目も強調表示されています。しかし、2 番目のフラグメントでは、リストビューはスクロールされず、アイテムも強調表示されません。誰かが私に何が問題なのか教えてもらえますか? ここでのことは、同じフラグメント クラスを xml の両方のフラグメントに配置してこれを確認したことです。一方が他方と変わらないため、両方が機能するか、両方が機能しないかのどちらかです。しかし、なぜこの問題が発生するのでしょうか。
私のフラグメントクラス:
public class Fragment1 extends ListFragment{
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment1,container,false);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v,int position, long id)
{
Toast.makeText(getActivity(), "You have selected "+countries[position], Toast.LENGTH_SHORT).show();
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:name="com.example.listfragmentexample.Fragment1"
android:id="@+id/fragment1"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="200dp" />
<fragment
android:name="com.example.listfragmentexample.Fragment1"
android:id="@+id/fragment2"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="300dp"/>
</LinearLayout>
fragment1.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>