5

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>
4

1 に答える 1

3

したがって、あなたのコードによるとFragment1 class、あなたのmain.xml. あなたのアクティビティ クラスには、onCreate()メソッドにsetContentView()が含まれているだけだと思います。両方のフラグメントが 1 つのアクティビティにあるため、最初に 1 つのビューのみが強調表示される可能性があります。これを確認しましたが、問題なく動作しています。むしろ、スクロールしていた可能性があります。2 番目のリストビューを強調表示したい場合は、別の xml ファイル (フラグメント 1 とフラグメント 2 など) とフラグメント用の別のクラスが必要になる可能性があります。次のコードを追加して、最初に必要なものに焦点を当てます。Just drag the listview in the second fragment,

listView1 = (ListView)findViewById(R.id.listView1);
listView1.requestFocus();

幸運を。

于 2012-10-01T10:38:49.653 に答える