質問

私は2つのフラグメントがあるアプリを作成し、両方のフラグメントにListViewsがある。fragment1の最初のListViewがスクロールされており、アイテムも強調表示されています。しかし、2番目の断片では、ListViewはスクロールされず、アイテムが強調表示されていません。誰かが私に問題があるものを教えてもらえますか?ここでのことは、同じフラグメントクラスを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>
.

役に立ちましたか?

解決

だから、あなたのコードに従ってあなたはあなたのFragment1 classの両方のフラグメントに同じmain.xmlを参照しているようです。私はあなたの活動クラスが oncreate()メソッドの setContentView()を含むと仮定します。両方のフラグメントが単一のアクティビティ上にあるので、最初に1つのビューだけが強調表示される可能性があります。これをチェックしましたが、うまく機能しています。むしろあなたはそれをスクロールしていたかもしれません。2番目のListViewが強調表示されている場合は、XMLファイル(fragment1とfragment2)を区切る必要があるかもしれません(safrment1とfragment2)、フラグメントのクラスを別々にして、次のコードを追加して最初に焦点を合わせることができます。

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

頑張ってください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top